简体   繁体   中英

Java Socket not Receiving connection

So I have made several similar simple Java servers and clients. They work fine when I run them from Eclipse or as far I can say from same computer.

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.

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. Eclipse is verified in Firewall though.

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

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. When I looked at my network properties I found my real address and when I used that one everything worked fine...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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