Sockets
Hugo Martires
hugomartires at hotmail.com
Thu Oct 11 09:59:15 EDT 2001
Peter Hansen <peter at engcorp.com> wrote in message news:<3BC50853.505C8641 at engcorp.com>...
> Hugo Martires wrote:
> >
> > My server need to send 2 strings separeted:
> > s1= 'xxx'
> > s2= 'yyy'
> >
> > My client must received like this:
> > rec1= 'xxx'
> > rec2= 'yyy'
> >
> > The problem is that the Client received s1 and s2 in one only string.
> >
> > How can i received in 2 separeted variables ?
>
> I note the other answers, and still feel the need to point out two
> things.
>
> 1. When asking questions of this sort, it is *always* advisable
> to post sample code showing what you are trying to do.
> If you don't, the answers are just guesses (maybe educated ones,
> but still guesses).
>
> 2. Assuming you are opening a socket between the two programs,
> my (educated :-) guess is that you are doing a pair of socket.send()
> calls with the two strings, and expecting these two be
> received *separately* with two recv() calls on the other end.
> If this is so, you misunderstand how sockets and TCP/IP and
> such work. Nowhere in the documentation will you find any
> guarantee that the data from two consecutive send() calls
> matches up with the two recv() calls. They might be lumped
> together so that you get 'xxxyyy' after the first recv().
> You might have to use four recv() calls and get 'x', 'x',
> 'xxy' and finally 'yy'. No guarantees. Don't ever assume
> you have all the data just because recv() returns.
>
> And that's the reason behind John's suggestion of disabling
> the Nagel algorithm, since it *might* appear to solve the
> problem. Don't be fooled: it's a very fragile, hackish
> way of "fixing" the problem, and not suitable for "real"
> programs in most cases. (Standard anti-flame disclaimers
> apply...)
///////////////////////////////////////////////////////
OK there's my code:
*----------- Server ----------------------*
#!/usr/bin/env python
from socket import *
from cga_dp import *
# input
n=10
s=4
g=10
param=[n , s , g]
HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
vp=get_vp()
SerSock = socket(AF_INET, SOCK_STREAM)
SerSock.bind(ADDR)
SerSock.listen(5)
while(1):
CliSock, addr = SerSock.accept()
CliSock.send(repr(param))
CliSock.send(vp)
CliSock.close()
SerSock.close()
*----------- Client -------------------*
#!/usr/bin/env python
from socket import *
HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
CliSock = socket(AF_INET, SOCK_STREAM)
CliSock.connect(ADDR)
while(1):
data = CliSock.recv(BUFSIZ)
if not data: break
???????????????????
CliSock.close()
******************************
My problem is : what i put in ???????????????
Some people told me to use a delimeter betwen the 2 strings.
But how can i use it? How to decode the whole string ?
Tanks
More information about the Python-list
mailing list