472,958 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 2873

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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.