SocketException:连接重置 - >但只有在通过公共IP访问时

时间:2014-02-06 14:27:48

标签: java windows sockets networking client-server

这是我关于Stack-Overflow的第一个问题,真的希望你可以帮助我;)!

那么问题是什么? 我写了一个小的客户端 - 服务器应用程序,一切都在localhost上运行,但是当我试图通过我的公共ip / ddns域到达服务器时,我得到了一个 java.net.SocketException:Connection reset 错误!! =( 我做了一个SSCCE,其中已经发生了错误! 如果你们能帮助我解决我的问题,我真的很感激:) ...

当然我已经在我的防火墙中打开了特定的端口并从我的路由器转发了端口...... 我尝试使用非活动防火墙,但这对我没有帮助=(

Client_SSCCE:

package server_SSCCE;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class Client_SSCCE {
    private Socket serverConnection = null;
    private ObjectOutputStream streamOut = null;
    private ObjectInputStream streamIn = null;
    private MainClientThread mClT;
        private boolean run_Thread_run = false;
    String serverAdress = "localhost\Public IP";
    int serverPort = 3443;

    public static void main(String[] args) {
        new Client_SSCCE().initConnection();
    }

    public void stopConnection() {
        run_Thread_run = false;
        try {
            if (serverConnection != null && !serverConnection.isClosed()) {
                serverConnection.close();
                System.out.println("CLOSED");
            }
        } catch (IOException ex) {
            System.out.println("stopConnection bug");
        }
        serverConnection = null;
    }

    public void initConnection() {
        stopConnection();
        try {
            System.out.println("Tries to init new Connection(ip: "+serverAdress+" port:"+serverPort+")");
            serverConnection = new Socket(serverAdress,serverPort);
            System.out.println("1");
            streamOut = new ObjectOutputStream(serverConnection.getOutputStream());
            System.out.println("2");
            streamIn = new ObjectInputStream(serverConnection.getInputStream());

            streamOut.writeInt(1234);
            streamOut.flush();
            System.out.println("3");
            run_Thread_run = true;
            mClT = new MainClientThread();
            System.out.println("4");
            mClT.start();
            System.out.println("CONNECTED");
        } catch (SocketTimeoutException ex) {
            ex.printStackTrace();
            System.out.println("Server not responding");
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("initConnection bug");
        }
    }

    public void sendToServer(Object o) {
        try {
            streamOut.writeObject(o);
            streamOut.flush();
        } catch(Exception ex) {
            System.out.println("send bug");
        }
    }
    public boolean isConnected() {
        return run_Thread_run && serverConnection.isConnected();
    }

    private class MainClientThread extends Thread {
        @Override
        public void run() {
            while (run_Thread_run) {
                try {
                    if (! serverConnection.isClosed()) {
                        Object o = streamIn.readObject();
                        System.out.println(o);
                    }
                } catch(Exception ex) {
                    if (serverConnection != null && !serverConnection.isClosed())
                        System.out.println("read bug");
                    stopConnection();
                }
            }
        }
    }
}

Server_SSCCE:

package server_sscce;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Server_SSCCE {

    public static void main(String[] args) {
        new Server_SSCCE().startServer();
    }

    private ServerSocket serverSocket = null;
    MainServerThread checkForNewClients;
        boolean run_Thread_run = false;
    private ArrayList<ClientThread> clients = new ArrayList<>();

    int serverPort = 3443;

    public void startServer() {
        try {
            serverSocket = new ServerSocket(serverPort);
            System.out.println("New Server Socket(port: "+serverPort+") initiated");
        } catch (IOException ex) {
            System.out.println("can't init new ServerSocket(with port: "+serverPort+"): " + ex.getMessage());
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {}
        run_Thread_run = true;
        checkForNewClients = new MainServerThread();
        checkForNewClients.start();
    }


    private class MainServerThread extends Thread {
        @Override
        public void run() {
            Socket newClientConnection = null;
            while (run_Thread_run) {
                try {
                    System.out.println("Server Waiting for new Client's...");
                    if (serverSocket != null && ! serverSocket.isClosed())
                        newClientConnection = serverSocket.accept();
                    System.out.println("New Client tries to connect: " + newClientConnection);
                    ObjectInputStream newClient_InputStream = new ObjectInputStream(newClientConnection.getInputStream());
                    ObjectOutputStream newClient_OutputStream = new ObjectOutputStream(newClientConnection.getOutputStream());
                    System.out.println(newClient_InputStream.readInt());

                    logInClient(newClientConnection, newClient_InputStream, newClient_OutputStream);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        private void logInClient(Socket newClient_Socket, ObjectInputStream newClient_InputStream, ObjectOutputStream newClient_OutputStream) throws IOException {
            ClientThread ct = new ClientThread(newClient_Socket, newClient_InputStream, newClient_OutputStream);
            ct.start();
            System.out.println("Client logged in");
            clients.add(ct);
        }
     }


     private class ClientThread extends Thread {
         private Socket clientConnection;
         private ObjectInputStream inputStream;
         private ObjectOutputStream outputStream;
            private boolean run_Thread_run = true;

         public ClientThread(Socket clientConn, ObjectInputStream inputStream, ObjectOutputStream outputStream) throws IOException {
             clientConnection = clientConn;
             this.inputStream = inputStream;
             this.outputStream = outputStream;
         }

         public void sendToClient(Object o) throws IOException {
             if (! clientConnection.isClosed() && !clientConnection.isOutputShutdown()) {
                 outputStream.writeObject(o);
                 outputStream.flush();
             }
         }

         @Override
         public void run() {
             while(run_Thread_run) {
                 try {
                     if (! clientConnection.isClosed()) {
                         Object o = inputStream.readObject();
                         System.out.println("recieved: "+o);
                     } else {
                        run_Thread_run = false;
                     }
                 } catch (IOException ex) {
                    run_Thread_run = false;
                 } catch (ClassNotFoundException ex) {
                     ex.printStackTrace();
                    run_Thread_run = false;
                 }
             }
         }

         public void close() {
             try {
                 clientConnection.close();
                 inputStream.close();
                 outputStream.close();
             } catch (IOException ex) {
                 ex.printStackTrace();
             }
         }
     }
}

输出:

本地主机:

**Client:**
Tries to init new Connection(ip: localhost port:3443)
1
2
3
4
CONNECTED

**Server:**
New Server Socket(port: 3443) initiated
Server Waiting for new Client's...
New Client tries to connect: Socket[addr=/127.0.0.1,port=1182,localport=3443]
1234
Client logged in
Server Waiting for new Client's...

public ip / ddns domain:

**Client:**
Tries to init new Connection(ip: publicIP port:3443)


**Server:**
New Server Socket(port: 3443) initiated
Server Waiting for new Client's...
New Client tries to connect: Socket[addr=/192.168.1.111,port=1183,localport=3443]
java.net.SocketException: Connection reset
Server Waiting for new Client's...
    at java.net.SocketInputStream.read(SocketInputStream.java:196)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2310)
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2323)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2794)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at server_kskb.Server_KSKB$MainServerThread.run(Server_KSKB.java:54)
New Client tries to connect: Socket[addr=/192.168.1.111,port=1183,localport=3443]
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:196)
Server Waiting for new Client's...
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2310)
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2323)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2794)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at server_kskb.Server_KSKB$MainServerThread.run(Server_KSKB.java:54)

如果我的英语听起来太可怕,我道歉...... :) 希望你能帮到我!

提前致谢!

P.S。:我的电脑使用Windows-XP!

0 个答案:

没有答案