1

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'
bastelflp
  • 9,362
  • 7
  • 32
  • 67
nautilor
  • 106
  • 12

2 Answers2

1

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.

Community
  • 1
  • 1
bastelflp
  • 9,362
  • 7
  • 32
  • 67
  • ok on the server is normal but when a new user connect he gets something like this `Username: b'127.0.0.1 Connected'`. The problem here are two, 1] i have to press enter to see if user_B send something to me. 2] when a new user connect he gets all the previous data. how can i filter the data to send it to the right user? i was thing about using addr[2] that is a unique number of the user – nautilor Dec 13 '15 at 15:13
0

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.

Armali
  • 18,255
  • 14
  • 57
  • 171