/**

 * Example Client program using TCP.

 */

 

public class Tclient {

 

      final static  String    serverIPname = "minnie.williams.org";     // server IP name

      final static  int       serverPort   = 3456;                      // server port number

 

public static void main(String args[]) {

      java.net.Socket         sock = null;                              // Socket object for communicating

      java.io.PrintWriter     pw   = null;                              // socket output to server

      java.io.BufferedReader  br   = null;                              // socket input from server

 

      try {

            sock = new java.net.Socket(serverIPname,serverPort);       // create socket and connect

            pw   = new java.io.PrintWriter(sock.getOutputStream(), true);  // create reader and writer

            br   = new java.io.BufferedReader(new java.io.InputStreamReader(sock.getInputStream()));

            System.out.println("Connected to Server");

 

            pw.println("Message from the client");                      // send msg to the server

            System.out.println("Sent message to server");

            String answer = br.readLine();                              // get data from the server

            System.out.println("Response from the server >" + answer);

 

            pw.close();                                                 // close everything

            br.close();

            sock.close();

      } catch (Throwable e) {

            System.out.println("Error " + e.getMessage());

            e.printStackTrace();

      }

}

}


/**

 * Example Server program using TCP.

 */

 

public class Tserver {

 

      final static      int   serverPort   = 3456;                      // server port number

 

public static void main(String args[]) {

      java.net.ServerSocket   sock = null;                              // original server socket

      java.net.Socket         clientSocket = null;                      // socket created by accept

      java.io.PrintWriter     pw   = null;                              // socket output stream

      java.io.BufferedReader  br   = null;                              // socket input stream

 

      try {

            sock = new java.net.ServerSocket(serverPort);               // create socket and bind to port

            System.out.println("waiting for client to connect");

            clientSocket = sock.accept();                               // wait for client to connect

            System.out.println("client has connected");

            pw   = new java.io.PrintWriter(clientSocket.getOutputStream(),true);

            br   = new java.io.BufferedReader(

new java.io.InputStreamReader(clientSocket.getInputStream()));

 

            String msg = br.readLine();                                 // read msg from client

            System.out.println("Message from the client >" + msg);

            pw.println("Got it!");                                      // send msg to client

 

            pw.close();                                                 // close everything

            br.close();

            clientSocket.close();

            sock.close();

      } catch (Throwable e) {

            System.out.println("Error " + e.getMessage());

            e.printStackTrace();

      }

}

}