2

My objective is every time the client send the data to the server, the server will display it. But Currently when the client send the data, the server capture the data and display it only for the first time. What is causing the problem? Btw, this is my first time doing network programming, so pls keep your answer simple.

Server Script

import socket
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_socket.bine(("",5001))
server_socket.listen(5)
print "TCP Server Waiting for incoming client connection on port 5001..."
while True:
 client_socket, address =server_socket.accept()
 print "Connection from ", address
 data = client_socket.recv(512)
 print "RECIEVED:" , data

Client Script

import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('169.254.0.1', 5001))
data=''
while data!='quit':
 data = raw_input ( "SEND :" )
 client_socket.send(data)
asked Aug 21, 2013 at 9:21

2 Answers 2

2

Your server code receive only once, then accept another client. You should loop until client disconnect. (when disconnected, recv() return empty string)

while True:
 client_socket, address = server_socket.accept()
 print "Connection from ", address
 while 1:
 data = client_socket.recv(512)
 if not data:
 break
 print "RECIEVED:" , data

BTW, your server code (and my code) does handle only one client at a time.

answered Aug 21, 2013 at 9:25
Sign up to request clarification or add additional context in comments.

Comments

1
while True:
 client_socket, address =server_socket.accept()
 print "Connection from ", address
 data = client_socket.recv(512)
 print "RECIEVED:" , data

This should be:

client_socket, address =server_socket.accept()
print "Connection from ", address
while True:
 data = client_socket.recv(512)
 print "RECIEVED:" , data

server_socket.accept() will wait indefinitely until a new client is connected. Actually your loop is like : "accept a client and receive one time the data he sent".

answered Aug 21, 2013 at 9:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.