Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-Can';无法连接服务器套接字_Java_Sockets_Port - Fatal编程技术网

Java-Can';无法连接服务器套接字

Java-Can';无法连接服务器套接字,java,sockets,port,Java,Sockets,Port,我正在尝试使用端口为2649的ServerSocket,其他人无法连接。它可以与localhost配合使用。这是人们在尝试连接时遇到的错误: Exception in thread "main" java.net.ConnectException: Connection timed out: connect at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSo

我正在尝试使用端口为2649的ServerSocket,其他人无法连接。它可以与localhost配合使用。这是人们在尝试连接时遇到的错误:

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
 at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
 at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
 at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
 at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
 at java.net.PlainSocketImpl.connect(Unknown Source)
 at java.net.SocksSocketImpl.connect(Unknown Source)
 at java.net.Socket.connect(Unknown Source)
 at java.net.Socket.connect(Unknown Source)
 at java.net.Socket.<init>(Unknown Source)
 at java.net.Socket.<init>(Unknown Source)
 at Client.main(Client.java:11)

如果不是防火墙。确保将服务器套接字绑定到0.0.0.0,而不是本地主机。
尝试调用server.bind(新的InetSocketAddress(“0.0.0.0”,port))

“连接超时”->防火墙丢弃数据包。很可能是Windows防火墙-尝试禁用它,看看它们是否可以连接。

你能发布你正在使用的代码吗?这些设置是在路由器上还是在电缆/dsl调制解调器上,还是两者都有?@HunterMcMillen添加了代码。你的服务器套接字正在监听端口2649,但你的套接字代码正在连接端口5555。@PaulTomblin抱歉,它实际上是2649端口。我的朋友有客户端代码,正在运行它以尝试连接,他已将其更改为2649。绑定服务器套接字。我没有安装任何防火墙。您的服务器和客户端之间有任何网络设备吗?从客户端到服务器的“跟踪器”是什么样子的?我发现了问题。我的端口无法打开。我真的不知道为什么。我什么都试过了…这很令人沮丧。“不会打开”的确切意思是什么?与打开它们相反。当我使用端口检查器时,它会显示它们未打开。
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static void main(String[] args)throws Exception {
        System.out.println("Starting...");
        File file = new File("C:/Testing.txt");
        InputStream in = new FileInputStream(file);
        ServerSocket server = new ServerSocket(2649);
        System.out.println("Ready for connection");
        Socket socket = server.accept();
        OutputStream output = socket.getOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(output);
        out.writeObject("C:/Testing.txt");
        byte[] buffer = new byte[socket.getSendBufferSize()];
        int bytesReceived = 0;
        while ((bytesReceived = in.read(buffer)) > 0) {
            output.write(buffer, 0, bytesReceived);
        }
        out.flush();
        out.close();
        in.close();
        server.close();
        socket.close();
        output.flush();
        output.close();
        System.out.println("Finished");
    }

}

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.Socket;

public class Client {

    public static void main(String[] args) throws Exception {
        System.out.println("Starting...");
        Socket socket = new Socket("IP ADDRESS", 2649);
        InputStream input = socket.getInputStream();
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
        FileOutputStream out = new FileOutputStream(new File((String) in.readObject()));
        byte[] buffer = new byte[socket.getReceiveBufferSize()];
        int bytesReceived = 0;
        while ((bytesReceived = input.read(buffer)) > 0) {
            out.write(buffer, 0, bytesReceived);
        }
        in.close();
        out.close();
        input.close();
        socket.close();
        System.out.println("Finished");
    }