473,320 Members | 2,109 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,320 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 2920

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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.