Java Socket没有接收连接

时间:2017-01-30 01:29:19

标签: java ios swift sockets

所以我制作了几个类似的简单Java服务器和客户端。当我从Eclipse运行它们或者我可以从同一台计算机上说它们时,它们工作正常。

当我将客户端程序发送到其他计算机时,它们无法绑定。

同样,我为iOS制作了Swift应用程序并尝试通过手机访问服务器,但无济于事。

现在,这是我的示例代码,它只是简单的骨头,它仍然无法正常工作。我需要解决防火墙问题吗?我运行MacBookPro 2010,OS Sierra 10.12 beta。虽然在防火墙中验证了Eclipse。

public class BasicServer {

    public static void main(String[] args) {

        ServerSocket sam = null;
        Socket bob = null;
        try {
            sam = new ServerSocket(1025);
            System.out.println("Server is Cheking acceptance");
            bob = sam.accept();
            if (bob != null) {
                System.out.println("\nSystem Accepted");
            }
            System.out.println("\nClosing Server...");
        } catch (Exception e) {
            System.out.println("Error on server side of type: " + e.getMessage());

        } finally {
            try {
            sam.close();
            bob.close();
            } catch (Exception e) {
                System.out.println("Socket on Finally had encountered an Error:" + e.getLocalizedMessage());
            }
        }
    }
}

这个适用于Eclipse

public class TransServer {

    public static void main(String[] args)  {
        System.out.println("Action Done");


        try {

            Socket sam = new Socket("some IPv6", 1025);
            System.out.println("Done...");
            if (sam.isConnected()) {
                System.out.print("Connected!");
            }

            sam.close();

        } catch (IOException e) {
            System.out.println("Error Occoured: " + e);
        } 

    }

}

这是快速的代码:

class ViewController: UIViewController {

    private let serverIP = "some IPv6"

    @IBOutlet weak var textOutlet: UITextView!

    @IBAction func connectAction(_ sender: UIButton) {
        // textOutlet.title = ""
        connectToServer()
    }

    private func connectToServer() {
        let mySocket = TCPClient(address: serverIP, port: 1025)

        let connectResult = mySocket.connect(timeout: 2)

        switch connectResult {
        case .failure(let e):
            textOutlet.text = "We received an error: \(e.localizedDescription)"
        case .success:
            textOutlet.text = "We have successfuly established a Connection to server!"
        }

        mySocket.close()

        textOutlet.text.append("\nWe have closed the Socket")

    }

}

有什么想法吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

所以问题是我从谷歌和其他网络服务获得了糟糕的IP地址。当我查看我的网络属性时,我找到了我的真实地址,当我使用那个时,一切都运行正常......