【问题标题】:Connecting two computers for client server communication in java在java中连接两台计算机以进行客户端服务器通信
【发布时间】:2015-02-03 20:57:15
【问题描述】:

这段代码是关于java中的客户端和服务器通信的。我可以在我的电脑上运行这两个代码,并且可以连接客户端和服务器。但是我将如何连接两台计算机作为客户端和服务器。以下是我的服务器和客户端代码:

我的服务器1

//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"));
    }
}

我的客户1

//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(); 
    }
}

【问题讨论】:

  • 第 1 步:格式化您的代码以便我们阅读。第 2 步:了解主机名。
  • 首先,客户端应该为你的服务器的IP地址创建一个套接字,而不是它自己的客户端(localhost)

标签: java client-server


【解决方案1】:

客户端只需要服务器的IP。
你必须找出服务器的IP并告诉客户端,比如:

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

服务器无需更改。

【讨论】:

  • WAN ip 还是 LAN ip?
【解决方案2】:

你只需要在创建Socket Instance时输入服务器的ip。 我建议你按照步骤进行

1) 在您要用作服务器的任何一台计算机上启动热点 2)在第二台电脑上启动wifi并连接我们刚刚启动的热点。 3) 现在进入共享中心并点击你连接的网络并检查详细信息并复制 dns 服务器 ip 并将其粘贴到客户端程序中

【讨论】:

  • 您应该在答案中添加一些代码以增加答案的稳健性。有关制定答案的更多帮助,请查看this link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多