here i have server and client that can connect together and send message.but second method*(def run(self):)* of these classes in both server and client did not called why they are send and receive message?(Why these methods working without calling them?)
code of server that uses socket and Thread:
from threading import Thread
import socket
class SendingThread(Thread):
def __init__(self, mySocket):
Thread.__init__(self)
self.mySocket = mySocket
def run(self):
# write code to send data continuously
while True:
data = input()
self.mySocket.send(bytes(data, 'utf-8'))
class ReceivingThread(Thread):
def __init__(self, mySocket):
Thread.__init__(self)
self.mySocket = mySocket
def run(self):
# write code to receive data continuously
while True:
msg = self.mySocket.recv(1024)
print(msg.decode('utf-8'))
# create a socket object
s = socket.socket(
socket.AF_INET, # internet address family => IP v4
socket.SOCK_STREAM # TCP
)
# bind socket with a port number
s.bind(('127.0.0.1', 2010))
# keep System_1 in listening mode
s.listen()
# accept the incoming connection request
mySocket, address = s.accept()
# create a thread to send data
sendThread = SendingThread(mySocket)
# create an another to receive data
receiveThread = ReceivingThread(mySocket)
# start both threads
sendThread.start()
receiveThread.start()
code of client that uses socket and Thread:
from threading import Thread
import socket
class MySendingThread(Thread):
def __init__(self, mySocket):
Thread.__init__(self)
self.mySocket = mySocket
def run(self):
# write code to send data to System_1
while True:
data = input()
self.mySocket.send(bytes(data, 'utf-8'))
class MyReceivingThread(Thread):
def __init__(self, mySocket):
Thread.__init__(self)
self.mySocket = mySocket
def run(self):
# write code to receive data from System_1
while True:
msg = self.mySocket.recv(1024)
print(msg.decode('utf-8'))
# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# send a connection request
s.connect(('127.0.0.1', 2010))
# create a thread to send data => System_1
mySendThread = MySendingThread(s)
# create a thread to receive data from System_1
myReceiveThread = MyReceivingThread(s)
# start both threads
mySendThread.start()
myReceiveThread.start()
>Solution :
SendingThread and ReceivingThread in server and MySendingThread and MyReceivingThread in client inherit from threading.Thread
Have a look at the docs:
The Thread class represents an activity that is run in a separate
thread of control. There are two ways to specify the activity: by
passing a callable object to the constructor, or by overriding the
run()method in a subclass. No other methods (except for the
constructor) should be overridden in a subclass. In other words, only
override the__init__()andrun()methods of this class.Once a thread object is created, its activity must be started by calling the thread’s
start()method. This invokes therun()method in
a separate thread of control.
When you call start() it invoke the respective run() method you override in the child class.