Threaded server and client - No signals, not working.

Threaded server and client - No signals, not working.

Postby peterkl » Tue Aug 02, 2016 6:23 pm

Greetings everyone,

I created a Threaded server in order to manage several clients accessing to the server at the same time. Unfortunately when I turn on the server and send a request with a client I have no signal either from the server or on the client side. When I say signals I mean the texts I sent between the server and client. I would like to more specific, but I don't know my self where the problem is. Please have a look at the threaded server code and at the client code. :D

Server code:
Code: Select all
import threading
from socket import *

def handleclient(c):
   msg = "welcome client"
   c.send(msg)
   request = c.recv(10000)
   print request
   c.close()
   return
ss = socket(AF_INET,SOCK_STREAM)
ss.bind(("",9000))
ss.listen(5)
while True :
   c,a = ss.accept()
   t = threading.Thread(target=handleclient, args=(c,a))
#I tried to take the "t=" off, so the command
#will execute and not equal the t value,
#it did not work anyway


Client code:
Code: Select all
from socket import *
s1 = socket(AF_INET,SOCK_STREAM)
s1.connect(('127.0.0.1', 9000))
info = s1.recv(10000)
print info
msg = "hello server"
s1.send(msg)
s1.close()
Last edited by micseydel on Tue Aug 02, 2016 6:54 pm, edited 1 time in total.
Reason: Initial post lock.
peterkl
 
Posts: 9
Joined: Tue Aug 02, 2016 6:00 pm

Re: Threaded server and client - No signals, not working.

Postby wavic » Wed Aug 03, 2016 2:06 am

I am not sure about threading but your handleclient function takes one argument but in here target=handleclient, args=(c,a) are two.
Any Errors, any Exceptions?
wavic
 
Posts: 165
Joined: Wed May 25, 2016 8:51 pm

Re: Threaded server and client - No signals, not working.

Postby peterkl » Wed Aug 03, 2016 12:54 pm

I change the args =(c) it did not change a thing.

The script does not print any errors, the server just stays there waiting and the client too, but if I stop them with the ctr+c :
error message server :
^CTraceback (most recent call last):
File "server.py", line 15, in <module>
c,a = ss.accept()
File "/usr/lib/python2.7/socket.py", line 206, in accept
sock, addr = self._sock.accept()
KeyboardInterrupt

error message client :
^CTraceback (most recent call last):
File "client.py", line 5, in <module>
info = s1.recv(10000)
KeyboardInterrupt
peterkl
 
Posts: 9
Joined: Tue Aug 02, 2016 6:00 pm

Re: Threaded server and client - No signals, not working.

Postby wavic » Wed Aug 03, 2016 1:47 pm

Where do you start the thread?
t.start() ?

I don't see it in the code.
wavic
 
Posts: 165
Joined: Wed May 25, 2016 8:51 pm

Re: Threaded server and client - No signals, not working.

Postby peterkl » Thu Aug 04, 2016 1:22 pm

wavic wrote:Where do you start the thread?
t.start() ?

I don't see it in the code.


Thank you for your time and help ! It worked fine with start, every thread needs to be started I wasn't aware of that.

Python thread library :
start()

Start the thread’s activity.


It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.
peterkl
 
Posts: 9
Joined: Tue Aug 02, 2016 6:00 pm

Re: Threaded server and client - No signals, not working.

Postby wavic » Sat Aug 06, 2016 11:00 pm

Good!
Be aware that when you use something like

t = some_object

you just create and instance of that object. In order to do something with it you have to call it, if it is instance of some_function() or call it's method. In your case you actually call threading.t.start() where 't' is instance of threading.Thread subclass
wavic
 
Posts: 165
Joined: Wed May 25, 2016 8:51 pm


Return to Networking

Who is online

Users browsing this forum: No registered users and 1 guest