How should I handle socket receiving?

H

Hans

I'm thinking to write a code which to:
1. establish tons of udp/tcp connections to a server
2. send packets from each connections
3. receive packets from each connections and then do something based
on received content and connection statues.
4. repeat step 2 and step 3.

my question is how should I handle receiving traffic from each
connection respectively? two ways I'm thinking are:
1. setup connections one by one, put socket handler into an
array(dictionary?), array also record each handler's status. then in a
big loop, keep using "select" to check sockets, if any socket get
something received, then find the socket in the array, based on the
socket status to do things should be done. this one is like single-
thread?

2. (I don't know if this one works, but I prefer this one if it do
works. ) Setup connection object, write codes in the object to handle
sending/receiving and status. let each object to check/receive its
socket by itslef(I don't know how yet). Then In the main proc, I just
initiate those objects.

Any responses would be very welcome, thanks.
 
N

n00m

I'm abs not sure but maybe you'll need to put
each client into separate thread; like this

def Client_func(s2, cn):
while 1:
data = cn.recv(4096)
if not data:
s2.shutdown(1)
return
s2.sendall(data)



cn, addr = s1.accept()
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((the_server_host, the_server_port))
thread.start_new_thread(Client_func, (s2, cn,))
 
H

Hans

What does "tons" mean?  Tens?  Hundreds?


You're really going to want to use "select".  You can store the objectsin
a dictionary where the key is the socket number.  That way, you can usethe
result of the select and get your network object directly.

I wrote code like this:
main proc:
import socket_thread
#start 1000 connection
while i<1000:
my_socket=socket_thread.socket_thread(i,host,port)
my_socket.send(some_data)
my_socket.recv()

socket_thread.py
class socket_thread:
def __init__:
self.soc_handle=socket.socket(socket.IF_INET,socket.DGRAM)
def send(data):
self.soc_handle.send(data)
def recv():
while 1:

input_list,output_list,exec_list=select.select([self.soc_handle],[],[],
2)
data=input_list[0].recv(2048)
print data

But it does not work as I hope. main proc can only initiate one thread
and then trapped by it, cannot get out.
I'm sure I missed something but I don't know. Thanks for any help.
 
M

MRAB

I wrote code like this:
main proc:
import socket_thread
#start 1000 connection
while i<1000:
my_socket=socket_thread.socket_thread(i,host,port)
my_socket.send(some_data)
my_socket.recv()
This won't run as-is because you never assign to "i".
socket_thread.py
class socket_thread:
def __init__:
self.soc_handle=socket.socket(socket.IF_INET,socket.DGRAM)
def send(data):
self.soc_handle.send(data)
def recv():
while 1:

input_list,output_list,exec_list=select.select([self.soc_handle],[],[],
2)
data=input_list[0].recv(2048)
print data

But it does not work as I hope. main proc can only initiate one thread
and then trapped by it, cannot get out.
I'm sure I missed something but I don't know. Thanks for any help.

Your "socket_thread" class is just a normal class. You create an
instance, use it to send data, and then call its "recv" method, which
loops forever.
 
H

Hans

I wrote code like this:
main proc:
import socket_thread
#start 1000 connection
while i<1000:
     my_socket=socket_thread.socket_thread(i,host,port)
     my_socket.send(some_data)
     my_socket.recv()

This won't run as-is because you never assign to "i".




socket_thread.py
class socket_thread:
     def __init__:
           self.soc_handle=socket.socket(socket.IF_INET,socket.DGRAM)
     def send(data):
           self.soc_handle.send(data)
     def recv():
           while 1:
input_list,output_list,exec_list=select.select([self.soc_handle],[],[],
2)
               data=input_list[0].recv(2048)
               print data
But it does not work as I hope. main proc can only initiate one thread
and then trapped by it, cannot get out.
I'm sure I missed something but I don't know. Thanks for any help.

Your "socket_thread" class is just a normal class. You create an
instance, use it to send data, and then call its "recv" method, which
loops forever.- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Thanks.
"i" problem is OK, I can find and fix it easily. I also understand
the second problem, but I don't know how to fix it. What's your
meaning of "normal class"? Can I specify it as some special class and
then it can work as I hope?
thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top