繁体   English   中英

Java套接字未接收连接

[英]Java Socket not Receiving connection

因此,我做了几个类似的简单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")

    }

}

有任何想法吗?

谢谢。

所以问题是我从Google和其他Web服务获取了错误的IP地址。 当我查看我的网络属性时,我找到了我的真实地址,当我使用该地址时,一切都很好...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM