Java Blog

 
 
 
// Server

import java.io.*;
import java.net.*;

public class EServer
{
 public static void main(String[] args) throws IOException 
 {

  ServerSocket S = new ServerSocket(3000);

  while(true)
  {
   Socket Client = S.accept();

   InputStream in = Client.getInputStream();

   DataInputStream Dis = new DataInputStream(in);

   System.out.println(Dis.readUTF());

   Client = new Socket(\"localhost\",4000);

  BufferedReader buff = new BufferedReader(new InputStreamReader (System.in));

   String Str = buff.readLine();

   OutputStream out = Client.getOutputStream();

   DataOutputStream Dos = new DataOutputStream(out);

   Str = \"Server Says :: \" + Str;

   Dos.writeUTF(Str);

   Client.close();

  }

 }

}


// Client 

import java.io.*;
import java.net.*;
import java.util.*;

public class EClient
{
 public static void main(String[] args) throws IOException 
 {
  Socket C = new Socket(\"localhost\",3000);
  BufferedReader buff = new BufferedReader(new InputStreamReader (System.in));
  String Str = buff.readLine();

  OutputStream out = C.getOutputStream();

  DataOutputStream Dos = new DataOutputStream(out);

  Dos.writeUTF(\"Client Say :: \" + Str);
  Dos.flush();

  ServerSocket S = new ServerSocket(4000);
  Socket Client = S.accept();
  InputStream in = Client.getInputStream();
  DataInputStream Dis = new DataInputStream(in);
  System.out.println(Dis.readUTF());
  Client.close();
 }
}



OUTPUT :

=========  Client  ============

C:\\jdk1.1.3\\bin>javac EClient.java

C:\\jdk1.1.3\\bin>java EClient
hi, how are you Server?
Server Says :: Fine, Thankyou. Bye.



=========  Server =============
C:\\jdk1.1.3\\bin>javac EServer.java

C:\\jdk1.1.3\\bin>java EServer
Client Say :: hi, how are you Server?
Fine, Thankyou. Bye.


*************************************************************************/

 
 
Didn't find what you were looking for? Find more on Corba program to Write a Echo server and client with UDP server and client