473,804 Members | 3,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading from sockets

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(s ocket.AF_INET,s ocket.SOCK_STRE AM)
s.connect( ("localhost",54 321) );
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

Aug 10 '06 #1
8 2246
AndrewTK wrote:
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(s ocket.AF_INET,s ocket.SOCK_STRE AM)
s.connect( ("localhost",54 321) );
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

Aug 10 '06 #2
AndrewTK wrote:
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(s ocket.AF_INET,s ocket.SOCK_STRE AM)
s.connect( ("localhost",54 321) );
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.
Aug 11 '06 #3
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/pe...ingServer.java

It launches on the command line with
java RespondingServe r _port_

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

Aug 12 '06 #4
Follow up the actual python code is at
http://www.dcs.st-and.ac.uk/~atk1/singleclient.py

Aug 12 '06 #5
AndrewTK wrote:
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/pe...ingServer.java

It launches on the command line with
java RespondingServe r _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

Aug 12 '06 #6
Simon Forman wrote:
So I'm guessing it's something wrong in your java server.
Thanks then. I'll keep testing then...

Aug 13 '06 #7
Simon Forman wrote:
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.....?

Aug 13 '06 #8
AndrewTK wrote:
Simon Forman wrote:
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.....?
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

Aug 13 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
5989
by: javastudent | last post by:
Hi, I am implementing a server that reads for socket connections at a port, and processes the socket on a separate thread. We do not control the client implementation. Here is the scenario. 0. Client connects to the socket. 1. Client writes to the socket. 2. Server reads from the socket. 3. Client waits for the response on the socket
21
13110
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the server is on one line (\r\n at end) and is formed as - each of which is seperated by a space. Arguments with spaces in them are enclosed in quotations. So, I'm able to open a connection to the server. When I send a message to
4
6325
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
3
4364
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly found out that .net uses system.net.sockets.socket or .tcpclient/.tcpserver. and these confuse me... :o| and help would be really great! links, code, wrappers, tips, whateveryougot... one of my main problems, i guess, is: why don't sockets...
9
3609
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so that its buffer is bigger than this. I did this expecting the data to be read in one pass.
0
983
by: =?Utf-8?B?R3JlZw==?= | last post by:
I am trying to read text from a Telnet site melvyl.ucop.edu. I run the following VB .NET 2003 code: Dim nstc As New Net.Sockets.TcpClient("melvyl.ucop.edu", 23) Dim nsns As Net.Sockets.NetworkStream = nstc.GetStream() If nsns.DataAvailable Then Dim ab(nstc.ReceiveBufferSize) As Byte nsns.Read(ab, 0, CInt(nstc.ReceiveBufferSize))
2
1004
by: Benoit | last post by:
I got myself into programming late in the summer and have dabbled in python for the most part in that time, recently beginning work on a music player. In January, I start my minor in Information Technology. I'd like to get ahead a bit, however, and over the break would like to do some reading. I seek your recommendations in the field of database design basics and network programming, with a bias towards texts which specifically address...
0
1096
by: Bruno Rafael Moreira de Barros | last post by:
Hey there. I have a small problem with sockets. The code is really simple, but it still fails. The problem is I am accessing a .rar file (for those who don't know it's an archive, like .zip) through fsockopen and I'm only getting the headers... The file is not coming. # Open the host $fp = fsockopen($url, 80, $errno, $errstr, 30); # Ask for the file. $method is GET and $GET is the path and filename
10
2116
by: puzzlecracker | last post by:
Say I want to arrange bytes in the internal buffer in a certain way. I receive those bytes in the socket. One solution is to read in socket in pieces: byte buffer = new byte; int index = 0; m_socket.Receive(buffer , 8, SocketFlags.None); index += 8; //read the rest of the message
0
9587
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.