subject
Engineering, 22.02.2020 01:42 precioussmith76

Socket Programming in Java: Make changes in the client code, so that the client allows the user to continue to send multiple requests until the user types in "Quit". This means that the client process should not exit after the user sends one request and receives one response. Instead, the client process should be able to receive subsequent inputs from the user. You need to have a loop in the client code, so that it can accept the user request until the user explicitly types in "Quit". Please comment on what each line does.

You are required to have three files:

The modified UDP client code. (preferred file name: UDPClient. py or UDPClient. java)

The modified TCP client code. (preferred file name: TCPClient. py or TCPClient. java)

The project report, which must include the tests you have run to verify that your code meets the requirements. You can paste what you got in the console or some screenshots.

In this project, you don’t need to change anything on the server side. Please run the given code on your machine to understand the code and the original application, before you make changes to the code. Make sure your code works without errors before submission.

The behaviors of the application are as follows:

1. client reads a line of characters (data) from its keyboard and sends data to server.

2. server receives the data and converts characters to uppercase.

3. server sends modified data to client; continue to run to serve other clients.

4. client receives modified data and displays line on its screen and then exits.

We assume that both the client and the server run on the same machine.

Java Socket Programming

If we use UDP sockets, we have the following code:

Java code for the UDP client

import java. io.*;

import java. net.*;

import java. util. Scanner;

public class UDPClient {

private final static int PORT = 5000;

private static final String HOSTNAME = "localhost";

public static void main(String[] args) {

System. out. println("This is the UDP Client.");

try {

DatagramSocket socket = new DatagramSocket(0);

Scanner scanner = new Scanner(System. in);

String requestString = scanner. nextLine();

scanner. close();

byte[] requestBuffer = requestString. getBytes();

InetAddress host = InetAddress. getByName(HOSTNAME);

DatagramPacket request = new DatagramPacket(requestBuffer, requestBuffer. length, host, PORT);

socket. send(request);

DatagramPacket response = new DatagramPacket(new byte[1024], 1024);

socket. receive(response);

String result = new String(response. getData());

System. out. println(result);

} catch (IOException e) {

e. printStackTrace();

}

}

}

Java code for the UDP server

import java. io.*;

import java. net.*;

public class UDPServer {

private final static int PORT = 5000;

public static void main(String[] args) {

System. out. println("This is the UDP Server.");

try {

DatagramSocket socket = new DatagramSocket(PORT);

while (true) {

DatagramPacket request = new DatagramPacket(new byte[1024], 1024);

socket. receive(request);

byte[] requestBuffer = request. getData();

String requestString = new String(requestBuffer);

String responseString = requestString. toUpperCase();

byte[] responseBuffer = responseString. getBytes();

DatagramPacket response = new DatagramPacket(responseBuffer, responseBuffer. length,

request. getAddress(), request. getPort());

socket. send(response);

}

} catch (IOException e) {

e. printStackTrace();

}

}

}

Java code for the TCP client

import java. net.*;

import java. io.*;

import java. util.*;

public class TCPClient {

private static final int PORT = 5000;

private static final String HOSTNAME = "localhost";

public static void main(String[] args) {

try {

Socket socket = null;

try {

socket = new Socket(HOSTNAME, PORT);

} catch (UnknownHostException e) {

e. printStackTrace();

}

System. out. println("Connected.");

DataOutputStream out = new DataOutputStream(socket. getOutputStream());

DataInputStream in = new DataInputStream(socket. getInputStream());

Scanner scanner = new Scanner(System. in);

String line = scanner. nextLine();

out. writeUTF(line);

out. flush();

String response = in. readUTF();

System. out. println(response);

scanner. close();

out. close();

in. close();

socket. close();

} catch(IOException e) {

e. printStackTrace();

}

}

}

Java code for the TCP server

import java. net.*;

import java. io.*;

public class TCPServer {

private static int PORT = 5000;

public static void main(String[] args) {

try {

ServerSocket server = new ServerSocket(PORT);

System. out. println("This is the TCP Server.");

while (true) {

Socket connectionSocket = server. accept();

System. out. println("Client accepted.");

DataInputStream in = new DataInputStream(connectionSocket. getInputStream());

DataOutputStream out = new DataOutputStream(connectionSocket. getOutputStream());

String line = in. readUTF();

String newLine = line. toUpperCase();

out. writeUTF(newLine);

out. flush();

System. out. println("Closing connection.");

connectionSocket. close();

in. close();

out. close();

}

} catch(IOException e) {

e. printStackTrace();

}

}

}

ansver
Answers: 2

Another question on Engineering

question
Engineering, 04.07.2019 18:20
An engine runs on the ideal diesel cycle. the cycle has a compression ratio of 20 and a cutoff ratio of 2. the highest temperature in the cycle is 1200 k. if the heat into the system is 300 kj/kg of working fluid and using variable specific heats determine the work produced per mass of working fluid
Answers: 3
question
Engineering, 04.07.2019 18:20
Apiston-cylinder device contains 0.1 m3 of liquid water and 0.9 m3 of water vapor in equilibrium at 800 kpa. heat is transferred at constant pressure until the temperature of water reaches 350 °c. determine (a) the quality of water at the initial state (b) the work associated with this process, (c) the heat associated with this process.
Answers: 2
question
Engineering, 04.07.2019 18:20
For each of the following process: a) sketch the p-v diagram, b)sketch t-s diagram, c) sketch t-v diagram, d) sketch the boundary work on one of the diagrams (a, b or c) and e) sketch the reversible heat transfer on one of the diagrams (a, b or c): 1- isobaric process from compressed liquid to superheated vapor 2- isothermal process from compressed liquid to superheated vapor 3- isentropic process from compressed liquid to superheated vapor
Answers: 3
question
Engineering, 05.07.2019 10:10
What is the correct way to measure the overall length of a boat? measure from the tip of the bow to the highest point on the gunwale. measure from the tip of the bow in a straight line to the stern of the boat. measure from the tip of the bow in a straight line to the back seat. measure from port to starboard at the boat's widest point.
Answers: 1
You know the right answer?
Socket Programming in Java: Make changes in the client code, so that the client allows the user to c...
Questions
question
Geography, 12.01.2021 18:00
question
Social Studies, 12.01.2021 18:00
question
English, 12.01.2021 18:00
question
Mathematics, 12.01.2021 18:00
question
Mathematics, 12.01.2021 18:00
Questions on the website: 13722367