I want to rephrase my previous question Should I replace all my "new Thread" with "Task.Factory.StartNew"?
I need to listen for several udp sockets
................
s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
........
public void Receive()
{
byte[] buffer = new byte[65536];
while (true)
{
try
{
count = s.Receive(buffer);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return;
}
if (count > 0)
{
OnNewDatagram(new NewDatagramEventArgs(buffer, count));
}
}
}
So I need to launch several "Receive()" methods (one for each socket). Now I'm using one Thread for one socket:
socketA = new...
....
threadA = new Thread(Receive);
.....
threadA.Start();
socketB = new...
....
threadB = new Thread(Receive);
.....
threadB.Start();
What should I use:
- one thread for one socket
- one thread for all sockets(how?)
- one task for all sockets
- one task for one socket
- other?
upd I have only 2-3 sockets, so it's probably better to use one-socket-one-thread model instead of Socket.Select... or one-socket-one-task model?