Python Sockets Question


M

Maboroshi

Hi I am building a simple chat program but I am running into problems
interacting with more than one client I can send a message to a server
and it can recieve it no problem but how do I get more than one client
to see the messages as well so clients can talk to eachother basically
how do I get the server to send the messages back so all the clients can
see it

Here is my code

The Server ------------------------------------------------------------

import socket
# Set the socket parameters
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)

# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)

# Receive messages
while 1:
data,addr = UDPSock.recvfrom(buf)
if not data:
print "Client has exited!"
break
else:
print "\nReceived message '", data,"'"

# Close socket
UDPSock.close()



The Client ------------------------------------------------------------

from Tkinter import *
import socket

# Set the socket parameters
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)

# Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)


class App(Frame):
def send(self):
# Send messages
while (1):
data = self.entry.get()
if not data:
break
else:
if(UDPSock.sendto(data,addr)):
break


def disconnect(self):
# Close socket
UDPSock.close()

def __init__(self, master=None):
Frame.__init__(self, master)
self.pack(fill=BOTH, expand=True)

self.createWidgets()

def createWidgets(self):
self.scrollbar = Scrollbar(self)
self.scrollbar.grid(row=0, column=1, sticky=N+S)
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.listbox = Listbox(self)
self.listbox.grid(row=0, column=0, sticky=N+E+S+W)
self.listbox.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.listbox.yview)
self.entry = Entry(self)
self.entry.grid(row=1, column=0, columnspan=2, sticky=E+W)


self.top = top = Toplevel()
top.title("Chat Client")
top.maxsize(100, 50)
top.minsize(100, 50)
self.execute = Button(top, text="Send", command=self.send)
self.execute.grid(row=1, column=0, sticky=E+W)
self.disconnect = Button(top, text="Disconnect",
command=self.disconnect)
self.disconnect.grid(row=2, column=0, sticky=E+W)

app = App()
app.master.title("Chat Client")
app.master.minsize(400, 300)
app.master.maxsize(600, 500)
app.mainloop()
 
Ad

Advertisements

J

Josiah Carlson

Hi I am building a simple chat program but I am running into problems
interacting with more than one client I can send a message to a server
and it can recieve it no problem but how do I get more than one client
to see the messages as well so clients can talk to eachother basically
how do I get the server to send the messages back so all the clients can
see it

Don't use UDP, use TCP. TCP connections are persistant, so you can send
information to all of your clients as necessary.

This will change your application, as it requires you to both read and
write from sockets. Check out the asyncore module, or perhaps even
asynchat (which is 95% of a chat server already).

- Josiah
 
M

Maboroshi

Hey thanks man Ya I was using TCP at first, to be honest this project
has changed so many times I will have to look further into those modules

Cheers

Andrew
 
Ad

Advertisements

I

Irmen de Jong

Maboroshi said:
Hey thanks man Ya I was using TCP at first, to be honest this project
has changed so many times I will have to look further into those modules

Cheers

Andrew

If you don't really care about the low-level network programming,
you could also consider using Pyro (http://pyro.sourceforge.net).
It includes a high level event server that makes it very easy
to create multi-client chat applications. There's an example included.

--Irmen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top