Java - Can't Connect With ServerSocket

2019-06-05 21:14发布

问题:

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

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

回答1:

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



回答2:

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