Connecting Tech Pros Worldwide Forums | Help | Site Map

Can two threads use same socket?

Member
 
Join Date: Apr 2008
Posts: 55
#1: Sep 4 '08
I'm explain my problem..
Expand|Select|Wrap|Line Numbers
  1. sockWriter=new Sender();
  2. sockWriter->client =new TcpClient();
  3. sockWriter->client->Connect(this->hostName,this->port_number);   
  4. sockWriter->clientStream=sockWriter->client->GetStream();
  5. localSocketWriter=new Sender(this->sockWriter->clientStream);
  6.  
  7. Thread *thread=new Thread(new ThreadStart(this,&platocomm::Listener));
  8.             thread->Name =S"Listener";
  9.             thread->Start();
  10.  
here is the sender class members
Expand|Select|Wrap|Line Numbers
  1.                                                 BinaryWriter *writer;
  2.             TcpClient *client;
  3.             NetworkStream *clientStream;
  4.             static StreamWriter *fileWriter;
  5.             Sender(NetworkStream *clientStream);
  6.  
now i use the Listener to read data from the transport connection. My question is, can i use another thread , to read the data from the same trasport connection. When i tried this, it shows "a socket can not be accessed in a way forbidden by its access permissions". how to make this possible...plz suggest some solution for this..

Expert
 
Join Date: Jan 2008
Location: York
Posts: 179
#2: Sep 4 '08

re: Can two threads use same socket?


I've got a similar example for you below demonstrating how to go about it. Obviously however you'll not be using Binary Readers/Writers, and it looks like you're coding in C++, but it should get you there...

Expand|Select|Wrap|Line Numbers
  1. namespace Server  
  2.  {  
  3.     public class Server : System.Windows.Forms.Form  
  4.     {  
  5.        private Socket connection;  
  6.        private Thread readThread;  
  7.        private NetworkStream socketStream;  
  8.        private BinaryWriter writer;  
  9.        private BinaryReader reader;  
  10.  
  11.        public Server()  
  12.        {  
  13.           ...  
  14.  
  15.           // create a new thread from the server  
  16.           readThread = new Thread( new ThreadStart( Run ) );  
  17.           readThread.Start();  
  18.        }  
  19.  
  20.        // allows a client to connect and displays the text it sends  
  21.        public void Run()  
  22.        {  
  23.           TcpListener listener;  
  24.  
  25.           try  
  26.           {  
  27.              // Create TcpListener and wait for connection  
  28.              listener = new TcpListener( 5000 );  
  29.              listener.Start();  
  30.  
  31.              while ( true )  
  32.              {  
  33.                 // Accept an incoming connection  
  34.                 connection = listener.AcceptSocket();  
  35.  
  36.                 // create NetworkStream object associated with socket  
  37.                 // and writers/reader for data transmission  
  38.                 socketStream = new NetworkStream( connection );  
  39.                 writer = new BinaryWriter( socketStream );  
  40.                 reader = new BinaryReader( socketStream );  
  41.  
  42.                 // inform client that connection was successfull  
  43.                 writer.Write( "SERVER>>> Connection successful" );  
  44.  
  45.                 string theReply = "";  
  46.  
  47.                 // Read String data sent from client  
  48.                 do  
  49.                 {  
  50.                    try  
  51.                    {     
  52.                       theReply = reader.ReadString();  
  53.                    }  
  54.                    catch ( Exception )  
  55.                    {  
  56.                       break;  
  57.                    }  
  58.  
  59.                 } while ( theReply != "CLIENT>>> TERMINATE" && connection.Connected );  
  60.  
  61.                 // Close connection  
  62.                 writer.Close();  
  63.                 reader.Close();  
  64.                 socketStream.Close();  
  65.                 connection.Close();  
  66.              }  
  67.           }  
  68.           catch ( Exception error )  
  69.           {  
  70.              MessageBox.Show( error.ToString() );  
  71.           }  
  72.  
  73.        }  
  74.     }  
Reply