0% found this document useful (0 votes)
35 views3 pages

Udpchat

The document contains Java code for a UDP chat server and client. The server listens for incoming messages on port 1234 and spawns a new thread for each client to handle communication. The client connects to the server, sends messages, and receives responses until 'bye' is typed to exit the chat.

Uploaded by

Uddipto Jana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views3 pages

Udpchat

The document contains Java code for a UDP chat server and client. The server listens for incoming messages on port 1234 and spawns a new thread for each client to handle communication. The client connects to the server, sends messages, and receives responses until 'bye' is typed to exit the chat.

Uploaded by

Uddipto Jana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package udpChatServer;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class UDPServer {


public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(1234);
System.out.println("Server started on port 1234...");

while (true) {
byte[] receiveBuffer = new byte[65535];
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer,
receiveBuffer.length);
ds.receive(receivePacket);

// Create a new thread for each client


new ClientHandler(ds, receivePacket).start();
}
}
}

class ClientHandler extends Thread {


private DatagramSocket socket;
private InetAddress clientAddress;
private int clientPort;

public ClientHandler(DatagramSocket socket, DatagramPacket initialPacket) {


this.socket = socket;
this.clientAddress = initialPacket.getAddress();
this.clientPort = initialPacket.getPort();

String clientMessage = new String(initialPacket.getData(), 0,


initialPacket.getLength());
System.out.println("Client " + clientAddress + ":" + clientPort + " says: "
+ clientMessage);

if (clientMessage.equalsIgnoreCase("bye")) {
System.out.println("Client " + clientAddress + ":" + clientPort + "
disconnected");
return;
}
}

public void run() {


try {
while (true) {
// Read server response from console
System.out.print("Response to " + clientAddress + ":" + clientPort
+ ": ");
Scanner in = new Scanner(System.in);
byte[] sendBuffer = in.nextLine().getBytes();

// Send response to the specific client


DatagramPacket sendPacket = new DatagramPacket(
sendBuffer, sendBuffer.length, clientAddress, clientPort);
socket.send(sendPacket);

if (new String(sendBuffer).equalsIgnoreCase("bye")) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

client:

package udpChatServer;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class UDPClient {


public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
DatagramSocket ds = new DatagramSocket();
InetAddress ip = InetAddress.getLocalHost();
byte[] sendBuffer;
byte[] receiveBuffer = new byte[65535];

System.out.println("Connected to server. Type 'bye' to exit.");

while (true) {
// Client sends message
System.out.print("You: ");
String message = sc.nextLine();
sendBuffer = message.getBytes();
DatagramPacket DpSend = new DatagramPacket(sendBuffer,
sendBuffer.length, ip, 1234);
ds.send(DpSend);

// Exit condition
if (message.equalsIgnoreCase("bye")) {
break;
}

// Receive response from server


DatagramPacket DpReceive = new DatagramPacket(receiveBuffer,
receiveBuffer.length);
ds.receive(DpReceive);
String serverMessage = new String(receiveBuffer, 0,
DpReceive.getLength());
System.out.println("Server: " + serverMessage);

// Clear the buffer


receiveBuffer = new byte[65535];
}
ds.close();
sc.close();
}
}

You might also like