服务器是这样的,在内网,但是绑定了域名,接收端口号5555的消息,通过路由器设置转发即可。客户端发送的消息,绑定了服务器域名和端口,然后发送消息确实服务器可以收到,
但是服务器向消息发来的端口回发消息时,路由器应该能识别消息转发给那个客户,可是客户用什么办法都收不到消息,接受任意IP,任意端口的也收不到。客户端接收任意IP端口消息的核心代码是这样的, udpclient upd = new udpclient();
IPEndPoint ipoint = new IPEndPoint(IPAddress.Any,0);
try
{
while (true)
{
byte[] content = udp.Receive(ref ipoint);
string mes = Encoding.UTF8.GetString(content);
MessageBox.Show(mes);
}
}服务器接收并回发的代码是这样的: udpc = new UdpClient(port);
IPEndPoint ipoint = new IPEndPoint(IPAddress.Any, port);
try
{
while (true)
{
byte[] content = udpc.Receive(ref ipoint);
string mes = Encoding.UTF8.GetString(content);
WriteMessageDelegate delegate0 = new WriteMessageDelegate(WriteMessage);//绑定委托的方法
this.Invoke(delegate0, "收到来自:" + ipoint.ToString() + "消息\n" + "消息内容:" + mes);//调用方法
UdpClient reply = new UdpClient();
content = Encoding.UTF8.GetBytes("服务器回复:消息已收到!");
reply.Send(content, content.Length, ipoint);
reply.Close();
}
}
但是服务器向消息发来的端口回发消息时,路由器应该能识别消息转发给那个客户,可是客户用什么办法都收不到消息,接受任意IP,任意端口的也收不到。客户端接收任意IP端口消息的核心代码是这样的, udpclient upd = new udpclient();
IPEndPoint ipoint = new IPEndPoint(IPAddress.Any,0);
try
{
while (true)
{
byte[] content = udp.Receive(ref ipoint);
string mes = Encoding.UTF8.GetString(content);
MessageBox.Show(mes);
}
}服务器接收并回发的代码是这样的: udpc = new UdpClient(port);
IPEndPoint ipoint = new IPEndPoint(IPAddress.Any, port);
try
{
while (true)
{
byte[] content = udpc.Receive(ref ipoint);
string mes = Encoding.UTF8.GetString(content);
WriteMessageDelegate delegate0 = new WriteMessageDelegate(WriteMessage);//绑定委托的方法
this.Invoke(delegate0, "收到来自:" + ipoint.ToString() + "消息\n" + "消息内容:" + mes);//调用方法
UdpClient reply = new UdpClient();
content = Encoding.UTF8.GetBytes("服务器回复:消息已收到!");
reply.Send(content, content.Length, ipoint);
reply.Close();
}
}
reply.Send(content, content.Length, ipoint);
byte[] content = reply.Receive(ref remotepoint);
这可能算是客户端。客户端跟服务器程序怎么颠倒概念呢?