A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
UDP protocol could be missing data packet. If you send mass data with UDP, client was able to handle the data and there is no sequencing of data in UDP and no retransmission of lost packets. If you require to guaranteed the data completion , I suggest you can use TCP protocol.
ConcurrentQueue is thread-safe collection. It provided the multiple threads operation. So I thinks this issue is not related with ConcurrentQueue.
I checked your project, to improve the data efficient, we could set the the connections number using socket.Listen(100) . Also you can try TCP protocol to check data transfer issue, as the sample below.
class Program
{
static Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private static byte[] result = new byte[1024];
static void Main(string[] args)
{
SocketServie();
}
public static void SocketServie()
{
Console.WriteLine("Server start");
string host = ConfigurationManager.AppSettings["HostIP"];
int port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
socket.Bind(new IPEndPoint(IPAddress.Parse(host), port));
socket.Listen(100);
Thread myThread = new Thread(ListenClientConnect);
myThread.Start();
Console.ReadLine();
}
private static void ListenClientConnect()
{
while (true)
{
Socket clientSocket = socket.Accept();
clientSocket.Send(Encoding.UTF8.GetBytes("from server"));
Thread receiveThread = new Thread(ReceiveMessage);
receiveThread.Start(clientSocket);
}
}
private static void ReceiveMessage(object clientSocket)
{
Socket myClientSocket = (Socket)clientSocket;
while (true)
{
try
{
int receiveNumber = myClientSocket.Receive(result);
if (receiveNumber == 0)
return;
Console.WriteLine("received client {0} message:{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.UTF8.GetString(result, 0, receiveNumber));
string sendStr = "received cliented send msg ";
byte[] bs = Encoding.UTF8.GetBytes(sendStr);
myClientSocket.Send(bs, bs.Length, 0);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
myClientSocket.Close();
myClientSocket.Shutdown(SocketShutdown.Both);
break;
}
}
}
}