Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 11-07-2004, 12:39 PM   #1 (permalink)
Insane
 
Location: West Virginia
[Python] Simple chat program (EDIT: Now a TELNET Question)

Okay basically I need to write a simple chat program. So far I have the following for the server and client code:

Code:
# server
import socket
import thread

HOST = 'localhost'               # Symbolic name meaning the local host
PORT = 150                       # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))

while True:
    s.listen(1)
    conn, addr = s.accept()
    print 'Connected by', addr
    while 1:
        data = conn.recv(1024)
        if data[:4] == "exit": print 'Parted by', addr
        else:   conn.send(data),
        if not data: break
        print `data`
conn.close()
and...
Code:
# client
import socket
getHOST = (raw_input('Enter the Host: '))
print getHOST
HOST = getHOST
PORT = 150        # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

NICK = raw_input('Welcome, Enter your name: ')

while True:
    MSG = raw_input('>> ')
    MSG = NICK + ': ' + MSG
    if MSG[5:] == "exit": break
    s.send(MSG)
    data = s.recv(1024)
    print data
s.close()
The main issue is this:
I have no idea to allow it to accept connections from multiple clients. I need it to basically keep checking for a new connection. After that is done, I can already tell I'm going to have an issue with being able to receive messages from other users while I am sitting at the prompt to send a message- since that will be holding up the program.

Anyone have some experience with this?
__________________

- Artsemis
~~~~~~~~~~~~~~~~~~~~
There are two keys to being the best:
1.) Never tell everything you know

Last edited by Artsemis; 11-07-2004 at 05:00 PM..
Artsemis is offline  
Old 11-07-2004, 04:11 PM   #2 (permalink)
a-j
Tilted
 
The basic solution (not python specific) is to fork or create a thread for each incoming connection. Somewhere after you do the accept , create a thread and handle all communication from that connection in the thread. The server while loop would then go back to listening for connections and forking/creating threads as it needs.
a-j is offline  
Old 11-07-2004, 05:00 PM   #3 (permalink)
Insane
 
Location: West Virginia
Well I modified the server code a bit and got it to work somewhat but the issue was still the client would "stall" while waiting on input before it would display anything the other users would input.

One way around this I found was to use Telnet as a client... only problem is as I type each letter, it is being sent to the server; other than that, it works great! Anyone have an idea of the cause of this? And is there a way to go around it?

Thanks!
__________________

- Artsemis
~~~~~~~~~~~~~~~~~~~~
There are two keys to being the best:
1.) Never tell everything you know
Artsemis is offline  
Old 11-07-2004, 05:13 PM   #4 (permalink)
Crazy
 
Location: Salt Town, UT
Actually, I'm not sure how good Python's support of threads or shared memory is.

(which if you are forking, and want to send data between connections, you need some way of sharing memory)

So what I would suggest is to use Python's select class, and you just sit there waiting on select, when something happens in the select, if it is your listening socket, you accept a new connection, add it to the list of sockets you are waiting on and go back into the loop. If you get something from one of the "clients" you loop through your connections (except the listening socket) and send the message out to each one.

I'm not too familar with Python, but I found this guide by searching on google, and it seems to be pretty good. And it even recommends using threads, so there's another way to do it: http://www.amk.ca/python/howto/sockets/
Rawb is offline  
Old 11-07-2004, 09:46 PM   #5 (permalink)
Insane
 
Location: West Virginia
Code:
# client
import socket
import thread

getHOST = (raw_input('Enter the Host: '))
HOST = getHOST
PORT = 150        # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
NAME = raw_input('Enter your name: ')
s.send(NAME)
data = s.recv(1024)
print data

def checkMessage():
    while 1:
        try:
            data = s.recv(1024)
            print data
        except:
            return None
    
thread.start_new_thread(checkMessage,())

while True:
    MSG = raw_input('>> ')
    s.send(MSG)
    #if MSG == "quit": break
s.close()
Made -alot- of progress! The ONLY issue I can see as of right now is that this thread isn't working that constantly reads for incoming data - any help is greatly appreciated.
__________________

- Artsemis
~~~~~~~~~~~~~~~~~~~~
There are two keys to being the best:
1.) Never tell everything you know
Artsemis is offline  
 

Tags
chat, program, python, simple


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 05:05 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76