2013-03-09 56 views
2

我想在C#中創建一個非常簡單的客戶端/服務器,基本上我現在要做的就是獲取客戶端連接到服務器,這是我得到的地方錯誤。我的服務器啓動正常,但是當我試着開始我的客戶,我得到這個錯誤:簡單的C#客戶端使用套接字錯誤

The requested address is not valid in this context 
    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0 
    at Client.Client.Main (System.String[] args) [0x00000] in <filename unknown>:0 

的基本客戶端我現在所擁有的去如下

 try 
     { 
      IPAddress ipAddress = IPAddress.Parse("0.0.0.0"); 
      IPEndPoint remoteEP = new IPEndPoint(ipAddress, 8001); 

      Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 

      try 
      { 
       sender.Connect(remoteEP);      

       Console.WriteLine("Connected to: " + remoteEP); 

       byte[] msg = Encoding.ASCII.GetBytes("Testing"); 

       sender.Send(msg); 


       sender.Shutdown(SocketShutdown.Both); 
       sender.Close(); 

      } 

      catch (Exception e) 
      { 
       Console.WriteLine(e.Message + "\n" + e.StackTrace); 
      } 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.Message + "\n" + e.StackTrace); 
     } 

回答

5

這不是一個有效的IP地址。對於本地主機(你自己的機器),這是你想要的

IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); 
+0

謝謝!我不知道我是如何錯過的...... – user1875195 2013-03-09 04:55:46

相關問題