Hi there,
I'm currently in the process of creating a Multi-User Dungeon (MUD) in python.
It'll allow multiple users to explore a text based world.

I've got a basic server but i can't seem to get a client to work with it.

MUD server.py

import socket

#Connect with telnet
#On linux: telnet localhost 5001
#or: telnet 192.168.1.8 5001 (if you connecting to a different machine)


#Port we wish to serve on
SERVER_PORT=5001

#create STREAMing socket (TCP)
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#Bind to any/default interface on the local machine
serversocket.bind(("", SERVER_PORT))

#become a server socket
serversocket.listen(5)


#List of clients - fill this up as we go
clients=[]

#Each client has a name and a socket.
#You can/should add to this - location, inventory, ...


#Set blocking to OFF.  Without threads, this is the only way we can
#deal with multiple users.  If blocking is ON, we will be stuck at the
#accept() funciton call until someone connects
serversocket.setblocking(0)

while True:
    try:
        #accept connections from outside. Note we get a new socket
        #object, leaving the original one free for more users to
        #connect
        (clientsocket, address) = serversocket.accept()
        #add it to the client list
        person={}
        person["name"]="guest" #Everyone starts as guest
        person["sock"]=clientsocket
        
        clients.append(person)
        #Set blocking to OFF.  Same as before, but this time we don't
        #want to block on the recv() call
        clientsocket.setblocking(0)
    except:
        #Non-blocking sockets throw an exception if asked to
        #connect/recieve when there's nothing to do.  They do this
        #rather than block.  We need to cope with that, so we catch
        #the exception and move on
        pass


    #Keep a list of messages ready to be sent
    messages=[]


    #See which clients have something to say
    for c in clients:
        try:
            #Catch the exception associated with non-blocking sockets again
            data=c["sock"].recv(1024)
            if data!="" and data != None:
                #Use this if you need to debug exacty what the server receives from clients
                #for ch in data:
                #    print ord(ch),",",
                #print
                data=data[:-2]
                if data.startswith("say:"):
                    messages.append("%s says '%s'\n"%(c["name"], data[data.find(":")+1:]))
                elif data=="look":
                    c["sock"].send("I can't see anything.  Nobody has implemented locations.  Yet...\n")
                else:
                    #If there is something else, send a message back
                    c["sock"].send("Sorry, didn't understand that.\n")
            
                
        except:
            pass
        
    #Now we have a list of messages sent from clients since the last
    #time through the loop, we send each item to every client
    for c in clients:
        for m in messages:
            c["sock"].send(m)

an example of a client i'm trying to ge to work is:
Client.py

#!/usr/bin/python
#Send text to the log server

import socket

HOST = '127.0.0.1'
SERVER_PORT = 5001              

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

while 1:
    #Get any data
    data = raw_input("Data: ")

    serversocket.sendto(data,(HOST,SERVER_PORT))
    if data=="quit": break

As i said i cannot get it to work, if i could the res, i think, would be a cakewalk (putting in rooms and equipment etc.)

Can anyone give me any advice to help get this working together?
I am abit stuck.

My apologese if i havent been very in depth.

Thanks in advance
Thehivtyrant

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.