Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / May 2005

Tip: Looking for answers? Try searching our database.

Threading with Socket

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cool Guy - 23 May 2005 12:51 GMT
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.?
Cool Guy - 23 May 2005 19:09 GMT
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?
[quoted text clipped - 4 lines]
>
> Is this thread-safe, etc.?

*cross-posting to the more populated C# language group, in hope of a
response from there*
Altramagnus - 24 May 2005 10:40 GMT
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.

>I wrote:
>
[quoted text clipped - 10 lines]
> *cross-posting to the more populated C# language group, in hope of a
> response from there*
Cool Guy - 24 May 2005 14:57 GMT
> 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.
Vince P - 24 May 2005 20:24 GMT
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

>> 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.
Markus Stoeger - 24 May 2005 20:52 GMT
>> 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
Cool Guy - 24 May 2005 23:15 GMT
> 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.
Feroze [msft] - 25 May 2005 01:05 GMT
Yes, you should be able to have a read and write operation outstanding on
the socket at the same time.

Signature

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

>I have wrote software that do that b4 in both java and C#.
>
[quoted text clipped - 24 lines]
>> *cross-posting to the more populated C# language group, in hope of a
>> response from there*
Cool Guy - 24 May 2005 23:13 GMT
> Is it possible for one thread to perform a blocking read on a socket while
> another thread performs a blocking write on it?
[quoted text clipped - 4 lines]
>
> 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();
 }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.