Java-无法与ServerSocket连接 [英] Java - Can't Connect With ServerSocket

查看:94
本文介绍了Java-无法与ServerSocket连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过端口2649使用ServerSocket,并且其他人无法连​​接.它与localhost正常工作.这是人们尝试连接时遇到的错误:

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.

http://i.imgur.com/NLdaA.png

http://i.imgur.com/FJpJQ.png

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

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

我也在使用Windows XP.感谢您的帮助.

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

谢谢

这是我正在使用的代码

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

推荐答案

(如果它不是防火墙).确保将服务器套接字绑定到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));

这篇关于Java-无法与ServerSocket连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆