server/client issues

Thread Solved

Join Date: May 2008
Posts: 5
Reputation: theOneCalledJef is an unknown quantity at this point 
Solved Threads: 0
theOneCalledJef theOneCalledJef is offline Offline
Newbie Poster

server/client issues

 
0
  #1
May 22nd, 2008
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
  1. import socket
  2. uname = raw_input('username:> ')
  3. uname = uname + ': '
  4. host = raw_input('host name:> ')
  5. port = 51423
  6.  
  7. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  8. s.connect((host, port))
  9. data = raw_input(':> ')
  10. s.send('\r\n')
  11. while data != '/exit':
  12. data = raw_input(':> ')
  13.  
  14. s.send(uname + data + "\r\n")
  15.  
  16. s.shutdown(1)

Server
  1. import socket, traceback, sys
  2.  
  3. host = ''
  4. port = 51423
  5.  
  6. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  7. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  8. s.bind((host, port))
  9. s.listen(1)
  10.  
  11. while 1:
  12. try:
  13. clientsock, clientaddr = s.accept()
  14. except KeyboardInterrupt:
  15. raise
  16. except:
  17. traceback.print_exc()
  18. continue
  19.  
  20. #clientsock.settimeout(60)
  21.  
  22. # Process the connection
  23. try:
  24. print "Connection received from", clientsock.getpeername()
  25. while 1:
  26. data = clientsock.recv(1024)
  27. if not len(data):
  28. break
  29. sys.stdout.write(data)
  30. except (KeyboardInterrupt, SystemExit):
  31. raise
  32. #except socket.timeout():
  33. print 'connection timed out'
  34. except:
  35. traceback.print_exc()
  36.  
  37. # Close the connection
  38. try:
  39. clientsock.close()
  40. except KeyboardInterrupt:
  41. raise
  42. except:
  43. traceback.print_exc()
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1
Reputation: tac is an unknown quantity at this point 
Solved Threads: 0
tac tac is offline Offline
Newbie Poster

Re: server/client issues

 
0
  #2
Jun 13th, 2008
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)
Last edited by tac; Jun 13th, 2008 at 1:27 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC