简体   繁体   中英

Connecting two computers for client server communication in java

This code is about client and server communication in java. I can run both codes in my PC and can connect client and server. But how will I connect 2 computers as a client and server. Here are my codes for server and client as follows:

MyServer1

//code for server
import java.io.*;
import java.net.*;

public class MyServer1
{
    ServerSocket ss;
    Socket s;
    DataInputStream dis;
    DataOutputStream dos;
    public MyServer1()
    {
        try
        {
            System.out.println("Server Started");
            ss=new ServerSocket(10);
            s=ss.accept();
            System.out.println(s);
            System.out.println("CLIENT CONNECTED");
            dis= new DataInputStream(s.getInputStream());
            dos= new DataOutputStream(s.getOutputStream());
            ServerChat();
        }
        catch(Exception e)
        {
             System.out.println(e);
        }
    }

    public static void main (String as[])
    {
         new MyServer1();
    }

    public void ServerChat() throws IOException
    {
         String str, s1;
         do
         {
             str=dis.readUTF();
             System.out.println("Client Message:"+str);
             BufferedReader br=new BufferedReader(new   InputStreamReader(System.in));
             s1=br.readLine();
             dos.writeUTF(s1);
             dos.flush();
         }
         while(!s1.equals("bye"));
    }
}

MyClient1

//code for client
import java.io.*;
import java.net.*;

public class MyClient1
{
    Socket s;
    DataInputStream din;
    DataOutputStream dout;
    public MyClient1()
    {
         try
         {
             //s=new Socket("10.10.0.3,10");
             s=new Socket("localhost",10);
             System.out.println(s);
             din= new DataInputStream(s.getInputStream());
             dout= new DataOutputStream(s.getOutputStream());
             ClientChat();
         }
         catch(Exception e)
         {
             System.out.println(e);
         }
     }
     public void ClientChat() throws IOException
     {
           BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
           String s1;
           do
           {
               s1=br.readLine();
               dout.writeUTF(s1);
               dout.flush();
               System.out.println("Server Message:"+din.readUTF());
           }
           while(!s1.equals("stop"));
    }
    public static void main(String as[])
    {
        new MyClient1(); 
    }
}

Client Just needs the IP of the Server.
You have to find out the IP of the server and tell the client about it, like:

String serverName = "IP of server comes here";  // Indicating the place to put Server's IP
s = new Socket(serverName, 10);

Server needs no change.

You have to just enter ip of the server while creating Socket Instance . i suggest you to follow steps

1) start hotspot in any one computer which you going to use as server 2) in second computer start wifi and connect with hotspot which we just started. 3) now ho to sharing center and click on network you connect and check detail and copy dns server ip and paste it in client program

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM