Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

More Books

Students also viewed these Databases questions

Question

What are the possible influences on this process?

Answered: 1 week ago

Question

1. Describe the types of power that effective leaders employ

Answered: 1 week ago