0

Unfortunately I am unable to find an answer for this even after an hour of searching.

I borrowed this from online tutorials - Youtube - Draps

import socket, threading, time, wx

tLock = threading.Lock()
shutdown = False

def receiving(name, sock):

    while not shutdown:
        try:
            tLock.acquire()
            #while True:
            data, addr = sock.recvfrom(1024)
            print str(data) + "hehehe"
        except:
            pass
        finally:
            tLock.release()

host = '127.0.0.1'
port = 0
server = ('127.0.0.1', 5000)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host,port))
s.setblocking(0)

rT = threading.Thread(target = receiving, args = ("RecvThread", s))
rT.start()

alias = raw_input("Name: ")
message = raw_input(alias + "-->")


while message != 'q':
    if message != '':
        s.sendto(alias + ":" + message, server)
    tLock.acquire()
    message = raw_input(alias + "-->")
    tLock.release()
    time.sleep(0.2)


shutdown = True
rT.join()
s.close()

I have two questions:

  1. In the code, host = '127.0.0.1'. However if I use socket.gethostbyname(socket.gethostname()), I get a socket error. Could anyone tell me why is this so? When I deploy a similar code to an external computer, it should not have this problem of creating a socket.

  2. I started a thread that runs continuously. Why is the shutdown value (which is declared after the thread has started) able to stop the rT thread and break the while loop? I am unable to understand the physics and surprised it is working.

1 Answer 1

1

I'm not 100% sure on the first question but for the second one shutdown is a global variable. Any threads spawned from the main thread have the ability to see the shutdown

Can you post the socket error you are getting?

Sign up to request clarification or add additional context in comments.

11 Comments

Traceback (most recent call last): File "C:/Users/kundemj/Desktop/Web Scraping/client.py", line 35, in <module> s.sendto(alias + ":" + message, server) socket.error: [Errno 10049] The requested address is not valid in its context
Would it also work, if both my variable and thread are defined in a class of main thread?
@jesh I'm not sure I understand your question? what do you mean "in a class of main thread"
also, that error you are getting has nothing to do with the variable host. You are trying to send data to server and it doesn't like that address. It could be for any number of reasons. Try not using the loopback? Are you actually running a server on port 5000?
Well it has to do with the "host" as it is working when host = '127.0.0.1' and my server is also on '127.0.0.1' port 5000. So if deploy my client code to another computer, what should be an universal value for the "host" variable?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.