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 01:50 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360