无法在java中的2台PC套接字之间将客户端连接到服务器
Posted
技术标签:
【中文标题】无法在java中的2台PC套接字之间将客户端连接到服务器【英文标题】:Can't connect client to server between 2 PCs socket in java 【发布时间】:2022-01-14 12:08:45 【问题描述】:我有一个问题,当我在同一台 PC 上创建 2 个文件(Client.java 和 Server.java)时,它可以工作。但是当我将 Client.java 文件发送到另一台 PC 时,它不起作用。我也关闭了防火墙,但它仍然不起作用。
类 Sever.java:
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
public static void main(String[] args) throws IOException
try
System.out.println("loading..");
ServerSocket serverSocket = new ServerSocket(6667);
Socket socket = serverSocket.accept();
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
String str = dataInputStream.readUTF();
System.out.println(str);
dataInputStream.close();
socket.close();
System.out.println("end");
catch (IOException e1)
e1.printStackTrace();
类Client.java:
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client
public static void main(String[] args)
try
System.out.println("client connecting...");
byte[] addr = (byte) 192, (byte) 168, (byte) 9, (byte) 9;
InetAddress inetAddress = InetAddress.getByAddress("lvh", addr);
Socket socket = new Socket(inetAddress, 6667);
//socket.setSoTimeout(999999999);
System.out.println(socket.isConnected());
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeUTF("hello");
dataOutputStream.flush();
dataOutputStream.close();
socket.close();
System.out.println("end");
catch (UnknownHostException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
我将类 Client.java 发送到其他 PC 并收到错误“连接超时”,如下图 (T_T): enter image description here
我不知道如何解决 :((请帮忙!!!
【问题讨论】:
【参考方案1】:在打开DataInputStream
之前,需要在两端打开DataOutputStream
。
更新:添加代码
服务器.java
package src;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
public static void main(String[] args) throws IOException
try
System.out.println("loading..");
ServerSocket serverSocket = new ServerSocket(6667);
try (Socket socket = serverSocket.accept())
try (DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()))
try (DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()))
String str = dataInputStream.readUTF();
System.out.println(str);
System.out.println("end");
catch (IOException e1)
e1.printStackTrace();
客户端.java
package src;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
public class Client
public static void main(String[] args)
try
System.out.println("client connecting...");
byte[] addr = (byte) 127, (byte) 0, (byte) 0, (byte) 1;
InetAddress inetAddress = InetAddress.getByAddress("localhost", addr);
try (Socket socket = new Socket(inetAddress, 6667))
//socket.setSoTimeout(999999999);
System.out.println(socket.isConnected());
try (DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()))
try (DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()))
dataOutputStream.writeUTF("hello");
System.out.println("end");
catch (IOException e)
e.printStackTrace();
服务器输出
loading..
hello
end
客户端输出:
client connecting...
true
end
希望这会有所帮助。 :)
【讨论】:
以上是关于无法在java中的2台PC套接字之间将客户端连接到服务器的主要内容,如果未能解决你的问题,请参考以下文章
当我将多个客户端同时连接到 Java 服务器时,套接字写入错误
如何将 VB.net 程序连接到 access 数据库并在 2 台不同的 PC 上运行它。