473,406 Members | 2,356 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,406 software developers and data experts.

Socket and multi-threading

Hi,

Socket class documentation says that it is not thread safe. We understand
that if do simultaneous sends on ONE socket then it will be a problem (or
simultaneous receive). Can we create TWO threads on OUR OWN such that one
will do SEND and one will do RECEIVE using the SAME socket reference? This
means that two threads WILL BE in the same SOCKET object - one doing receive
and one doing send. We know we can use asynchronous calls on the socket to
achieve this but whether we can do this using our own threads. Any pointers
to official documentation that shows that this is permissible?

Thanks in advance,
Regards,
Mahesh


Nov 21 '05 #1
4 1274
Mahesh,

I know that this subject (and like this) is often been in the newsgroups

Maybe you find something in these threads before somebody else answers you.
http://groups-beta.google.com/group/...rch+this+group

I hope this helps,

Cor
Nov 21 '05 #2
www.vbip.com

Cor Ligthert wrote:
Mahesh,

I know that this subject (and like this) is often been in the newsgroups

Maybe you find something in these threads before somebody else answers you.
http://groups-beta.google.com/group/...rch+this+group

I hope this helps,

Cor

Nov 21 '05 #3
Mahesh Devjibhai Dhola [MVP] wrote:
Hi,

Socket class documentation says that it is not thread safe. We understand
that if do simultaneous sends on ONE socket then it will be a problem (or
simultaneous receive). Can we create TWO threads on OUR OWN such that one
will do SEND and one will do RECEIVE using the SAME socket reference? This
means that two threads WILL BE in the same SOCKET object - one doing receive
and one doing send. We know we can use asynchronous calls on the socket to
achieve this but whether we can do this using our own threads. Any pointers
to official documentation that shows that this is permissible?

If you perform synchronized use of the socket by using thread locks (that is perform a
sending operation with one thread, after the other thread has finished reading), then I
suppose it is OK. However why are you using the Socket class itself for this?
In a book I am currently reading about .NET networking, stream socket communication is
performed in the style:

(get the connection Socket)
get a NetworkStream/create a NetworkStream with the Socket object
create a BinaryWriter and a BinaryReader with the NetworkStream

use the BinaryReader and BinaryWriter for network I/O

Call methods Close() of BinaryReader, BinaryWriter, NetworkStream, (Socket) in turn.
Nov 21 '05 #4
You should be able to use a Monitor to lock one thread (Monitor.Enter(Me) and
Monitor.Wait(Me)) to ensure the second thread is finished (use
Monitor.Enter(Me) and Monitor.Pulse(Me) and Monitor.Exit(Me) in the second
class.

"Mahesh Devjibhai Dhola [MVP]" wrote:
Hi,

Socket class documentation says that it is not thread safe. We understand
that if do simultaneous sends on ONE socket then it will be a problem (or
simultaneous receive). Can we create TWO threads on OUR OWN such that one
will do SEND and one will do RECEIVE using the SAME socket reference? This
means that two threads WILL BE in the same SOCKET object - one doing receive
and one doing send. We know we can use asynchronous calls on the socket to
achieve this but whether we can do this using our own threads. Any pointers
to official documentation that shows that this is permissible?

Thanks in advance,
Regards,
Mahesh


Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: 0k | last post by:
Hi everyone, I am trying to write a small app that sends multicast udp packets using a socket object. I have more than one NIC on my PC and the following code works OK only if I disable all the...
4
by: Alexander Muylaert | last post by:
Hi I their a way I can interupt socket.Receive. I want my multi-threader server to be able to handle requests as wel as Broadcast messages. This broadcasting is done when the socket is not...
5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
3
by: Winston Nimchan | last post by:
Hi: I would like to build a socket server application that is user configurable using XML (app.config) and would like some guidance. I already have a single socket server working but would...
6
by: Sharon | last post by:
Hi all. I'm trying first time async socket connection. In all the examples i've seen, the server connection is closed when the message is complete. Is it common to close the connection after...
14
by: DaTurk | last post by:
I am makeing a Multicast server client setup and was wondering what the difference is between Socket.Connect, and Socket.Bind. It may be a stupid question, but I was just curious. Because I...
0
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made...
6
by: roblugt | last post by:
I have what I imagine is a well-known .Net networking problem, but even though I've Googled for some time I've not yet come across a thread where this has been fully explained... There is a...
16
by: =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?= | last post by:
hi all! I got a problem. I declared a SOCKET var in my C program but when i compiled the program it displayed like *--------------------------------------------------------------* *'SOCKET':...
1
by: hankypan1 | last post by:
Hi All, I am developing a multi threaded CLI application which will be running over the socket interface. So i need to have a telnet server sort of thing, command line editor application with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.