473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Basic Server/Client socket pair not working

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
1 2304

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: User | last post by:
Hi, I tried to find the information over the internet but didn't find any answers. I'm looking for a server side code example of winsock accepting many clients. I know that in VB.NET it is...
9
by: zxo102 | last post by:
Hi everyone, I am using a python socket server to collect data from a socket client and then control a image location ( wxpython) with the data, i.e. moving the image around in the wxpython frame....
5
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a...
4
by: SpreadTooThin | last post by:
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')
0
by: =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= | last post by:
Hello, First of all I wish you a good day. My help request is about .NET asynchrounus socket communication. I have developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I...
2
by: Dave Dean | last post by:
Hi all, I'm just starting out in sockets/network programming, and I have a very basic question...what are the 'security' implications of opening up a socket? For example, suppose I've written a...
6
by: 7stud | last post by:
My question pertains to this example: #!/usr/bin/env python import socket, sys, time host = sys.argv textport = sys.argv s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
11
by: Krzysztof Retel | last post by:
Hi guys, I am struggling writing fast UDP server. It has to handle around 10000 UDP packets per second. I started building that with non blocking socket and threads. Unfortunately my approach...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.