//************************************************************** // Sample Program //************************************************************** import java.net.*; import java.io.*; import java.util.*; public class SocketClient { public static void main(String[] args) { Socket socket = null; PrintWriter OutToServer = null; BufferedReader InFromServer = null; String text; Scanner sc = new Scanner(System.in); //******************************************* //Create socket connection //******************************************* try{ //***************************************************************** //***** Your souce code should be here to do the following********* // (1) Construct an object of Socket // (2) Construct an object of PrintWriter and name it OutToServer // (3) Construct an object of BufferReader and name it InFromServer //***************************************************************** //***************************************************************** } catch (UnknownHostException e) { System.out.println("Unknown host"); System.exit(1); } catch (IOException e) { System.out.println("No I/O"); System.exit(1); } //******************************************* //Send Text to server //******************************************* while(true) { System.out.print("Please Enter Text to be sent to the server : "); text = sc.nextLine(); if(text.equalsIgnoreCase("cr")) { try { socket.close(); } catch (IOException e) { System.out.println(e); } System.out.println("Connection Has Been Closed"); break; } else { //************************************************************ //*** Your source code should be here to print *************** // (1) Print the text at the client window // (2) Send the text back to the server //************************************************************ //************************************************************ } //******************************************* //Receive text from server //******************************************* try { String line = InFromServer.readLine(); System.out.println("Text received from Server :" + line); } catch (IOException e) { System.out.println("Read failed"); System.exit(1); } } } }