1

So , i learned the concepts of socket programming and multithreading today and quickly had an idea to implement it by creating a Messenger kind of app , where two connected clients can chat with each other.

Server.py :

 import socket

c = None #Client socket1
addr = None #Client address1
c2 = None #Client socket2
addr2 = None #Client address2

server_socket1 = socket.socket() #by default it is SOCK_STREAM (TCP) and has porotocal AF_INET (IPv4) 

server_socket1.bind(('192.168.217.1',9999)) #server machine's ip and port on which it will send and recieve connections from

server_socket1.listen(2) #We will only accept two connections as of now , one for each client
print("Server started successfully!!!")
print("Waiting for connections...\n\n")

while (((c is None)and(addr is None)) and ((c2 is None) and (addr2 is None))):
    if((c is None) and (addr is None)):
        c,addr = server_socket1.accept()
        print("User connected to client1 socket!!")
        c.send(bytes("Connected to the apps server!!!","utf-8"))
        name = c.recv(1024).decode()
        print("Client connected with name "+name+ " ip address "+str(addr))
        c.send(bytes("******Welcome to Messenger Lite "+name+" ******","utf-8"))

    if((c2 is None) and (addr2 is None)):
        c2,addr2 = server_socket1.accept()
        print("\n\nUser connected to client2 socket!!")
        c2.send(bytes("\n\nConnected to the apps server!!!","utf-8"))
        name2 = c2.recv(1024).decode()
        print("Client connected with name "+name2+ " ip address "+str(addr2))
        c.send(bytes("******Welcome to Messenger Lite "+name2+" ******","utf-8"))


while True:
    msg = c.recv(4096)
    if(msg!=None):
            msg = msg.decode()
            c2.send(bytes(msg,"utf-8"))

    msg2 = c2.recv(4096)
    if(msg2!=None):
            msg2 = msg2.decode()
            c.send(bytes(msg2,"utf-8"))

Client.py :

import socket
from threading import *


class recv_msg(Thread):
    def run(self):
        while True:
            msg = client_socket.recv(4096)
            if(msg!=None):  
                msg= client_socket.recv(4096).decode()
                print("\n"+msg)

class send_msg(Thread):
    def run(self):
        while True:
            message = input("You : ")
            client_socket.send(bytes(name+" : "+message,"utf-8"))


client_socket = socket.socket() #by default it is SOCK_STREAM (TCP) and has porotocal AF_INET (IPv4) 

client_socket.connect(('192.168.217.1',9999)) #server machine's ip and port on which it will send and recieve connections from
name = input ("Enter your name : ")
client_socket.send(bytes(name,"utf-8"))

print(client_socket.recv(1024).decode())
print(client_socket.recv(1024).decode())

t1 = recv_msg()
t2 = send_msg()
t1.start()
t2.start()
t2.join()

Here when i run this , two clients can communicate with each other , but not all messages gets printed in order. What i want to do is at a time i want the client to ask for inputs for messages and also want to print a message if received , concurrently. But when i run these , the messages are sometime printed , sometimes not , the output flow is not as it is supposed to.

As seen in this picture , The client first waits for input and then prints the incoming messages.

So how can i make something , when a message is received , it is printed even though i don't supply any inputs? Or is there any concept still i need to learn , that needs to be implemented here??

Priyansh Gupta
  • 89
  • 1
  • 10

0 Answers0