Posted By:
21 Feb, 2014 9:51 am PST

Programs to send data from Server to Client

Develop a code to create a server to send data to client.

//to create server to send data to client



public class SendData {

        public static void main(String[] args) throws Exception {

                            //To create a server socket with some port number

              ServerSocket a = new ServerSocket(250);

                            //the server should wait till the client accepts connection

              Socket s= a.accept();

              System.out.println("Connection with client system  established successfully");

 //Attach output stream to server socket which is used by socket to send data to client

              OutputStream p=s.getOutputStream();

              //attach print stream to send data till socket

              PrintStream pr = new PrintStream(p);

              //send data to client

              String g= "Data successfully transmitted to Client";


              pr.println("Hello, How are you doing?");

           //close the connection by closing all the streams and sockets at server side






Note: Compile this program but do not run this program until a client is also created

Write a code to create client side program to receive data from server.

//to create client to receive data from server



public class ReceiveData {

       public static void main(String []args) throws Exception


             //to create a socket on client side with some port number

              Socket cs=new Socket("",250);

              //to attach input stream to socket to receive data from server

              InputStream i= cs.getInputStream();

              //to read data from socket into client

              BufferedReader r= new BufferedReader(new InputStreamReader(i));

              //read data

              String h;









Note: After compiling the above 2 programs (SendData & ReceiveData), run these two programs in two separate dos-windows.



Description: : Provides the classes for implementing networking applications.

Serversocket class: This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester.

Socket : This class implements client sockets .


Was it useful?  

Please login to reply to this problem.