Question
Problem 3 Java Programming Reimplement the echo server and client using TCP. The TCP server should support multiple client connections. Each connection should be handled
Problem 3 Java Programming
Reimplement the echo server and client using TCP. The TCP server should support multiple client connections. Each connection should be handled by a thread. After sending a response (echo), the thread execution should stop. The main server thread, as previously, is supposed to run in a loop until manual termination.
This is the given UDP Client/ Server programme
/*
UDPEchoClient.java
A simple echo client with no error handling
*/
package dv201.labb2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
public class UDPEchoClient {
public static final int BUFSIZE= 1024;
public static final int MYPORT= 0;
public static final String MSG= "An Echo Message!";
public static void main(String[] args) throws IOException {
byte[] buf= new byte[BUFSIZE];
if (args.length != 2) {
System.err.printf("usage: %s server_name port ", args[1]);
System.exit(1);
}
/* Create socket */
DatagramSocket socket= new DatagramSocket(null);
/* Create local endpoint using bind() */
SocketAddress localBindPoint= new InetSocketAddress(MYPORT);
socket.bind(localBindPoint);
/* Create remote endpoint */
SocketAddress remoteBindPoint=
new InetSocketAddress(args[0],
Integer.valueOf(args[1]));
/* Create datagram packet for sending message */
DatagramPacket sendPacket=
new DatagramPacket(MSG.getBytes(),
MSG.length(),
remoteBindPoint);
/* Create datagram packet for receiving echoed message */
DatagramPacket receivePacket= new DatagramPacket(buf, buf.length);
/* Send and receive message*/
socket.send(sendPacket);
socket.receive(receivePacket);
/* Compare sent and received message */
String receivedString=
new String(receivePacket.getData(),
receivePacket.getOffset(),
receivePacket.getLength());
if (receivedString.compareTo(MSG) == 0)
System.out.printf("%d bytes sent and received ", receivePacket.getLength());
else
System.out.printf("Sent and received msg not equal! ");
socket.close();
}
}
/*
UDPEchoServer.java
A simple echo server with no error handling
*/
package dv201.labb2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
public class UDPEchoServer {
public static final int BUFSIZE= 1024;
public static final int MYPORT= 4950;
public static void main(String[] args) throws IOException {
byte[] buf= new byte[BUFSIZE];
/* Create socket */
DatagramSocket socket= new DatagramSocket(null);
/* Create local bind point */
SocketAddress localBindPoint= new InetSocketAddress(MYPORT);
socket.bind(localBindPoint);
while (true) {
/* Create datagram packet for receiving message */
DatagramPacket receivePacket= new DatagramPacket(buf, buf.length);
/* Receiving message */
socket.receive(receivePacket);
/* Create datagram packet for sending message */
DatagramPacket sendPacket=
new DatagramPacket(receivePacket.getData(),
receivePacket.getLength(),
receivePacket.getAddress(),
receivePacket.getPort());
/* Send message*/
socket.send(sendPacket);
System.out.printf("UDP echo request from %s", receivePacket.getAddress().getHostAddress());
System.out.printf(" using port %d ", receivePacket.getPort());
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started