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.javaimport 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) {
3 回答
HUH函数
TA贡献1836条经验 获得超4个赞
Socket clientSocket = new Socket("localhost", 5000);
Socket clientSocket = new Socket(serverName, 5000);
ServerSocket
添加回答
举报
0/150
提交
取消