reading from sockets

A

AndrewTK

Hello,

I'm trying to read data from a socket and I'm not seeing what I'm
expecting.... it seems to skip the first line of data. I am new to
Python and just trying to test what I can do with it... and it's not
looking pretty.


I have some Python code:
[------------------------------
#! /usr/bin/python

import socket

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect( ("localhost",54321) );
s.send("hello")
data = s.recv(1024)
while len(data) > 0:
print repr(data) # the first print of data always seems to "ignore"
the first line of data...
data = s.recv(1024)
------------------------------]

On localhost 54321 I have a server (in Java) writing out

[------------------------------
first
second
third
<EOF>
------------------------------]
(on EOF, the stream is simply closed.)

The result of the script is to print

[------------------------------
second
third
------------------------------]

I have tried already
[++++++++++++++
data = " "
while len(data) > 0:
data = s.recv(1024)
print repr(data)
++++++++++++++]

but that has not changed anything. The first line to come through is
always skipped it would seem.

Any ideas as to why that would be....? I was thinking scope when I made
the last example, but apparently that is not the case.

Andrew
 
S

Simon Forman

AndrewTK said:
Hello,

I'm trying to read data from a socket and I'm not seeing what I'm
expecting.... it seems to skip the first line of data. I am new to
Python and just trying to test what I can do with it... and it's not
looking pretty.


I have some Python code:
[------------------------------
#! /usr/bin/python

import socket

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect( ("localhost",54321) );
s.send("hello")
data = s.recv(1024)
while len(data) > 0:
print repr(data) # the first print of data always seems to "ignore"
the first line of data...
data = s.recv(1024)
------------------------------]

On localhost 54321 I have a server (in Java) writing out

[------------------------------
first
second
third
<EOF>
------------------------------]
(on EOF, the stream is simply closed.)

The result of the script is to print

[------------------------------
second
third
------------------------------]

I have tried already
[++++++++++++++
data = " "
while len(data) > 0:
data = s.recv(1024)
print repr(data)
++++++++++++++]

but that has not changed anything. The first line to come through is
always skipped it would seem.

Any ideas as to why that would be....? I was thinking scope when I made
the last example, but apparently that is not the case.

Andrew

Hmm.. that's very very strange.

Try putting "print repr(data)" immediately before your while statement
and after the first recv() call to see... well, actually, nevermind
that.

Double check that the first line really is being sent (with netcat or
telnet or something.) That's the only thing I can think of, your code
should be working.. If the first line is really being sent, but not
arriving in your code, you've got something strange going on.

(BTW, "while len(data) > 0:" can just be "while data:")

(Heaven help me, but maybe you could post your java server code here...
If anyone screams too loudly blame me. Hahaha..)

Peace,
~Simon
 
A

AlbaClause

AndrewTK said:
Hello,

I'm trying to read data from a socket and I'm not seeing what I'm
expecting.... it seems to skip the first line of data. I am new to
Python and just trying to test what I can do with it... and it's not
looking pretty.


I have some Python code:
[------------------------------
#! /usr/bin/python

import socket

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect( ("localhost",54321) );
s.send("hello")
data = s.recv(1024)
while len(data) > 0:
print repr(data) # the first print of data always seems to "ignore"
the first line of data...
data = s.recv(1024)
------------------------------]

I'm new to Python too, but I'd have to agree with Simon on this: There's
probably something screwy in your java code.

I'm assuming that your server waits to receive the word 'hello' before
replying with the three strings (first, second, and third)? So once your
script sends the word 'hello', there's nothing for it to do but wait for a
response. And that's exactly what your script does.
 
A

AndrewTK

I'm assuming that your server waits to receive the word 'hello' before
replying with the three strings (first, second, and third)? So once your

Nope - actually it's a threaded "server", with the main thread simply
dumping network input to the console and command line input being
directly dumped to the network output stream.

I confess to having typed the three lines manually...!

It's at:
http://www.dcs.st-and.ac.uk/~atk1/perso/java/servers/RespondingServer.java

It launches on the command line with
java RespondingServer _port_

_port_ being the port number it should listen for data on.
 
S

Simon Forman

AndrewTK said:
Nope - actually it's a threaded "server", with the main thread simply
dumping network input to the console and command line input being
directly dumped to the network output stream.

I confess to having typed the three lines manually...!

It's at:
http://www.dcs.st-and.ac.uk/~atk1/perso/java/servers/RespondingServer.java

It launches on the command line with
java RespondingServer _port_

_port_ being the port number it should listen for data on.

Hooooy. I tried reading your Java code.. What can I say? There's a
reason why I like Python... :)

I couldn't see anything obviously broken in your server, but there's
definitely nothing wrong with your python code, except that you call
flush() on the socket (fixed in your posted code but not in the linked
code.)

Here are the results of testing, with netcat substituted for your java
server:

# on the server side:
sforman@garbage:~ $ netcat -p 54321 -l
hellohi there
how are you
I'm fine thanks

sforman@garbage:~ $



# on the client side:
sforman@garbage:~ $ python delme.py
'hi there\n'
'how are you\n'
"I'm fine thanks\n"
sforman@garbage:~ $


So I'm guessing it's something wrong in your java server.

HTH,
~Simon
 
A

AndrewTK

Simon said:
So I'm guessing it's something wrong in your java server.

Thanks then. I'll keep testing then... Although I don't seem to have
netcat on my unit...

I'm using a uni computer so I can't install stuff... but I'm guessing
what I wrote is something like a basic-basic thingy that does what
netcat is designed to do.....?
 
S

Simon Forman

AndrewTK said:
Thanks then. I'll keep testing then... Although I don't seem to have
netcat on my unit...

I'm using a uni computer so I can't install stuff... but I'm guessing
what I wrote is something like a basic-basic thingy that does what
netcat is designed to do.....?

First link from googling netcat:
http://netcat.sourceforge.net/

Online man page for netcat (a.k.a. nc)
http://www.openbsd.org/cgi-bin/man.cgi?query=nc
(Note, it's the openbsd man page. YMMV)

If you can compile C on your university computer then you should be
able to download, compile, and "install" it to a dir in your homedir.
I've done it, so I know it's possible. :) (I was working on a remote
machine that I didn't have root on, and I needed to test a tcp server I
was writing.)

Netcat's a truly awesome and surprisingly useful little tool. Any
effort you spend to learn it will be well repaid.

Peace,
~Simon
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top