Question: When the program starts, it should open a server socket to listen for connections on port 8081. When a client connection is received, the program
When the program starts, it should open a server socket to listen for connections on port 8081. When a client connection is received, the program should create a new thread to handle the connection. The worker thread should read a single line of text from the connection (up to the newline character), write the line back to the client connection, and close the connection. The program should accept new connections until it is forcibly shut down. The program must be multi-threaded and be capable of responding to multiple requests at once. Helpful Hints: Use the java.net.ServerSocket class to open a port and listen for new connections (see chapter 3 of the textbook for more information). Use the java.net.ServerSocket.accept() method to accept a new client connection. You can read from and write to the data streams in java.net.ServerSocket.getOutputStream() and java.net.ServerSocket.getInputStream() just as if they were files or standard input/output. I created the following program but I keep getting three errors that I do not know hot to correct. import java.net.*; //error here import java.io.*; //error here public class mserver { public static Socket s[] = new Socket[10]; public static void main(String a[]) { int i=0; try { ServerSocket ss = new ServerSocket(8081); while(true) { s[i] = ss.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(s[i].getInputStream())); String msg; try { reqhandler req = new reqhandler(s[i],i); i++; Thread t = new Thread(req); t.start(); } catch(Exception e) { System.out.println(e); } } } catch(Exception e) { System.out.println(e); } } } class reqhandler implements Runnable { public int n; public Socket s; public reqhandler(Socket soc,int i) { s = soc; n = i; } public void run() { String msg = ""; BufferedReader br; PrintWriter pw; try { br = new BufferedReader(new InputStreamReader(s.getInputStream())); msg = br.readLine(); pw = new PrintWriter(new OutputStreamWriter(mserver.s[n].getOutputStream())); pw.println(msg+" "); pw.flush(); s.close(); } catch(Exception e) { } } } client side program //error here import java.net.*; // error here import java.io.*; // error here public class mclient { public static void main(String a[]) { BufferedReader in,br; PrintWriter pw; try { Socket s = new Socket("localhost",8081); in = new BufferedReader(new InputStreamReader(System.in)); String msg; pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream())); msg = in.readLine(); pw.println(msg); pw.flush(); br = new BufferedReader(new InputStreamReader(s.getInputStream())); msg = br.readLine(); System.out.println(msg); } catch(Exception e) { System.out.println(e); } } } I keep getting error codes: error: class, interface, or enum expected client side program ^ error: class, interface, or enum expected import java.io.*; ^ 2 errors Thank you
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
