469,117 Members | 1,348 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,117 developers. It's quick & easy.

socket programming

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


----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
Jul 18 '05 #1
3 1775
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.

--
René Pijlman
Jul 18 '05 #2
Rene Pijlman wrote:
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.
Jul 18 '05 #3
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.
--
--Bryan
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by pyguy2 | last post: by
5 posts views Thread by John Sheppard | last post: by
1 post views Thread by John Sheppard | last post: by
2 posts views Thread by djc | last post: by
11 posts views Thread by atlaste | last post: by
8 posts views Thread by =?Utf-8?B?Sm9obg==?= | last post: by
3 posts views Thread by Stuart | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.