在java中连接两台计算机进行客户端服务器通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28308584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting two computers for client server communication in java
提问by Pr4njal
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:
这段代码是关于java中的客户端和服务器通信。我可以在我的 PC 上运行这两个代码,并且可以连接客户端和服务器。但是我将如何连接 2 台计算机作为客户端和服务器。这是我的服务器和客户端代码如下:
MyServer1
我的服务器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"));
}
}
MyClient1
我的客户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();
}
}
采纳答案by user2831683
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:
客户端只需要服务器的IP。
您必须找出服务器的 IP 并将其告诉客户端,例如:
String serverName = "IP of server comes here"; // Indicating the place to put Server's IP
s = new Socket(serverName, 10);
Server needs no change.
服务器无需更改。
回答by MAULIK RAMANI
You have to just enter ip of the server while creating Socket Instance. i suggest you to follow steps
您只需在创建Socket Instance时输入服务器的 ip 即可。我建议你按照步骤
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
1)在您将用作服务器的任何一台计算机上启动热点 2)在第二台计算机上启动wifi并连接我们刚刚启动的热点。3)现在到共享中心并点击您连接的网络并检查详细信息并复制dns服务器ip并将其粘贴到客户端程序中