我正在尝试实现 TCP 连接,从服务器端看一切正常,但是当我运行客户端程序(从客户端计算机)时出现以下错误:java.net.ConnectException:
我正在尝试实现 TCP 连接,从服务器端看一切正常,但是当我运行客户端程序(从客户端计算机)时出现以下错误:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at TCPClient.main(TCPClient.java:13)
我尝试更改套接字号码(以防它正在使用中),但无济于事,有人知道是什么导致了这个错误以及如何修复它吗?
服务器代码:
//TCPServer.java
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception {
String fromclient;
String toclient;
ServerSocket Server = new ServerSocket(5000);
System.out.println("TCPServer Waiting for client on port 5000");
while (true) {
Socket connected = Server.accept();
System.out.println(" THE CLIENT" + " " + connected.getInetAddress()
+ ":" + connected.getPort() + " IS CONNECTED ");
BufferedReader inFromUser = new BufferedReader(
new InputStreamReader(System.in));
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connected.getInputStream()));
PrintWriter outToClient = new PrintWriter(
connected.getOutputStream(), true);
while (true) {
System.out.println("SEND(Type Q or q to Quit):");
toclient = inFromUser.readLine();
if (toclient.equals("q") || toclient.equals("Q")) {
outToClient.println(toclient);
connected.close();
break;
} else {
outToClient.println(toclient);
}
fromclient = inFromClient.readLine();
if (fromclient.equals("q") || fromclient.equals("Q")) {
connected.close();
break;
} else {
System.out.println("RECIEVED:" + fromclient);
}
}
}
}
}
客户端代码:
//TCPClient.java
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception {
String FromServer;
String ToServer;
Socket clientSocket = new Socket("localhost", 5000);
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
System.in));
PrintWriter outToServer = new PrintWriter(
clientSocket.getOutputStream(), true);
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
while (true) {
FromServer = inFromServer.readLine();
if (FromServer.equals("q") || FromServer.equals("Q")) {
clientSocket.close();
break;
} else {
System.out.println("RECIEVED:" + FromServer);
System.out.println("SEND(Type Q or q to Quit):");
ToServer = inFromUser.readLine();
if (ToServer.equals("Q") || ToServer.equals("q")) {
outToServer.println(ToServer);
clientSocket.close();
break;
} else {
outToServer.println(ToServer);
}
}
}
}
}
我想补充一点,这是 我的经验 -
\'我在 localhost ,并尝试 通过指定正确的 URL(例如) Android 模拟器 http://localhost/my_api/login.php
。但我收到了 连接被拒绝错误 \'
需要注意的是 - 当我在电脑上打开浏览器并使用相同的 URL 时( http://localhost/my_api/login.php
) 我得到了正确的响应
所以在我的案例中,问题 localhost
替换了 IP (因为您的服务器托管在您的机器上),这使得它可以从同一台 PC 上的模拟器访问。
要获取本地机器的 IP,你可以 ipconfig
在 cmd 上使用命令,你将获得类似 192.68.xx.yyVoila 的 IPv4。这是你托管服务器的机器的 IP。使用它代替 localhost
http://192.168.72.66/my_api/login.php
注意 - 您无法从此计算机之外的任何节点访问此私有 IP。(如果需要,您可以使用 Ngnix)