dear Group256
i am a student who is doing my project on python platform...according to my project i has to have a client server public chat code which is very similar to the one u have posted here. i am currently working on a code for this puepose but i found many errors

i would be very thankful if you could send me your code to [snipped] so that i would be able to complete my project
thanking you
Mithra nair

Dani AI

Generated

This thread asks for a client/server public chat in Python. Before asking for full code, post a minimal reproducible example (the exact error traceback, the Python version and OS, and the few lines that fail). and were pointing in that direction: contributors can help best when there is a concrete problem to fix. For a client GUI, run networking off the UI thread (use worker threads or an event loop) as suggested.

A simple, reliable design for a text chat:

  • TCP server accepts clients, keeps a list, and broadcasts framed messages (use newline as a delimiter).
  • Decide an encoding (utf-8) and stick to it on both ends.
  • Handle disconnects and exceptions (remove dead sockets).
  • For GUI clients, use thread-safe UI calls (wx.CallAfter, Qt signals, or queue+poll).

Example: a compact threaded server and a minimal console client. Run the server first, then start multiple clients.

# server.py
import socket, threading
HOST, PORT = '', 12345
clients, lock = [], threading.Lock()

def handle(conn, addr):
    with conn:
        f = conn.makefile('r')
        for line in f:
            msg = line.rstrip('\n')
            with lock:
                for c in clients[:]:
                    if c is not conn:
                        try:
                            c.sendall((addr[0]+': '+msg+'\n').encode('utf-8'))
                        except Exception:
                            clients.remove(c)

sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((HOST, PORT))
sock.listen(5)
while True:
    conn, addr = sock.accept()
    with lock: clients.append(conn)
    threading.Thread(target=handle, args=(conn, addr), daemon=True).start()
# client.py
import socket, threading, sys
s = socket.create_connection(('127.0.0.1', 12345))
def recv():
    f = s.makefile('r')
    for line in f:
        print(line, end='')
threading.Thread(target=recv, daemon=True).start()
for line in sys.stdin:
    s.sendall(line.encode('utf-8'))

Troubleshooting: if bind fails, check port and use SO_REUSEADDR; watch for Python2 vs Python3 encoding issues; if the GUI freezes, move networking off the UI thread; test with telnet to confirm the server. Post the smallest failing snippet and full traceback for targeted help.

Recommended Answers

All 3 Replies

wx.python would be fairly easy to use for the client side GUI stuff.

ahappysadface can you help yourself so that we can help you ok?

dear Group256
i am a student who is doing my project on python platform...according to my project i has to have a client server public chat code which is very similar to the one u have posted here. i am currently working on a code for this purpose but i found many errors

i would be very thankful if you could send me your code to so that i would be able to complete my project
thanking you
Mithra nair

You should [thread=78223]Read This First[/thread] and then [thread=573]this[/thread]. Unless you can first show some code, some obvious effort to solve your own problem, DaniWeb is not the right place for you. You should also follow the DaniWeb rules of etiquette. In particular, you should not hijack a thread, but instead start your own thread with your own question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.