sockets 
Author Message
 sockets

Hi

I am building a `proof of concept' for my final thesis. This involves
some socket-programming. I was new to this subject so I read some man
pages and the relevant chapters from "Programming Python" (2nd edition).
Here's what I came up with (in interactive mode):

    >>> from socket import *
    >>> sockobj = socket(AF_INET,SOCK_STREAM)
    >>> sockobj.connect( ("kubsuw.kub.nl",7124) )
    >>> sockobj.recv(1024)
    'Welcome to the Tagdemo server.\n'
    >>> sockobj.send("Dit is een test .\n")
    18
    >>> data = sockobj.recv(1024)
    >>> print data
    Dit//NNP is/VBZ een//RB test/VB ./.

Since this worked fine I moved it to a script:

    from socket import *

    sockobj = socket(AF_INET,SOCK_STREAM)
    sockobj.connect( ("kubsuw.kub.nl",7124) )

    # the server sends a welcome-mesg
    sockobj.recv(1024)

    # send a test-line to the tagger
    # receive the data and print it
    sockobj.send("Dit is een test .\\n")
    data = sockobj.recv(4096)
    print data

    # close the socket
    sockobj.close()
    print "end"

However, that didn't word:


    Welcome to the Tagdemo server.

    Dit
    end

I found this rather odd.... I asked around with my collegues but they
couldn't help out. Anyone on the list have a suggestion?

many thanx in advance

    Bas

--

Build a system that even a fool can use, and only a fool will want to use it.

  application_pgp-signature_part
< 1K Download


Mon, 21 Jun 2004 18:44:09 GMT  
 sockets


Quote:
> I am building a `proof of concept' for my final thesis. This involves
> some socket-programming. I was new to this subject so I read some man
> pages and the relevant chapters from "Programming Python" (2nd edition).
> Here's what I came up with (in interactive mode):

>     >>> from socket import *
>     >>> sockobj = socket(AF_INET,SOCK_STREAM)
>     >>> sockobj.connect( ("kubsuw.kub.nl",7124) )
>     >>> sockobj.recv(1024)
>     'Welcome to the Tagdemo server.\n'
>     >>> sockobj.send("Dit is een test .\n")
>     18
>     >>> data = sockobj.recv(1024)
>     >>> print data
>     Dit//NNP is/VBZ een//RB test/VB ./.

> Since this worked fine I moved it to a script:

>     from socket import *

>     sockobj = socket(AF_INET,SOCK_STREAM)
>     sockobj.connect( ("kubsuw.kub.nl",7124) )

>     # the server sends a welcome-mesg
>     sockobj.recv(1024)

>     # send a test-line to the tagger
>     # receive the data and print it
>     sockobj.send("Dit is een test .\\n")

thats not the same as above: look at "\n"

Quote:
>     data = sockobj.recv(4096)

this receives up to 4096 bytes but it can also be only one.if you wanna be
sure that you have the entire line, receive in a loop util you get "\n".
you could also read until the connection is terminated from the server or
use a simple protocol.

- Show quoted text -

Quote:
>     print data

>     # close the socket
>     sockobj.close()
>     print "end"

> However, that didn't word:


>     Welcome to the Tagdemo server.

>     Dit
>     end

> I found this rather odd.... I asked around with my collegues but they
> couldn't help out. Anyone on the list have a suggestion?

> many thanx in advance

>     Bas

--



Mon, 21 Jun 2004 21:12:54 GMT  
 sockets


[text ended up as a text-attachment so it's hard to
reply to it.  Anyway...]:

Bas did, interactively:

    >>> sockobj.send("Dit is een test .\n")
    18
    >>> data = sockobj.recv(1024)
    >>> print data
    Dit//NNP is/VBZ een//RB test/VB ./.

here, as he manually entered the "data = " &c statement,
time passed, so no doubt the server had time to send
the whole response.  Then, he tried to put this in a
script:

    sockobj.send("Dit is een test .\\n")
    data = sockobj.recv(4096)
    print data

but then data was only bound to:

    Welcome to the Tagdemo server.

    Dit

where the first part is something the server sent
"earlier".

Finally, Bas asks:

"""
I found this rather odd.... I asked around with my collegues but they
couldn't help out. Anyone on the list have a suggestion?
"""

Remember socket-level network interactions take time, and
the stream is sent in arbitrary chunks.  You just cannot
"bet" that a single recv call will give you all that you
expect has been sent up to that point: if you make your
recv call fast enough you may get one byte or two or 20,
with more still to come -- and nothing in TCP/IP per se
tells you HOW MUCH is "still to come".

When you use module socket interactively, you insert human
delays which slow things down to where such problems very
rarely show up (unless the server is horribly busy, the line
near-dead, etc:-).

You need to be able to "parse" the data you're receiving
in order to know when a "meaningful unit" of such data is
done.  For example, you may want to "receive up to the
first \n", knowing that a '\n' IS going to come.  Then, e.g.:

def getlinefrom(sockobj):
    pieces = []
    while 1:
        piece = sockobj.recv(1024)
        pieces.append(piece)
        if piece.count('\n'): break
    return ''.join(pieces)

This is very rough, but, I hope, still a useful pointer.
Either the data has a terminating-indication, or before
the data comes some header telling how big the data is
going to be.  You also need to handle and gracefully deal
with exceptional situations such as the line going down
in mid-stream, ending the data prematurely.

Alex



Mon, 21 Jun 2004 21:10:36 GMT  
 sockets

Quote:
> Since this worked fine I moved it to a script:

>     from socket import *

>     sockobj = socket(AF_INET,SOCK_STREAM)
>     sockobj.connect( ("kubsuw.kub.nl",7124) )

>     # the server sends a welcome-mesg
>     sockobj.recv(1024)

>     # send a test-line to the tagger
>     # receive the data and print it
>     sockobj.send("Dit is een test .\\n")
>     data = sockobj.recv(4096)
>     print data

>     # close the socket
>     sockobj.close()
>     print "end"

> However, that didn't word:


>     Welcome to the Tagdemo server.

>     Dit
>     end

What's happening is probably: the server is sending the data
in chunks, and you're calling recv() before all of the output
has been received.

You might want to try:

  # receive the data and print it
  sockfile = sockobj.makefile('r')
  data = sockfile.readline()
  print data

The "readline()" method waits for the whole line of data
to arrive.  If that's not good enough, you can write a simple
loop yourself, to gather data until you have all the data you
want.

## Jason Orendorff    http://www.jorendorff.com/



Tue, 22 Jun 2004 01:04:38 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. socket problems under windows: cannot load Socket.so under windows

2. SSL support for socket module for server sockets

3. Q: simple sockets, bind, and socket error 10048

4. NT Tcl socket vs. UNIX Tcl socket

5. Socket Help "Socket Newbie"

6. UDP sockets w/ db-sockets

7. Socket connection fails with socket already connected message

8. TCL and Socket TO Socket

9. old thread: SSL support for socket module for server sockets

10. SOCKET:SOCKET-STREAM missing?

11. VW/Sockets (detecting abnormally closed socket)

12. socket question:socket.error:(4,'Interrupted system call')

 

 
Powered by phpBB® Forum Software