Question
Please make changes in the client code, so that the client allows the user to continue to send multiple requests until the user types in
Please 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.
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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