[Tutor] should we use multithreading for multiple clients in socket class

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] should we use multithreading for multiple clients in socket class

surya k
I am learning networking and just wrote a primitive client server program..

server:

from time import ctime
from socket import *

HOST = ""
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpSerSoc = socket(AF_INET, SOCK_STREAM)
tcpSerSoc.bind(ADDR)
tcpSerSoc.listen(5)

while True:
    print "waiting for connection.."
    tcpCliSoc, addr = tcpSerSoc.accept()
    print "# Connected from:", addr
    
    while True:
        data = tcpCliSoc.recv(BUFSIZ)
        if not data:
            break
        tcpCliSoc.send( "[%s] %s" % (ctime(), data ))
    tcpCliSoc.clock()
tcpSerSoc.close()
            

Client:

from socket import *

HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpCliSoc = socket(AF_INET, SOCK_STREAM)
tcpCliSoc.connect(ADDR)

while True:
    data = raw_input(">")
    if not data:
        break
    tcpCliSoc.send(data)
    data = tcpCliSoc.recv(BUFSIZ)
    if not data:
        break
    print data
tcpCliSoc.close()

This client sends a string, and receives the same string with current time...


So, I tried to run another client (same code but from different file) but found that its now working.

why its happening, should I use multithreading so that I can handle two clients simultaneously??





_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] should we use multithreading for multiple clients in socket class

Alan Gauld
On 12/04/12 12:38, Surya K wrote:
> I am learning networking and just wrote a primitive client server program..
...
> So, I tried to run another client (same code but from different file)
> but found that its now working.

I assume that should be *not working*?
A more detailed description of what you did and the result would help
diagnose the issue.

> why its happening, should I use multithreading so that I can handle two
> clients simultaneously??

It's not strictly necessary if there are no resource conflicts such as
two attempts to write to the same file/data structure. But in the
general case multi-threading a server (or forking a separate
process) is a good idea.

However, there is some implicit "threading" takes place in the socket
library insofar as each session is given a separate temporary port to
communicate over once the connection has been accepted so unless the
connections are using conflicting resource it should still work.

See the network programming topic in my tutorial for an example and
screenshots of two clients talking to one server...


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor