By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,995 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,995 IT Pros & Developers. It's quick & easy.

Basic Server/Client socket pair not working

P: n/a
Hi there,

I'm trying to write a simple server/client example. The client should be able
to send text to the server and the server should distribute the text to all
connected clients. However, it seems that only the first entered text is sent
and received. When I then get prompted for input again and press return,
nothing gets back to me. Any hints on what I have done would be very much
appreciated!

Here's my code:

############ SERVER ##########
import socket
import select

mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.bind(('', 11111))
mySocket.listen(1)

clientlist = []

while True:
connection, details = mySocket.accept()
print 'We have opened a connection with', details
clientlist.append(connection)
readable = select.select(clientlist, [], [])
msg = ''
for i in readable[0]:
while len(msg) < 1024:
chunk = i.recv(1024 - len(msg))
msg = msg + chunk

for i in clientlist:
totalsent = 0
while totalsent < 1024:
sent = i.send(msg)
totalsent = totalsent + sent

############## CLIENT ################
import socket
import select

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect(("127.0.0.1", 11111))

while True:
text = raw_input("Du bist an der Reihe")
text = text + ((1024 - len(text)) * ".")
totalsent = 0
while totalsent < len(text):
sent = socket.send(text)
totalsent = totalsent + sent

msg = ''
while len(msg) < 1024:
chunk = socket.recv(1024 - len(msg))
msg = msg + chunk

print msg
Aug 29 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

Michael Goettsche wrote:
Hi there,

I'm trying to write a simple server/client example. The client should be able
to send text to the server and the server should distribute the text to all
connected clients. However, it seems that only the first entered text is sent
and received. When I then get prompted for input again and press return,
nothing gets back to me. Any hints on what I have done would be very much
appreciated!

Here's my code:

############ SERVER ##########
import socket
import select

mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.bind(('', 11111))
mySocket.listen(1)

clientlist = []

while True:
connection, details = mySocket.accept()
print 'We have opened a connection with', details
clientlist.append(connection)
readable = select.select(clientlist, [], [])
msg = ''
for i in readable[0]:
for i in readable:
while len(msg) < 1024:
chunk = i.recv(1024 - len(msg))
msg = msg + chunk

for i in clientlist:
totalsent = 0
while totalsent < 1024:
sent = i.send(msg)
totalsent = totalsent + sent

############## CLIENT ################
import socket
import select

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect(("127.0.0.1", 11111))

while True:
text = raw_input("Du bist an der Reihe")
text = text + ((1024 - len(text)) * ".")
totalsent = 0
while totalsent < len(text):
sent = socket.send(text)
totalsent = totalsent + sent

msg = ''
while len(msg) < 1024:
chunk = socket.recv(1024 - len(msg))
msg = msg + chunk

print msg


Aug 29 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.