PC Review


Reply
Thread Tools Rate Thread

Threading with Socket

 
 
Cool Guy
Guest
Posts: n/a
 
      23rd May 2005
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.?
 
Reply With Quote
 
 
 
 
Cool Guy
Guest
Posts: n/a
 
      23rd May 2005
I wrote:

> 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*
 
Reply With Quote
 
Altramagnus
Guest
Posts: n/a
 
      24th May 2005
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.



"Cool Guy" <(E-Mail Removed)> wrote in message
news:1v6neunzyn6lw$.(E-Mail Removed)...
>I wrote:
>
>> 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*



 
Reply With Quote
 
Cool Guy
Guest
Posts: n/a
 
      24th May 2005
Altramagnus <(E-Mail Removed)> wrote:

> 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.
 
Reply With Quote
 
Vince P
Guest
Posts: n/a
 
      24th May 2005
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 Guy" <(E-Mail Removed)> wrote in message
news:mjwpi9ru6vq4$.(E-Mail Removed)...
> Altramagnus <(E-Mail Removed)> wrote:
>
>> 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.



 
Reply With Quote
 
Markus Stoeger
Guest
Posts: n/a
 
      24th May 2005
Cool Guy wrote:

> Altramagnus <(E-Mail Removed)> wrote:
>
>> 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.


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

 
Reply With Quote
 
Cool Guy
Guest
Posts: n/a
 
      25th May 2005
Cool Guy <(E-Mail Removed)> wrote:

> 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();
}
}
 
Reply With Quote
 
Cool Guy
Guest
Posts: n/a
 
      25th May 2005
Markus Stoeger <(E-Mail Removed)> wrote:

> 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.
 
Reply With Quote
 
Feroze [msft]
Guest
Posts: n/a
 
      25th May 2005
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.
----------------

"Altramagnus" <(E-Mail Removed)> wrote in message
news:4292f73f$(E-Mail Removed)...
>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.
>
>
>
> "Cool Guy" <(E-Mail Removed)> wrote in message
> news:1v6neunzyn6lw$.(E-Mail Removed)...
>>I wrote:
>>
>>> 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*

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Threading with Socket Cool Guy Microsoft C# .NET 5 25th May 2005 02:05 AM
Socket and multi-threading Mahesh Devjibhai Dhola [MVP] Microsoft Dot NET Framework 6 10th May 2005 03:33 AM
Re: Socket and multi-threading Ioannis Vranos Microsoft VB .NET 0 9th May 2005 02:13 PM
Re: Socket and multi-threading Cor Ligthert Microsoft VB .NET 1 9th May 2005 10:07 AM
Socket Threading Komkrich Jaikaew Microsoft VB .NET 0 29th Jul 2004 01:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:30 PM.