UdpClient in two threads
HI,
I'm trying to use the same UdpClient instance in two different threads. The first is continuously blocking on a Receive() method while the other uses the Send() method to send data to another host.
The problem is that I can't use Receive() and Send() methods simultaneously in different threads. When I try to Send(), the thread that is block on Receive() throws a SocketException saying that an existing connection was forced to close by remote host.
Any help?
Thanks,
Spulit
[494 byte] By [
Spulit] at [2007-12-31]
I gave up using UdpClient and started using Sockets but I came up with the same problem. I have a like this (simplified version):
class SocketRW{
public:
SocketRW()
{
// Initialize socket;
};
send(){
socket->BeginSendTo(message, 0, message->Length, SocketFlags::None, endpoint, gcnew AsyncCallback(&sendCallback), nullptr);
};
receive(){
socket->BeginReceive(buffer, 0, buffer->Length, SocketFlags::None, gcnew AsyncCallback(&receiveCallback), nullptr);
}
};
private:
static void sendCallback(IAsyncResult ^ar){
socket->EndSendTo(ar);
};
static void receiveCallback(IAsyncResult ^ar){
socket->EndReceive(ar);
socket->BeginReceive(buffer, 0, buffer->Length,
SocketFlags::None, gcnew AsyncCallback(&receiveCallback), nullptr);
};
static Socket ^socket;
};
Now, if I call SocketRW::receive(), it will be receiving data on socket correctly, but as soon as I call SocketRW::send(), the program in fact sends that message but immediately crashes with the error I mentioned in the first post.
Am I missing something?
Thanks,
Spulit