| | |
server/client issues
Thread Solved |
•
•
Join Date: May 2008
Posts: 5
Reputation:
Solved Threads: 0
I am working on making my own chat server and client, but have reached a little hiccup. The first line of text that the client send is not displayed by the server. Everything else is, just not the first line.
Client
Server
Client
python Syntax (Toggle Plain Text)
import socket uname = raw_input('username:> ') uname = uname + ': ' host = raw_input('host name:> ') port = 51423 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) data = raw_input(':> ') s.send('\r\n') while data != '/exit': data = raw_input(':> ') s.send(uname + data + "\r\n") s.shutdown(1)
Server
python Syntax (Toggle Plain Text)
import socket, traceback, sys host = '' port = 51423 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host, port)) s.listen(1) while 1: try: clientsock, clientaddr = s.accept() except KeyboardInterrupt: raise except: traceback.print_exc() continue #clientsock.settimeout(60) # Process the connection try: print "Connection received from", clientsock.getpeername() while 1: data = clientsock.recv(1024) if not len(data): break sys.stdout.write(data) except (KeyboardInterrupt, SystemExit): raise #except socket.timeout(): print 'connection timed out' except: traceback.print_exc() # Close the connection try: clientsock.close() except KeyboardInterrupt: raise except: traceback.print_exc()
•
•
Join Date: Feb 2007
Posts: 1
Reputation:
Solved Threads: 0
The problem was that the first data = raw_input(':> ') is never sent
only the one that appears inside the while data !="/exit" loop is sent.
So obviously that first data is never displayed on the server side :-)
Solution 1: to set the first data assignment an empty string "" ex:
data =""
Solution 2 (breaks the DRY principle):
replace s.send('\r\n')
with
s.send(uname + data + "\r\n")
import socket
uname = raw_input('username:> ')
uname = uname + ': '
host = raw_input('host name:> ')
port = 51423
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
##### You don't want this here it never gets sent it just eats your first data
#data = raw_input(':> ')
#####
data=""
s.send('\r\n')
while data != '/exit':
data = raw_input(':> ')
s.send(uname + data + "\r\n")
s.shutdown(1)
only the one that appears inside the while data !="/exit" loop is sent.
So obviously that first data is never displayed on the server side :-)
Solution 1: to set the first data assignment an empty string "" ex:
data =""
Solution 2 (breaks the DRY principle):
replace s.send('\r\n')
with
s.send(uname + data + "\r\n")
import socket
uname = raw_input('username:> ')
uname = uname + ': '
host = raw_input('host name:> ')
port = 51423
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
##### You don't want this here it never gets sent it just eats your first data
#data = raw_input(':> ')
#####
data=""
s.send('\r\n')
while data != '/exit':
data = raw_input(':> ')
s.send(uname + data + "\r\n")
s.shutdown(1)
Last edited by tac; Jun 13th, 2008 at 1:27 am.
![]() |
Similar Threads
- Cannot find server or DNS Error - please help! (Viruses, Spyware and other Nasties)
- Issues w/ file uploads in PHP (PHP)
- Network Config Properties is missing a menu, what could be wrong - picture included (Windows NT / 2000 / XP)
- Server Avail check (HTML and CSS)
- Multi-user program issues (VB.NET)
- rundll32.exe issues (Viruses, Spyware and other Nasties)
- ftp client issues...im so confused...plz help!!! (Windows Software)
Other Threads in the Python Forum
- Previous Thread: using Eric IDE
- Next Thread: Comparing the next item in a list
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui heads homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython





