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

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

On Sat, 23 Aug 2008 02:25:17 +0800, Leo Jay <py***********@gmail.comwrote:
>On Sat, Aug 23, 2008 at 1:58 AM, Jean-Paul Calderone <ex*****@divmod.comwrote:
>On Sat, 23 Aug 2008 01:47:23 +0800, Leo Jay <py***********@gmail.comwrote:
>>>
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

You're connecting and accepting with the same socket. That's not a very
good thing to do. You're not even reading and writing on the same socket,
since you're writing to the socket which you get from accept (if the code
could get that far).

not exactly. the socket connecting to port 1 is listening to port 2.
port 1 and port 2 are not the same.
No - it's just what I said. create_socket creates one socket and passes
it to read_socket and write_socket. read_socket calls connect on the
socket it is passed. write_socket calls accept on the socket it is passed.
So a single socket has connect and accept called on it. Now, main does
call create_socket twice, so this does happen to two sockets, but it's
broken in each case.
>
>What are you trying to do? Why are you connecting and accepting? Why do
you need two threads?

I'm migrating a java module to python. In the java code, after
creating a socket,
the code gets an InputStream and an OutputStream from that socket,
sends and receives data in different threads.
So I just want to do the same thing. (at least do the same thing
currently, I don't want to change all related projects at the same
time.)
Two threads per socket is a bad design. :(
>
>Have you seen Twisted? http://twistedmatrix.com/

not yet, but it seems that it's quite a complicated module, isn't it?
is it possible to get the work done without adopting such a monster? :)
It's complicated so that applications using it don't have to be. It's
easier to write network code with Twisted than with the socket and
threading modules.

Jean-Paul
Aug 22 '08 #1
0 2924

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? ...
0
by: Leo Jay | last post by:
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...
0
by: Leo Jay | last post by:
On Sat, Aug 23, 2008 at 1:58 AM, Jean-Paul Calderone <exarkun@divmod.comwrote: not exactly. the socket connecting to port 1 is listening to port 2. port 1 and port 2 are not the same. I'm...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.