簡體   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