By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,694 Members | 1,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,694 IT Pros & Developers. It's quick & easy.

Socket.BeginSendTo and Socket.BeginSendFrom on a single Socket instancefrom multiple threads

P: n/a
Is it safe to call socket.BeginSendTo and socket.BeginSendFrom on a
single instance of Socket from two different threads running
simultaneously?

This is required because I need to listen on a certain URI [IP:Port] and
I need to send outgoing messages from that local socket [IP:Port]

something like this:
public class Communicator {
Socket skt = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp );
private Thread listenerThread;
private Thread sendThread;
public Communicator() { }
public void Start() {
listenerThread = new Thread(new ThreadStart(Listen));
sendThread = new Thread(new ThreadStart(Send));
listenerThread.Start();
sendThread.Start();
}
private void Listen() {
while(!bStop) {
skt.BeginReceiveFrom(..);
//wait for AsyncCallback to be called
//using ManualResetEvent for this
}
}
private void Send() {
while(!bStop) {
//send all queued messages
skt.BeginSendTo(..);
//wait for AsyncCallback to be called - that would send an event
//using ManualResetEvent for this
}
}
public void Stop() {
bStop = true
}
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jonas,

The documentation for the Socket class indicate that instances of the
Socket class are thread-safe, so I would say yes, it is safe.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jonas Hei" <j_*******@hotmail.com> wrote in message
news:42**********************@news.free.fr...
Is it safe to call socket.BeginSendTo and socket.BeginSendFrom on a single
instance of Socket from two different threads running simultaneously?

This is required because I need to listen on a certain URI [IP:Port] and I
need to send outgoing messages from that local socket [IP:Port]

something like this:
public class Communicator {
Socket skt = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp );
private Thread listenerThread;
private Thread sendThread;
public Communicator() { }
public void Start() {
listenerThread = new Thread(new ThreadStart(Listen));
sendThread = new Thread(new ThreadStart(Send));
listenerThread.Start();
sendThread.Start();
}
private void Listen() {
while(!bStop) {
skt.BeginReceiveFrom(..);
//wait for AsyncCallback to be called
//using ManualResetEvent for this
}
}
private void Send() {
while(!bStop) {
//send all queued messages
skt.BeginSendTo(..);
//wait for AsyncCallback to be called - that would send an event
//using ManualResetEvent for this
}
}
public void Stop() {
bStop = true
}

Nov 17 '05 #2

P: n/a
Nicholas,

The documentation says "Any public static members of this type are
thread safe. Any instance members are not guaranteed to be thread safe."

So it doesn't really specifically guarentee thread safety of BeginSendTo
and BeginReceiveFrom.

Any ideas or recommendations?
Nicholas Paldino [.NET/C# MVP] wrote:
Jonas,

The documentation for the Socket class indicate that instances of the
Socket class are thread-safe, so I would say yes, it is safe.

Hope this helps.

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.