如在其他SO問題中所建議的,我遵循本教程https://www.youtube.com/watch?v=ObcGBT4ZWEU 這適用於局域網,但是當我嘗試通過從任何Web服務獲取的IP嘗試Internet時,使用端口80,它給了意大利的部分爲「地址請求非在自己的範圍內有效」的形象 c#TCP/IP通信錯誤:請求的地址在自己的上下文中無效
錯誤(我不知道這是否會爲這個錯誤英文文本) 錯誤出來了「開始「按鈕(按鈕1)。代碼:
private void button1_Click(object sender, EventArgs e)
{
try
{
epLocal = new IPEndPoint(IPAddress.Parse(textLocalIp.Text), Convert.ToInt32(textLocalPort.Text));
sck.Bind(epLocal);
epRemote = new IPEndPoint(IPAddress.Parse(textFriendsIp.Text), Convert.ToInt32(textFriendsPort.Text));
sck.Connect(epRemote);
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
button1.Text = "Connected";
button1.Enabled = false;
button2.Enabled = true;
textMessage.Focus();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
插座變量聲明爲全局:
Socket sck;
EndPoint epLocal, epRemote;
和初始化:
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
任何建議?謝謝!!
編輯:我successfull得到englsh錯誤! 在意大利versione我的精確太行,它是:
sck.Bind(epLocal);
你有使用的有效IP嗎?如果它在本地局域網上工作,聽起來你可能會使用錯誤的IP地址。你是在公司環境中還是在家中連接?如果在家中,您可能需要使用http://whatsmyip.org/查看本地IP,因爲很可能您的路由器正在進行NAT轉換。這也可能意味着遠程IP不在該端口上偵聽。您可以嘗試執行telnet並查看您是否連接。什麼是拋出異常? –
Mike
也可以看看這裏http://bytes.com/topic/c-sharp/answers/233487-tcpclient-exception-requested-address-not-valid-its-context – Mike
轉到https://www.grc。 com/shieldsup然後按繼續,向下滾動到可以選擇端口掃描的地方「在那裏你可以看到你是互聯網提供商的IP」,並掃描你的計算機的開放端口,如果需要的話。 – Creator