In Python using the threading module, my program will not run the 2 threads I created simultaneously. I am trying to create an instant message program using P2P, and I don't know if the problem is to do with the socket module or I am doing something wrong. Code:
import socket
import threading
class Receiver:
def __init__(self):
# Create socket that supports IPv4, TCP Protocol
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Socket created."
# Requests for IP of host (DNS)
dns = "localhost"
HOST = ''
PORT = 57492
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT)) # Listens on all interfaces...
print 'Listening on port 25565'
s.listen(True) # Listen on the newly created socket...
conn, addr = s.accept()
print 'Connected in port 25565'
global data
while 1:
data = conn.recv(1024)
print data
s.close()
def Sender(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dns = "localhost"
HOST = socket.gethostbyname(dns)
PORT = 57492
# Connect to server
s.connect((HOST, PORT))
print "Socket connected to " + dns + " on IP " + HOST
while 1:
# Assign message to be sent to server to a variable
message = raw_input("Message to be sent to server: ")
#Send the actual message to server
s.sendall(message)
print "Message sent successfully"
s.close()
def go(self):
th1 = threading.Thread()
th2 = threading.Thread(target=self.Sender)
th1.start()
th2.start()
t = Receiver()
t.go()