Saturday, January 11, 2014

Client Server programming in Java [Simple]

//Simple server in java
import java.io.*;
import java.net.*;

public class SimpleServer{
static int reqno=0;
public static void main(String arg[])throws Exception{

ServerSocket s =  new ServerSocket(6666);

     while(true){

Socket s1 = s.accept(); //accepting the client socket
System.out.println("Request No is"+(reqno++));
PrintWriter  pr  = new PrintWriter(s1.getOutputStream());
pr.write("Hello client how are u?\n if ok need not replay\n bye");
pr.close();
s1.close();
     }

}
}


//simple client program
import java.net.*;
import java.io.*;
public class SimpleClient{
public static void main(String arg[])throws Exception{
       
Socket s1= new Socket("localhost",6666);
InputStreamReader isr = new InputStreamReader(s1.getInputStream());
BufferedReader br = new BufferedReader(isr);
                 String line;
while((line=br.readLine())!=null){
System.out.println("Message  :"+line+"\n");
}

br.close();
s1.close();


}
}

Post a Comment