473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2589
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Gabriele Farina | last post by:
Hi, I'd like to manage multiple socket connection without having to use Threads. I'd like to manage every connection when it is returned from socket_Accept(), without having to wait for the...
8
by: Brad Tilley | last post by:
I have a function that starts a socket server looping continuously listening for connections. It works. Clients can connect to it and send data. The problem is this: I want to get the data that...
9
by: Phil Jenson | last post by:
I am try to evaluate the most efficient method of handling thousands of simultaneous TCP connects each of which remain connected to the server for hours and pass a small amount of data usually once...
5
by: Russell Warren | last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it a cross-process/system setting or does it stay local in the application in which it is called? I've been testing this and...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
13
by: coloradowebdev | last post by:
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via TCP. the remoting site works like a champ. my...
4
by: Ryan Liu | last post by:
TcpClient has a method called GetworkStream GetStream(); So in other words, there is only one stream associate with it for input and output, right? So while it is receiving, it can not send, and...
5
by: Bruce | last post by:
Hello I am building a C# app that creates anywhere from 10 to 100 connections to a specified server and sends 1000s of TCP requests and processes the responses. (it is a stress tool) I planned...
0
by: Jean-Paul Calderone | last post by:
On Sat, 23 Aug 2008 02:25:17 +0800, Leo Jay <python.leojay@gmail.comwrote: No - it's just what I said. create_socket creates one socket and passes it to read_socket and write_socket. ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.