I am new to socket programming. I wanted to create a simple messaging system between the server and the client ( chat ). I have included my code below. I am expecting it to work as similar as chat system but it doesn't work. If the message is sent it should receive and print it out but only after giving the input the received string is printed. I am expecting it should run parallelly (receive a message and send a message).
Server :
import socket
import time
import threading
def get(s):
tm = s.recv(1024)
print("\nReceived: ",tm.decode('ascii'))
def set_(s):
i=input("\nEnter : ")
s.send(i.encode('ascii'))
serversocket = socket.socket()
host = socket.gethostname()
port = 9981
serversocket.bind((host,port))
serversocket.listen(1)
clientsocket,addr = serversocket.accept()
while(1):
t1=threading.Thread( target = get , args = (clientsocket,) )
t1.start()
t2=threading.Thread( target = set_ , args = (clientsocket,) )
t2.start()
time.sleep(10)
clientsocket.close()
Client:
import socket
import threading
import time
def get(s):
tm = s.recv(1024)
print("\nReceived: ",tm.decode('ascii'))
def set_(s):
i=input("\nEnter : ")
s.send(i.encode('ascii'))
s = socket.socket()
host = socket.gethostname()
port = 9981
s.connect((host,port))
while(1):
t1=threading.Thread( target = get , args = (s,) )
t2=threading.Thread( target = set_ , args = (s,) )
t1.start()
t2.start()
time.sleep(10)
s.close()
Output (At Client) :
Enter: hello ------------------------------>(1)
Received: hello --------------------------->(3)
Output (At Server) :
Enter: hello ------------------------------>(2)
Received : hello ------------------------->(4)
Expected Output:
Output (At Client) :
Enter: hello ------------------------------>(1)
Received: hello --------------------------->(4)
Output (At Server) :
Received : hello ------------------------->(2)
Enter: hello ------------------------------>(3)
The number represents the order of execution.
Sockets and the socket API are used to send messages across a network. They provide a form of inter-process communication (IPC). The network can be a logical, local network to the computer, or one that's physically connected to an external network, with its own connections to other networks.
There is an issue with the threading logic of your program. You should move the while(True)
loops to the thread workers, and only start your threads once. As it stands, your code can only send/receive one message every 10 seconds.
Server:
import socket
import threading
def get(s):
while True:
tm = s.recv(1024)
print("\nReceived: ",tm.decode('ascii'))
def set_(s):
while True:
i=input("\nEnter : ")
s.send(i.encode('ascii'))
serversocket = socket.socket()
host = socket.gethostname()
port = 9981
serversocket.bind((host,port))
serversocket.listen(1)
clientsocket,addr = serversocket.accept()
t1=threading.Thread( target = get , args = (clientsocket,) )
t1.start()
t2=threading.Thread( target = set_ , args = (clientsocket,) )
t2.start()
Client:
import socket
import threading
def get(s):
while True:
tm = s.recv(1024)
print("\nReceived: ",tm.decode('ascii'))
def set_(s):
while True:
i=input("\nEnter : ")
s.send(i.encode('ascii'))
s = socket.socket()
host = socket.gethostname()
port = 9981
s.connect((host,port))
t1=threading.Thread( target = get , args = (s,) )
t2=threading.Thread( target = set_ , args = (s,) )
t1.start()
t2.start()
You'll need to handle closing the sockets differently, and the enter/received prints get out of sync after the first message due to the multithreaded nature of the program, but the input is still waiting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With