Question: i have two java files tcp_server.java import java.net.*; import java.io.*; //tcp_server.java public class tcp_server { public static void main(String[] args){ System.out.println(tcp_Server start....); try{ ServerSocket ss=new
i have two java files
tcp_server.java
import java.net.*; import java.io.*; //tcp_server.java public class tcp_server { public static void main(String[] args){ System.out.println("tcp_Server start...."); try{ ServerSocket ss=new ServerSocket(6667); while(true) { Socket s=ss.accept();//establishes connection DataInputStream dis=new DataInputStream(s.getInputStream()); String str=(String)dis.readUTF(); System.out.println("Original massage from Client = "+str); DataOutputStream dout=new DataOutputStream(s.getOutputStream()); dout.writeUTF(str.toUpperCase()); dout.flush(); } //ss.close(); for close socket }catch(Exception e){System.out.println(e);} } } tcp_client
import java.net.*; import java.io.*; import java.util.Scanner; // import the Scanner class
//tcp_client.java public class tcp_client { public static void main(String[] args) { System.out.println("tcp_Client start...."); try{ Socket s=new Socket("localhost",6667); Scanner myObj = new Scanner(System.in); // Create a Scanner object System.out.println("Enter massage for server");
String msg = myObj.nextLine(); // Read user input System.out.println("massage is: " + msg); // Output user input DataOutputStream dout=new DataOutputStream(s.getOutputStream()); dout.writeUTF(msg); dout.flush(); DataInputStream dis=new DataInputStream(s.getInputStream()); String str=(String)dis.readUTF(); System.out.println("Modified massage from server = "+str); s.close(); System.out.println("tcp_Client closed"); }catch(Exception e){System.out.println(e);} } }
Now we will allow the client to send as many messages as it wants. Instead of ending the program after receiving the modified message from the server, we will allow the client to keep sending messages until it sends the word end to the server. Once the word end is sent to the server the program will end after receiving the servers response to that end message. Hint: For this part all you must modify is the client file.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
