socket client server... simple example... not working...

SpreadTooThin bjobrien62 at gmail.com
Thu Oct 5 04:31:38 CEST 2006


client:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.1.101", 8080))
print 'Connected'
s.send('ABCD')
buffer = s.recv(4)
print buffer
s.send('exit')


server:

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(("192.168.1.101", 8080))
serversocket.listen(5)
print 'Listen'
(clientsocket, address) = serversocket.accept()
print 'Accepted'
flag = True
while flag:
	chunk = serversocket.recv(4)
	if chunk == '':
		raise RuntimeError, "socket connection broken"
	elif chunk == 'exit':
		flag = False
	else:
		serversocket.send(chunk)
print 'Done'

Server says!

Listen
Accepted
Traceback (most recent call last):
  File "server.py", line 11, in ?
    chunk = serversocket.recv(4)
socket.error: (57, 'Socket is not connected')


Client says:
Connected

What have I done wrong now!




More information about the Python-list mailing list