socket.recvfrom() & sendto()

Ron Johnson ron.l.johnson at home.com
Tue May 8 01:54:53 EDT 2001


Ulrich Berning wrote:

> Hi,
> 
> I think, you want to something like that:
> 
> import socket, select, os
> 
> # Create a connection socket, bind it and listen on it
> connect_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> connect_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
> connect_socket.bind((socket.gethostname(), 50000))
> connect_socket.listen(5)
> 
> # Our server accepts connect requests forever
> while 1:
>     # The accept() call blocks until there is a connect request.
>     # The call returns a new socket, that should be used for further
>     # communication.
>     work_socket, addr = connect_socket.accept()
>     # Start a new process (or thread) for client server communications
>     # or use select() to do this in the same process
>     child_pid = os.fork()
>     if not child_pid: # Child process
>         # The child process should close the connect socket immediately
>         connect_socket.close()
>         # This functions should do the communication with the client
>         client_worker_function(work_socket)
>         # When all work is done, the child can die
>         sys.exit()
> 
> 
> The trick is the accept() system call, which returns a new socket that
> should be used for the client server communication. The connect socket
> can then listen on the defined port for the next connect request.

This is exactly what I want!  I might, though, try threading, 
since global data must be kept: depending on what client1 does,
data is sent to client2..clientN.

This is looking more and more like a chat program, where clients
and server are chatting back and forth, instead of clients chatting.

Still, I have to think how the client can wait for a possible message
from the server while simultaneously letting the client do "stuff" 
like send messages to the server.  Threading at the client, too?
One thread waiting for chat from the server, while the other waits
for user input to send to the server?

Something seems too complicated here...

Thanks,
Ron
-- 
 Ron Johnson, Jr.        Home: ron.l.johnson at home.com
 Jefferson, LA  USA      http://ronandheather.dhs.org
 "Is Python better or worse than Perl?"
  "Perl is worse than Python because people wanted it 
   worse." -Larry Wall, 10/14/1998



More information about the Python-list mailing list