nautilor
nautilor

Reputation: 116

Python simple socket chat User connection and output of message

I am creating a simple chat in python 3 using socket

here are the code

CLIENT

#!/bin/python

import socket
import threading
import time

tLock = threading.Lock()
poweroff = False

def receving(name, sock):
    while not poweroff:
        try:
            tLock.acquire()
            while True:
                data, addr = sock.recvfrom(1024)
                print (str(data))
        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=receving, args=("RecvThread", s))
rT.start()

alias = input("Username: ")
time.sleep(0.2)

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

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

SERVER

#!/bin/python

import socket
import time

hostname = '127.0.0.1'
port = 5000

clients = []

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((hostname, port))
s.setblocking(0)

iQuit = False
print ("Server Started.")

while not iQuit:
    try:
        data, addr = s.recvfrom(1024)
        if addr not in clients:
            clients.append(addr)

        print (time.ctime(time.time()) + str(addr) + ": :" + str(data))
        for client in clients:
            s.sendto(data, client)
    except:
        pass
s.close()

How do i print a string to the server when a user connect?

I have tried to add this string after you have insert the name

s.sendto(str(alias + " Connected").encode('utf-8'), server)

but the output is orrible for me

Another Question: Why i get something like this when seding a message?

Username: User_A
User_A>>> Hello
User_A>>> How Are you?
b'User:A: Hello'
User_A>>> 
b'User_A: How Are you?'
b'User_B: Hi'

Upvotes: 1

Views: 1236

Answers (2)

Armali
Armali

Reputation: 19375

i have to press enter to see if user_B send something to me.

You enforced this behavior by locking out the receiving thread during the input of a message. You have to make up your mind whether you want this or want incoming data to be printed while typing.
You might want to cf. Simultaneous input and output for network based messaging program.

Upvotes: 0

bastelflp
bastelflp

Reputation: 10086

Concerning your second question: You are printing binary strings, see here for more information.

Use str(data.decode('utf-8')) instead of str(data) when printing the message on the server or the client.

Concerning the first question: This should work if you send the "Connected" string just after asking for the user name. The string is decoded the same way as a common message if you include the decode('utf-8') and looks normal to me.

Upvotes: 1

Related Questions