socket programming

A

Ajay

hi!

on my client side, i have a socket that sends and then receives
the code is
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((PDA_ADDRESS, PDA_PORT))
s.send("persona=")
s.send(persona)
s.send("\n")
str = s.recv(1024)
data=""
while len(str) != 0:
data += str
str = s.recv(1024)
print data

on my server side i create the serversocket, listen, get the client socket
and the client socket receives the data as follows
(clientsocket, address) = serversocket.accept()
str = clientsocket.recv(256)
data=""
while len(str) != 0:
data += str
str = clientsocket.recv(256)

the problem is when my client finished sending and waits to receive, on the
server side, it still stays in the while loop waiting to receive more data.

how do i stop that?

thanks
 
R

Rene Pijlman

Ajay:
the problem is when my client finished sending and waits to receive, on the
server side, it still stays in the while loop waiting to receive more data.

how do i stop that?

You need to define a protocol for the communication between client and
server, and implement the server in such a way that it sends what it's
expected to send, when it has received what it expected to receive. In
your current code, the server is expecting an infinite amount of data,
while the client is sending only a few bytes.
 
B

Brad Tilley

Rene said:
You need to define a protocol for the communication between client and
server, and implement the server in such a way that it sends what it's
expected to send, when it has received what it expected to receive. In
your current code, the server is expecting an infinite amount of data,
while the client is sending only a few bytes.

You could define a special char that indicated the end of the transmission.
 
B

Bryan Olson

Ajay wrote:
[...]
the code is
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) [...]
the problem is when my client finished sending and waits to receive, on the
server side, it still stays in the while loop waiting to receive more data.

SOCK_STREAM sockets have no concept record-boundary. If you
need to convey the end of one message, but keep the connection
open, then you need to define or find a protocol that builds
distinct messages on top of the byte stream.

If the client is truly done sending on that socket, it can call
s.shutdown(1). Then, after the server has received all the sent
data, (the socket will select as readable and) the server's next
recv will return, but the size of the read will be zero bytes.

Remote shutdown of sending is the only case in which recv will
succeed with a zero size. If remote side has not shut down, and
there is no data available, a recv on a blocking sockets will
block, and non-blocking sockets will raise/return EWOULDBLOCK.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top