套接字连接通过LAN而不是WAN(互联网)工作

时间:2013-08-07 11:19:40

标签: java sockets port

我有一个问题,我确信这很简单,但我不知道答案。

我创建了一个聊天程序,允许两个用户直接相互连接,前提是他们知道另一个用户的端口和IP。

插槽

try {
        if (IP != null && checkIP(IP)) {
            connection = new Socket(IP, port);
            out = new PrintWriter(connection.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            System.out.println("CONNECTED");

            printTimestamp();
            StyledDocument doc = mainText.getStyledDocument();
            Style style = mainText.addStyle("connection", null);
            StyleConstants.setForeground(style, Color.ORANGE);
            try {
                doc.insertString(doc.getLength(), "Connection open to:  " + IP + "\n", style);
            } catch (Exception e) {
                e.printStackTrace();
            }
            ListenServer.stop();

            System.out.println("Making thread");
            runnable2 = new InputListener();
            thread2 = new Thread(runnable2);
            thread2.start();
        }
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: " + IP);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: " + IP);
    }

的ServerSocket

try {
        serverSocket = new ServerSocket(ClientWindow.port);
        Settings.work = true;
        serverSocket.setSoTimeout(3000);
    } catch (IOException e) {
        System.err.println("Could not listen on port: " + ClientWindow.port);
        Settings.work = false;
    }

    while (running) {
        try {
            connection1 = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed port: " + ClientWindow.port);
        } catch(NullPointerException ne){
            JOptionPane.showMessageDialog(ClientWindow.window,
            "There was a fatal error in startup.\nPlease close ALL instances of Chattable before attempting to open the program again.",
            "Fatal Error",
            JOptionPane.ERROR_MESSAGE);
            running = false;
        }

        try {
            if (connection1 != null) {
                out = new PrintWriter(connection1.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(connection1.getInputStream()));
                System.out.println("Accepted over server");
                runnable2 = new InputListener();
                thread2 = new Thread(runnable2);
                thread2.start();
                running = false;

                StyledDocument doc = mainText.getStyledDocument();
                Style style = mainText.addStyle("connection", null);
                StyleConstants.setForeground(style, Color.ORANGE);
                try {
                    doc.insertString(doc.getLength(), "[ " + getCurrentTimeStamp() + " ] Connection open to:  " + connection1.getRemoteSocketAddress() + "\n", style);
                } catch (Exception e) {}
            }
        } catch (IOException e) {
            System.err.println("Couldn't get I/O.");
        }
    }

此系统在LAN上运行良好,但是当您尝试通过Internet使用它时,它会从Socket代码中抛出IOException。这是使用TCP还是UDP?这对我想做什么意味着什么。有没有人有任何关于如何使这个工作的提示?这是港口没有开放的问题吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

这是使用TCP还是UDP?

TCP

这对我想做的事情意味着什么。有没有人有关于如何使其发挥作用的任何提示?

见下文。

这是端口未打开的问题吗?

最有可能,但您应该能够从IOException获取更多详细信息以确认原因。

在这个问题中,我已经回答了有关您可能觉得有用的端口转发的详细信息; C++ Sockets with Router

您可能还需要配置防火墙以允许端口外部访问,具体取决于您的环境。