2013-11-09 117 views
2
private void btnStart_Click(object sender, EventArgs e) 
     { 

try 
      { 
       epLocal = new IPEndPoint(IPAddress.Parse(txtIP1.Text), Convert.ToInt32(txtPort1.Text)); 
       sck.Bind(epLocal); 
       epRemote = new IPEndPoint(IPAddress.Parse(txtIP2.Text), Convert.ToInt32(txtPort2.Text)); 
       //Here Error Occures I dont know what is my mistake? 
       sck.Bind(epRemote); 
       byte[] buffer = new byte[1500]; 
       sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer); 
       btnStart.Text = "Connected"; 
       btnStart.Enabled = false; 
       btnSend.Enabled = true; 
       txtMessage.Focus(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 

回答

2

要綁定同一個插座,以多發端點的對象,這就是爲什麼你得到一個InvalidArgument異常,您可以在一個套接字綁定到只有一個IPEndPoint對象。

當您嘗試監聽公共IP地址和本地IP,請試試這個

IPEndPoint ep = new IPEndPoint(IPAddress.Any, yourportnumber); 
sck.Bind(ep) ; 

,這將更好地創建您的PC上的所有IP地址聽監聽器, 否則你使用一個單獨的套接字對象

如果我是你,我就不會從textbox解析本地IPAddress代替我會用像IPAddress.Loopback

+0

謝謝,在這裏,作爲基層即時連接我自己的IP爲txtIP1和txtIP2:192.168.0.102,端口分別爲91和92。端口可用性由netstat -a命令檢查,它們可用,我很困惑,接下來要嘗試什麼。 – User23289

+0

sck是Socket還是TCPSocket? –

+0

我已經更新了我的回答,這個很好的幫助你 –

相關問題