Threading with Socket

  • Thread starter Thread starter Cool Guy
  • Start date Start date
C

Cool Guy

Is it possible for one thread to perform a blocking read on a socket while
another thread performs a blocking write on it?

I'm about to start writing an IRC client and I'm considering having a
thread in which all the reading from the socket takes place, and another
thread in which all the writing to the socket takes place.

Is this thread-safe, etc.?
 
I said:
Is it possible for one thread to perform a blocking read on a socket while
another thread performs a blocking write on it?

I'm about to start writing an IRC client and I'm considering having a
thread in which all the reading from the socket takes place, and another
thread in which all the writing to the socket takes place.

Is this thread-safe, etc.?

*cross-posting to the more populated C# language group, in hope of a
response from there*
 
I have wrote software that do that b4 in both java and C#.

For java, socket is not thread safe. When 1 thread tries to read from a
socket ( in block mode ), while the oother thread
tries to write to it, sometimes it will coz error. ( For UDP, did not try
for TCP )

For C#, according to the MSDN doc, it is not meant to be thread safe,
however,
so far i have not encounter any error. ( For TCP, did not try for UDP )

Conclusion I got is, no, socket is not thread safe.
But for C#, since i have not encounter any problem, I took the risk.
 
Altramagnus said:
But for C#, since i have not encounter any problem, I took the risk.

Thanks for the reply. I'll have to wait on a definite answer on this
before I start this project, though.
 
This is really intersting,.. I too am writing an IRC Client in C#.

I'm pretty much finished but came to the group here because i get periodic
blocking in the UI thread and I came to find out what sort of threading
issues i might be having.

So i'm going to make sure my socket threads are being marshed properly.

vince

vincepac
at
hotmail
dot
com
 
Cool said:
Thanks for the reply. I'll have to wait on a definite answer on this
before I start this project, though.

by far not a definite answer, but thats what I do:

two threads, one for sending, one for receiving. both use sock.Poll(...) to
wait until they can read or write. once Poll returns true I use lock() to
lock the socket and call the send/receive functions inside the lock. this
way I can be sure that the send/receive functions 1) do not block and 2) do
not get called at the same time.

I just hope that Poll is thread safe (MSDN docs say it's not guaranteed to
be.. like pretty much everything in .net). So far it seems to work without
problems.

Max
 
Cool Guy said:
Is it possible for one thread to perform a blocking read on a socket while
another thread performs a blocking write on it?

I'm about to start writing an IRC client and I'm considering having a
thread in which all the reading from the socket takes place, and another
thread in which all the writing to the socket takes place.

Is this thread-safe, etc.?

Well, I did a Google search and a Google Groups search, and it seems that
there are quite a few people who think this is okay (in Winsock, at least).

Also, the program listed at the end of this post works as expected.

So I'm going to do this and cross my fingers.

Program:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;

class Test {
static Socket socket;

[STAThread]
static void Main(string[] args) {
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
socket.Connect(new IPEndPoint(Dns.Resolve("google.com").AddressList[0],
80));

new Thread(new ThreadStart(Receive)).Start();

Thread.Sleep(1000);

string httpRequest = "GET /non-existant-URI HTTP/1.0\r\n\r\n";
socket.Send(Encoding.ASCII.GetBytes(httpRequest));

Console.Write("Request sent.\n\n");
Console.Read();
}

static void Receive() {
Console.Write("Ready to receive.\n\n");

byte[] buffer = new byte[99999];
socket.Receive(buffer);

Console.WriteLine(
"Reponse received!\n\n" +
"First few characters:\n\n" +
"{0}",
Encoding.ASCII.GetString(buffer).Substring(0, 100));

socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
 
Markus Stoeger said:
two threads, one for sending, one for receiving. both use sock.Poll(...) to
wait until they can read or write.

In my case, I'm gonna do this without using Socket.Poll, since I'm gonna be
passing Streams to the reading/writing methods (mainly in order to make
them easier to unit test).

I'm crossing my fingers!

Thanks, all.
 
Yes, you should be able to have a read and write operation outstanding on
the socket at the same time.

--
feroze

-----------------
This posting is provided as-is. It offers no warranties and assigns no
rights.

See http://weblogs.asp.net/feroze_daud for System.Net related posts.
----------------
 
Back
Top