I got project description and code that is not doing things that said in programm…
- User
I got project description and code that is not doing things that said in programm description.I need you to rewrite my code making it match with description-->import java.io.; import java.net.; import java.util.concurrent.*;
public class TCPPacketServer { private static int port = 1024; private static ExecutorService pool = Executors.newFixedThreadPool(10);
codepublic static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(port); while (true) { Socket clientSocket = serverSocket.accept(); pool.execute(new ClientHandler(clientSocket)); } } catch (IOException e) { e.printStackTrace(); } } private static class ClientHandler implements Runnable { private Socket clientSocket; public ClientHandler(Socket socket) { this.clientSocket = socket; } public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintStream ps = new PrintStream(clientSocket.getOutputStream()); String line; while ((line = br.readLine()) != null && !line.equals("CLOSE")) { byte[] buffer = new byte[1024]; int bytesRead = clientSocket.getInputStream().read(buffer); if (bytesRead > 0) { ByteArrayInputStream bais = new ByteArrayInputStream(buffer, 0, bytesRead); ObjectInputStream ois = new ObjectInputStream(bais); Packet packet = (Packet) ois.readObject(); System.out.println("FROM CLIENT " + clientSocket.getRemoteSocketAddress() + ": Packet SerialNo#" + packet.getSerialNo() + " is received"); Packet responsePacket = new Packet(packet.getSerialNo(), "ACK"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(responsePacket); byte[] responseBuffer = baos.toByteArray(); ps.write(responseBuffer); } } ps.close(); br.close(); clientSocket.close(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } - Assistant
- User
How to use this programm what i have to run/type/print/after what?I have to show that this project is working
- Assistant
To demonstrate that this project is working, follow these steps:
Step 1: Compile the Java files
- Open the terminal (or command prompt) and navigate to the directory containing the Java files.
- Run the following command to compile the Java files:
codejavac TCPPacketServer.java TCPPacketClient.java Packet.javaStep 2: Start the TCPPacketServer
- Run the following command to start the server:
codejava TCPPacketServerStep 3: Start the TCPPacketClient
- Open another terminal (or command prompt) and navigate to the same directory as before.
- User
Your code doing not right thing. When you run "TCPPacketServer" it have to write "Opening port..." after when you run"TCPPacketClient" it have to write "Enter the data packet:" after you write something here it will return to "TCPPacketServer" it have to look like this TCPPacketServer: Opening port...
Recieving From Clients Packet's serialNo#1 and packet's Data is THIS IS CLIENT1 Recieving From Clients Packet's serialNo#1 and packet's Data is THIS IS CLIENT2
TCPPacketClient: Enter the data packet: this is Client1 FROM SERVER:Packet SerialNo#1 is recieved Enter the data packet: this is Client2 FROM SERVER:Packet SerialNo#2 is recieved
and after runnging classes like above we run "MTServer" and "Clients" classes
in MTServer: Waiting for client request
New clientis pop up!
Server: Hello
Clients: Hi
Server:How are you?
Clients:CLOSE End of conversationin Clients: Request sent successfully Server: Hello
Clients: Hi
Server:How are you?
Clients: CLOSE END of conversationRewrite your code so it will do the same thing as I writed above
- Assistant
- User
You didn't get it, there is have to be conversation between client and server, so they could write whatever they want.And if client will write "CLOSE" conversation have to end in running terminal of "MTCServer" server have to write things he want and in running terminal of "Clients" clients have to write their own things.
- Assistant
- User
Now "Clients" and "MTServer" works perfectly, thanks. But TCPPacketServer and TCPPacketClient doesn't work as said When you run TCPPacketServer it will write "Opening port…" and wait till TCPPacketClient gives info
and after that when running TCPPacketClient will write "Enter the data packet:" and wait till user write anything. After he writes something like "Hello" like that "Enter the data packet: Hello" TCPPacketClient will give this data to TCPPacketServer and TCPPacketServer will write "Recieving From Clients Packet’s serialNo#1 and packet’s Data is Hello" and TCPPacketClient will write "FROM SERVER:Packet SerialNo#1 is recieved"
Recieving From Clients Packet’s serialNo#1 and packet’s Data is THIS IS CLIENT1 Recieving From Clients Packet’s serialNo#1 and packet’s Data is THIS IS CLIENT2
TCPPacketClient: Enter the data packet: this is Client1 FROM SERVER:Packet SerialNo#1 is recieved Enter the data packet: this is Client2 FROM SERVER:Packet SerialNo#2 is recieved
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.