471,599 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

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

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
2 2507
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
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.

Similar topics

7 posts views Thread by Gabriele Farina | last post: by
8 posts views Thread by Brad Tilley | last post: by
9 posts views Thread by Phil Jenson | last post: by
5 posts views Thread by Russell Warren | last post: by
2 posts views Thread by Stressed Out Developer | last post: by
13 posts views Thread by coloradowebdev | last post: by
reply views Thread by Jean-Paul Calderone | last post: by
2 posts views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.