Advertisement
bangnaga

Java Socket Programming

Jun 1st, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. The server
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. public class Provider{
  6.     ServerSocket providerSocket;
  7.     Socket connection = null;
  8.     ObjectOutputStream out;
  9.     ObjectInputStream in;
  10.     String message;
  11.     Provider(){}
  12.     void run()
  13.     {
  14.         try{
  15.             //1. creating a server socket
  16.             providerSocket = new ServerSocket(2004, 10);
  17.             //2. Wait for connection
  18.             System.out.println("Waiting for connection");
  19.             connection = providerSocket.accept();
  20.             System.out.println("Connection received from " + connection.getInetAddress().getHostName());
  21.             //3. get Input and Output streams
  22.             out = new ObjectOutputStream(connection.getOutputStream());
  23.             out.flush();
  24.             in = new ObjectInputStream(connection.getInputStream());
  25.             sendMessage("Connection successful");
  26.             //4. The two parts communicate via the input and output streams
  27.             do{
  28.                 try{
  29.                     message = (String)in.readObject();
  30.                     System.out.println("client>" + message);
  31.                     if (message.equals("bye"))
  32.                         sendMessage("bye");
  33.                 }
  34.                 catch(ClassNotFoundException classnot){
  35.                     System.err.println("Data received in unknown format");
  36.                 }
  37.             }while(!message.equals("bye"));
  38.         }
  39.         catch(IOException ioException){
  40.             ioException.printStackTrace();
  41.         }
  42.         finally{
  43.             //4: Closing connection
  44.             try{
  45.                 in.close();
  46.                 out.close();
  47.                 providerSocket.close();
  48.             }
  49.             catch(IOException ioException){
  50.                 ioException.printStackTrace();
  51.             }
  52.         }
  53.     }
  54.     void sendMessage(String msg)
  55.     {
  56.         try{
  57.             out.writeObject(msg);
  58.             out.flush();
  59.             System.out.println("server>" + msg);
  60.         }
  61.         catch(IOException ioException){
  62.             ioException.printStackTrace();
  63.         }
  64.     }
  65.     public static void main(String args[])
  66.     {
  67.         Provider server = new Provider();
  68.         while(true){
  69.             server.run();
  70.         }
  71.     }
  72. }
  73.  
  74.  
  75. The client
  76.  
  77.   import java.io.*;
  78. import java.net.*;
  79. public class Requester{
  80.     Socket requestSocket;
  81.     ObjectOutputStream out;
  82.     ObjectInputStream in;
  83.     String message;
  84.     Requester(){}
  85.     void run()
  86.     {
  87.         try{
  88.             //1. creating a socket to connect to the server
  89.             requestSocket = new Socket("localhost", 2004);
  90.             System.out.println("Connected to localhost in port 2004");
  91.             //2. get Input and Output streams
  92.             out = new ObjectOutputStream(requestSocket.getOutputStream());
  93.             out.flush();
  94.             in = new ObjectInputStream(requestSocket.getInputStream());
  95.             //3: Communicating with the server
  96.             do{
  97.                 try{
  98.                     message = (String)in.readObject();
  99.                     System.out.println("server>" + message);
  100.                     sendMessage("Hi my server");
  101.                     message = "bye";
  102.                     sendMessage(message);
  103.                 }
  104.                 catch(ClassNotFoundException classNot){
  105.                     System.err.println("data received in unknown format");
  106.                 }
  107.             }while(!message.equals("bye"));
  108.         }
  109.         catch(UnknownHostException unknownHost){
  110.             System.err.println("You are trying to connect to an unknown host!");
  111.         }
  112.         catch(IOException ioException){
  113.             ioException.printStackTrace();
  114.         }
  115.         finally{
  116.             //4: Closing connection
  117.             try{
  118.                 in.close();
  119.                 out.close();
  120.                 requestSocket.close();
  121.             }
  122.             catch(IOException ioException){
  123.                 ioException.printStackTrace();
  124.             }
  125.         }
  126.     }
  127.     void sendMessage(String msg)
  128.     {
  129.         try{
  130.             out.writeObject(msg);
  131.             out.flush();
  132.             System.out.println("client>" + msg);
  133.         }
  134.         catch(IOException ioException){
  135.             ioException.printStackTrace();
  136.         }
  137.     }
  138.     public static void main(String args[])
  139.     {
  140.         Requester client = new Requester();
  141.         client.run();
  142.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement