//************************************************************** // Sample Program //************************************************************** import java.net.*; import java.io.*; public class SocketServer { public static void main(String args[]) { ServerSocket server = null; Socket client = null; BufferedReader InFromClient = null; PrintWriter OutToClient = null; String line; try { //***************************************************************** //***** Your souce code should be here to do the following********* // (1) Construct an object of ServerSocket and name it server //***************************************************************** //***************************************************************** System.out.println("Socket Open on Port 4444"); } catch (IOException e) { System.out.println("Could not listen on port 4444"); System.exit(-1); } try { //***************************************************************** //***** Your souce code should be here to do the following********* // Note: Please find the information on accept() method of ServerSocket // (1) call accept() of an object of ServerSocket and // (2) assign the result to an Socket object named "client" which // defined above. //***************************************************************** //***************************************************************** } catch (IOException e) { System.out.println("Accept failed: 4444"); System.exit(-1); } try { //************************************************************ //*** Your source code should be here to print *************** // (1) Construct an object of BufferedReader to receive // a text from client and assign to InFromClient // (2) Construct an object of PrintWriter to send // the text back to the server and assign to OutToClient //************************************************************ //************************************************************ } catch (IOException e) { System.out.println("Accept failed: 4444"); System.exit(-1); } while(true) { try { //************************************************************ //*** Your source code should be here to print *************** // (1) readline() from InFromClient and assign to "line" //************************************************************ //************************************************************ // connection closed by client if(line.equalsIgnoreCase("cr")) { System.out.println("Connection Closed Client"); break; } System.out.println("Text from Client : "+line); //Send data back to client with stamp //************************************************************ //*** Your source code should be here to print *************** // (1) println() to the OutToClient //************************************************************ //************************************************************ } catch (IOException e) { System.out.println("Read failed"); System.exit(-1); } } try { InFromClient.close(); OutToClient.close(); server.close(); } catch (IOException e) { System.out.println("Could not close."); System.exit(-1); } } }