简体   繁体   English

Java套接字未接收连接

[英]Java Socket not Receiving connection

So I have made several similar simple Java servers and clients. 因此,我做了几个类似的简单Java服务器和客户端。 They work fine when I run them from Eclipse or as far I can say from same computer. 当我从Eclipse或从同一台计算机运行时,它们运行良好。

The moment I send client program to other computer, they can't bind. 当我将客户端程序发送到其他计算机时,它们无法绑定。

As well, I made Swift app for iOS and tried to reach the server from phone but to no avail. 同样,我为iOS开发了Swift应用程序,并尝试通过电话访问服务器,但无济于事。

Now, this is my sample code, it's just bare bones, and it still doesn't work. 现在,这是我的示例代码,仅是简单的操作,仍然无法使用。 Is there some problem with problem with Firewall that I need to address? 我需要解决的防火墙问题是否存在某些问题? I run MacBookPro 2010, OS Sierra 10.12 beta. 我运行MacBookPro 2010,OS Sierra 10.12 beta。 Eclipse is verified in Firewall though. 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());
            }
        }
    }
}

this one works from Eclipse 这是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);
        } 

    }

}

and this is swift code: 这是快速的代码:

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

    }

}

Any ideas? 有任何想法吗?

Thank you. 谢谢。

So the problem was that I was getting bad IP address from Google and other web services. 所以问题是我从Google和其他Web服务获取了错误的IP地址。 When I looked at my network properties I found my real address and when I used that one everything worked fine... 当我查看我的网络属性时,我找到了我的真实地址,当我使用该地址时,一切都很好...

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

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