UDP 客户端服务器在同一台计算机上工作,但不能通过 LAN
我有一个简单的应用程序,它将连续的 UDP 数据包从服务器发送到客户端。如果服务器应用程序和客户端应用程序位于同一台计算机上,则一切正常,但当我将客户端移动到 LAN 中的另一台计算机时,它会停止处理消息 “所请求的地址在其上下文中无效”。我真的不明白为什么会发生这种情况。 这是服务器代码:
public void SendData()
{
udpClient = new UdpClient();
for (int i = 0; i < 8192; i++)
{
dataBytes[i] = Convert.ToByte(Convert.ToInt16(dataValues[i]));
}
udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
try
{
udpClient.Connect(ipAddress, port);
udpClient.Send(dataBytes, dataBytes.Length);
}
catch (Exception ex)
{
}
}
在客户端计算机上,这是代码:
private void receive()
{
try
{
udpClient = new UdpClient();
udpClient.Client.ReceiveTimeout = 1000;
udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
int port = Convert.ToInt32("13064");
IPAddress ipAddress = IPAddress.Parse("192.168.1.107");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
socket = new UdpClient(localEndPoint);
udpClient.Connect(ipAddress, port);
socket.BeginReceive(new AsyncCallback(ReceivedDataCallback), socket);
}
catch (Exception ex)
{
if (!socketConnected)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ReceivedDataCallback(IAsyncResult AR)
{
try
{
socketConnected = true;
clientSocket = AR.AsyncState as UdpClient;
var port = Convert.ToInt32("13064");
IPAddress ipAddress = IPAddress.Parse("192.168.1.107");
IPEndPoint source = new IPEndPoint(ipAddress, port);
nums = clientSocket.EndReceive(AR, ref source);
OutputRemoteSamples();
clientSocket.BeginReceive(new AsyncCallback(ReceivedDataCallback), clientSocket);
}
catch (Exception ex)
{
}
}
感谢您的任何建议 汤姆
I have a simple application that sends continuous UDP packets from a server to a client. Everything works well if the server app and client app are on the same computer but the minute I move the client to another computer in the LAN it stops working with a message
"the requested address is not valid in its context". I really don't understand why this is happening.
Here is the server code:
public void SendData()
{
udpClient = new UdpClient();
for (int i = 0; i < 8192; i++)
{
dataBytes[i] = Convert.ToByte(Convert.ToInt16(dataValues[i]));
}
udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
try
{
udpClient.Connect(ipAddress, port);
udpClient.Send(dataBytes, dataBytes.Length);
}
catch (Exception ex)
{
}
}
On the client machine here is the code:
private void receive()
{
try
{
udpClient = new UdpClient();
udpClient.Client.ReceiveTimeout = 1000;
udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
int port = Convert.ToInt32("13064");
IPAddress ipAddress = IPAddress.Parse("192.168.1.107");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
socket = new UdpClient(localEndPoint);
udpClient.Connect(ipAddress, port);
socket.BeginReceive(new AsyncCallback(ReceivedDataCallback), socket);
}
catch (Exception ex)
{
if (!socketConnected)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ReceivedDataCallback(IAsyncResult AR)
{
try
{
socketConnected = true;
clientSocket = AR.AsyncState as UdpClient;
var port = Convert.ToInt32("13064");
IPAddress ipAddress = IPAddress.Parse("192.168.1.107");
IPEndPoint source = new IPEndPoint(ipAddress, port);
nums = clientSocket.EndReceive(AR, ref source);
OutputRemoteSamples();
clientSocket.BeginReceive(new AsyncCallback(ReceivedDataCallback), clientSocket);
}
catch (Exception ex)
{
}
}
Thanks for any suggestions
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论