Sockets UDP服务器,每次循环后输出加倍
我有一个运行良好的UDP服务器/客户端程序 我将从一个客户机接收到的消息发送回所有连接的客户机,但每次循环后输出加倍。 我想我必须用客户机重置ListArray,但是只有第一条消息才会被发送 有人知道吗?谢谢 UDP服务器Sockets UDP服务器,每次循环后输出加倍,sockets,tcp,server,network-programming,udp,Sockets,Tcp,Server,Network Programming,Udp,我有一个运行良好的UDP服务器/客户端程序 我将从一个客户机接收到的消息发送回所有连接的客户机,但每次循环后输出加倍。 我想我必须用客户机重置ListArray,但是只有第一条消息才会被发送 有人知道吗?谢谢 UDP服务器 //Listening on Port 12222 int servPort = 12222; UdpClient client = null; //Create a new ListArray for the connected Clie
//Listening on Port 12222
int servPort = 12222;
UdpClient client = null;
//Create a new ListArray for the connected Clients
ArrayList IPArray = new ArrayList();
try
{
//Create an instance of UdpClient
client = new UdpClient(servPort);
}
catch (SocketException se)
{
Console.WriteLine(se.ErrorCode + ": " + se.Message);
Environment.Exit(se.ErrorCode);
}
//Create an new IPEndPoint
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
//Endless loop
for (; ; )
{
try
{
//Receive a byte array with contents
byte[] byteBuffer = client.Receive(ref remoteIPEndPoint);
//Message from Client
string returnData = Encoding.ASCII.GetString(byteBuffer);
//Add connected Client IPs to ListArray
IPArray.Add(remoteIPEndPoint);
//Send the received Message back to all Clients in the ArrayList
for (int i = 0; i < IPArray.Count; i++)
{
Console.WriteLine("Handling client at " + IPArray[i] + " - " + returnData + " arraylist.Length " + IPArray.Count + "\n");
Byte[] sendBytes = Encoding.ASCII.GetBytes("Clients Send " + returnData + "\n");
client.Send(sendBytes, sendBytes.Length, (IPEndPoint)IPArray[i]);
}
Console.WriteLine("echoed {0} bytes.", byteBuffer.Length);
}
catch (SocketException se)
{
Console.WriteLine(se.ErrorCode + ": " + se.Message);
}
}
}
UDP服务器:
具有iP地址的阵列会随着来自
客户端,但我只想要客户端发送的最后一条消息
如果这是需求,那么就不需要循环。此外,在没有arraylist帮助的情况下,您可以使用remoteIPEndPoint本身显示各个客户端的详细信息。这是做你想做的事的更整洁的方式
从UDP服务器中删除以下行:
...
//Send the received Message back to all Clients in the ArrayList
for (int i = 0; i < IPArray.Count; i++)
{
Console.WriteLine("Handling client at " + IPArray[i] + " - " + returnData + " arraylist.Length " + IPArray.Count + "\n");
Byte[] sendBytes = Encoding.ASCII.GetBytes("Clients Send " + returnData + "\n");
client.Send(sendBytes, sendBytes.Length, (IPEndPoint)IPArray[i]);
}
...
它所做的是显示向服务器发送消息的客户端的IP地址和端口号\
此外,对于网络/套接字编程来说,当前的方式并不是更好的方式。您应该始终使用单独的线程(使用ConnectionQueue将它们限制在池中)来接收每个客户端的消息。使用ConnectionQueue来维护线程顺序(可以对每个新的客户端请求重复使用)。这是预期的,因为代码正在执行cmd上显示的操作!它将继续接收每个客户端响应的字节。因此,随着列表中项目数量的增加,服务器将多次响应处理客户端。。。消息你的问题不清楚除了这个你想要什么?对不起,我的英语不好。iP地址的数组随着来自客户端的每条消息而增长,但我只想要客户端发送的最后一条消息。
...
//Send the received Message back to all Clients in the ArrayList
for (int i = 0; i < IPArray.Count; i++)
{
Console.WriteLine("Handling client at " + IPArray[i] + " - " + returnData + " arraylist.Length " + IPArray.Count + "\n");
Byte[] sendBytes = Encoding.ASCII.GetBytes("Clients Send " + returnData + "\n");
client.Send(sendBytes, sendBytes.Length, (IPEndPoint)IPArray[i]);
}
...
//1. Now, you can show the count, but, you can get the indiviudal client detail
//using the remoteIPEndPoint, there is no need of iterating through the array.
Console.WriteLine("This message was sent from " + remoteIPEndPoint.Address.ToString() +
" on their port number " +
remoteIPEndPoint.Port.ToString() + " - " + returnData + "\n");
//2. Then send the data back to the individual client without using ArrayList,
// by passing the client's IPEndPoint in the Send() method.
Byte[] sendBytes = Encoding.ASCII.GetBytes("Clients Send " + returnData + "\n");
client.Send(sendBytes, sendBytes.Length, remoteIPEndPoint);