473,378 Members | 1,106 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,378 software developers and data experts.

How to read and write the same socket in different threads?

I'd like to read and write the same socket in different threads.
one thread is only used to read from the socket, and the other is only
used to write to the socket.
But I always get a 10022 'Invalid argument' exception. Anyone knows why?

I'm using windows xp.

my source code is here:
http://pastebin.com/m23e633a2

and the stack trace is:
C:\WINDOWS\system32\cmd.exe /c python "main.py"
Exception in thread Thread-1:
Traceback (most recent call last):
File "c:\Python25\lib\threading.py", line 486, in __bootstrap_inner
self.run()
File "c:\Python25\lib\threading.py", line 446, in run
self.__target(*self.__args, **self.__kwargs)
File "main.py", line 7, in read_socket
sock.connect(('127.0.0.1', remoteport))
File "<string>", line 1, in connect
error: (10022, 'Invalid argument')

Exception in thread Thread-3:
Traceback (most recent call last):
File "c:\Python25\lib\threading.py", line 486, in __bootstrap_inner
self.run()
File "c:\Python25\lib\threading.py", line 446, in run
self.__target(*self.__args, **self.__kwargs)
File "main.py", line 7, in read_socket
sock.connect(('127.0.0.1', remoteport))
File "<string>", line 1, in connect
error: (10022, 'Invalid argument')
If i replace the 'args' in line 39 with '(socket(), remoteport)', which means
use a different socket to connect to the remote port, the program works.
But if the first element of the tuple is 's', the program doesn't work.

Is it possible to read and write the same socket in different threads?

Thanks in advance.

--
Best Regards,
Leo Jay
Aug 22 '08 #1
0 2195

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jim | last post by:
Hi, I am trying to figure out a way to implement a timeout along with a read() call on an open file. It only has to work on linux, for now I am trying: ret = select.select( ,,, timeout ) if...
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: Jonas Hei | last post by:
Is it safe to call socket.BeginSendTo and socket.BeginSendFrom on a single instance of Socket from two different threads running simultaneously? This is required because I need to listen on a...
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...
5
by: Steven Blair | last post by:
I need to write an ASP.NET application which can do the following: 1. Create a socket which will stay alive and continously read data. 2. The data read needs to be displayed on the webpage. 3....
18
by: hg | last post by:
Hi, I am looking for the most efficient / cleanest way to implement a socket read with timeout (Windows mainly but would be great if the same code worked under *nix) Tanks, hg
3
by: Ryan Liu | last post by:
Will TcpClient.GetStream().Read()/ReadByte() block until at least one byte of data can be read? In a Client/Server application, what does it mean at the end of stream/no more data available? ...
2
by: Zytan | last post by:
I just had the problem occur again, with NetworkStream.Write() doing its thing with a timeout... and it just sits and waits and waits and waits... it never times outs. So, I shut the server down...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.