Java - 无法与 ServerSocket 连接

发布于 2024-12-31 21:35:37 字数 3222 浏览 1 评论 0原文

我尝试使用 ServerSocket 端口 2649,其他人无法连​​接。它与本地主机一起工作得很好。这是人们在尝试连接时遇到的错误:

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)

我已转发端口,并且我的计算机上没有活动的防火墙。这是我在端口转发时使用的设置。

https://i.sstatic.net/dqZqd.png

https://i.sstatic.net/cho8K.png

当我在 canyouseeme.org 上检查端口 2649 时,它说连接超时。

我也在使用Windows XP。任何帮助表示赞赏。

谢谢

编辑:这是我正在使用的代码

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");
    }

I am trying to use ServerSocket with port 2649, and other people cannot connect. It works fine with localhost. This is the error people get when trying to connect:

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)

I have port forwarded, and I do not have a firewall active on my computer. Here are the settings I used when port forwarding.

https://i.sstatic.net/dqZqd.png

https://i.sstatic.net/cho8K.png

When I check port 2649 on canyouseeme.org, it says the connection timed out.

I am using Windows XP too. Any help is appreciated.

Thanks

EDIT: Here is the code I am using

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");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尴尬癌患者 2025-01-07 21:35:37

如果不是防火墙的话。确保将服务器套接字绑定到 0.0.0.0 而不是 localhost。
尝试调用 server.bind(new InetSocketAddress("0.0.0.0", port));

if it's not the firewall. make sure you bind the server socket to 0.0.0.0 and not to localhost.
try calling server.bind(new InetSocketAddress("0.0.0.0", port));

嗳卜坏 2025-01-07 21:35:37

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

"Connection timed out" -> a firewall discards packets. Most likely Windows Firewall - try disabling it and see if they can connect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文