Client server callback of message

//code for client

public class MyClient {
 
 
    public static void main(String[] args) throws IOException {
     
        Socket obj = new Socket("localhost",8040);
        DataOutputStream dout = new DataOutputStream(obj.getOutputStream());
        dout.writeUTF("This message is from client side");
        dout.flush();
     
     
    }
}





// code for server

public class MyServer {
    public static void main(String[] args) throws IOException {
     
        ServerSocket obj1 = new ServerSocket(8040);
        Socket obj = obj1.accept();
        DataInputStream din = new DataInputStream(obj.getInputStream());
        String str = (String)din.readUTF();
        System.out.println("The message from client is  " +str);

     
    }
 
 
 
}

output (in server console)
This message is from client side



//here first we have to run server code then client code ..
//the message sent by the client will be readed in server concole
// the message are delivered through port no which is listened at sockets














No comments:

Post a Comment