Write a program in java to send the message client to server?

Server

import java.io.*;

import java.net.*;

public class Server

{

      public static void main(String arg[])

      {

            try

            {

                  System.out.println("Running Server");

                  ServerSocket ss=new ServerSocket(7777);

                  Socket s=ss.accept();

                  DataInputStream dis=new DataInputStream(s.getInputStream());

                  String str=(String)dis.readUTF();

                  System.out.println("Message = "+str);

                  ss.close();

            }

            catch(Exception e)

            {

                  System.out.println(e);

            }

      }

}

Server.PNG

Client

import java.io.*;

import java.net.*;

public class Client

{

      public static void main(String arg[])

      {

            try

            {

                  Socket s=new Socket("localhost",7777);

                  DataOutputStream dout=new DataOutputStream(s.getOutputStream());

                  dout.writeUTF("Hello Server How Are You?");

                  dout.flush();

                  dout.close();

            }

            catch(Exception e)

            {

                  System.out.println(e);

            }

      }

}

Client.PNG

, , , , , , ,

Leave a Reply