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

Can two threads use same socket?

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..
Sep 4 '08 #1
1 1649
IanWright
179 100+
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.     }  
Sep 4 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This...
6
by: Zunbeltz Izaola | last post by:
Hi, I have the following problem. I'm developing a GUI program (wxPython). This program has to comunicate (TCP) whit other program that controls a laboratory machine to do a measurement. I...
5
by: Russell Warren | last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it a cross-process/system setting or does it stay local in the application in which it is called? I've been testing this and...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
2
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send...
5
by: PH | last post by:
Hi guys; I got a single processor computer, running an application that launches 2 threads. Each of these threads listens for incoming connections in a specific port, so there is a Loop ....
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...
2
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are...
0
by: Qui Sum | last post by:
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not. The class I write has the following structure: public class IPScanner { public...
0
by: Jean-Paul Calderone | last post by:
On Sat, 23 Aug 2008 02:25:17 +0800, Leo Jay <python.leojay@gmail.comwrote: No - it's just what I said. create_socket creates one socket and passes it to read_socket and write_socket. ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.