2013-11-10 32 views
0

我正在爲通過互聯網連接到它的多個winform客戶端創建一個小型服務器。當它被設置爲本地地址 下面的代碼工作:從C#中的套接字異常修復原因#

服務器:

static void Main(string[] args) 
{ 
    TcpListener serverSocket = new TcpListener(9000); 
    TcpClient clientSocket = default(TcpClient); 
    int counter = 0; 

    serverSocket.Start(); 
    Console.WriteLine("Chat Server Started ...."); 
    counter = 0; 
    while ((true)) 
    { 
     counter += 1; 
     clientSocket = serverSocket.AcceptTcpClient(); 

     byte[] bytesFrom = new byte[10025]; 
     string dataFromClient = null; 

     NetworkStream networkStream = clientSocket.GetStream(); 
     networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize); 
     dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom); 
     dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$")); 

     clientsList.Add(dataFromClient, clientSocket); 

     broadcast(dataFromClient + " Joined ", dataFromClient, false); 

     Console.WriteLine(dataFromClient + " Joined chat room "); 
     handleClinet client = new handleClinet(); 
     client.startClient(clientSocket, dataFromClient, clientsList); 
    } 

    clientSocket.Close(); 
    serverSocket.Stop(); 
    Console.WriteLine("exit"); 
    Console.ReadLine(); 
} 

Winform的客戶:

public partial class Form1 : Form 
{ 
    System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient(); 
    NetworkStream serverStream = default(NetworkStream); 
    string readData = null; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     readData = "Conected to Chat Server ..."; 
     msg(); 
     clientSocket.Connect("127.0.0.1", 9000); 
     serverStream = clientSocket.GetStream(); 

     byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox3.Text + "$"); 
     serverStream.Write(outStream, 0, outStream.Length); 
     serverStream.Flush(); 

     Thread ctThread = new Thread(getMessage); 
     ctThread.Start(); 
    } 
} 

這些代碼片段在網絡上正常工作互相交談。 但是,當我決定這行代碼更改

clientSocket.Connect("127.0.0.1", 9000);clientSocket.Connect("81.62.126.41", 9000);

我的IP地址,它給了我下面的SocketException:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

所以有些事情我已經嘗試。端口轉發端口9000,因爲我在我的路由器後面。爲端口9000創建入站和出站規則。在我的服務器正在運行時,我已經去了http://www.canyouseeme.org/,並且網站實際上可以看到我的服務。

任何人都知道我能做些什麼來解決這個問題?

這是服務器運行時的一張netstat圖。認爲它可能有幫助。 enter image description here

+0

根據您的描述,有些東西阻止IP數據包進出您的主機。嘗試使用Wireshark或Microsoft網絡監視器來查看主機上的數據包,它應該指向數據包被過濾的位置。 – William

+0

@William不知道那是什麼,但我會測試一下。 –

+0

@William我正在使用Wireshark,我已經能夠捕獲我想要的包和信息,但我不知道我想找什麼。 –

回答

0

我有類似的問題。

  1. 如果在代碼中只更改了一個字符串,並且它停止工作,請仔細檢查您正在使用的工具。某些內部代理可能允許/禁止您與內部(127.0.0.1)或外部IP進行通信。
  2. 檢查您的瀏覽器127.0.0.1:9000和81.62.126.41:9000相同的URL。如果它不打開第二種情況,那麼在防火牆中發出或者設置你的服務器以便能夠進行外部訪問,而不是在你的代碼中。
  3. 您的異常意味着您的服務器出現問題:您可以嘗試打開http://81.62.12.32/並打開http://81.62.126.41:9000/,您會看到兩個不同的錯誤。

結論:問題不在您的代碼中。它位於您的Web服務器設置或防火牆設置中。