socket.recv Not Getting All Data Sent

This is the place for queries that don't fit in any of the other categories.

socket.recv Not Getting All Data Sent

Postby jogl8 » Mon Feb 18, 2013 7:42 pm

I have this code in a little data collection prog that I am monkeying around with:
Code: Select all
#Now receive data

reply = s.recv(65536)

print reply


The Arduino sending is sending this:
Code: Select all
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
 
          // output the value of each analog input pin
 
            int sensread = analogRead(3);
 
            client.print(sensread);
            client.print("  temp is   ");           
            client.print(Temperature(sensread));           
            client.println("<br />");     

          client.println("</html>");
          break;


Everything seems to be working but I am only receiving the first two lines that the Arduino sends, namely:
Code: Select all
HTTP/1.1 200 OK
Content-Type: text/html


It is like the s.receive is not waiting long enough or is stopping after a CR or something?

Any ideas?

Thanks,
JP.
jogl8
 
Posts: 31
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: socket.recv Not Getting All Data Sent

Postby stranac » Mon Feb 18, 2013 8:01 pm

The argument to s.recv() is a maximum length to read.
It is not guaranteed to read all the data.

http://linux.die.net/man/2/recv wrote:The receive calls normally return any data available, up to the requested amount, rather than waiting for receipt of the full amount requested.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1115
Joined: Thu Feb 07, 2013 3:42 pm

Re: socket.recv Not Getting All Data Sent

Postby jogl8 » Mon Feb 18, 2013 10:36 pm

I tried 4096, 9192 and 65,536 in the s.recv function. Results were always the same so I figured something else was going on. Is there a different function I should use that would wait for a specific condition to assume that all data had been received.

The Arduino code works perfect when I enter the IP address into any browser.

Thanks,
JP.
jogl8
 
Posts: 31
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: socket.recv Not Getting All Data Sent

Postby stranac » Mon Feb 18, 2013 10:49 pm

No, there isn't such a function, but you can create one(a while loop and some checks should do).

In general, the socket module is really awkward to work with, especially if you're gonna be doing something more complicated.
There are a lot of common concepts sockets just don't understand.
This is one of the reasons I suggested using twisted in one of your earlier topics.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1115
Joined: Thu Feb 07, 2013 3:42 pm

Re: socket.recv Not Getting All Data Sent

Postby jogl8 » Mon Feb 18, 2013 10:59 pm

OK, I'll check out Twisted again.

I'm a binder twine and crazy glue simple kind of guy, so I was hoping for an, at least, fairly simple copy / paste solution. Guess I will have to put in a little more effort!

Thanks Stranic,
JP.
jogl8
 
Posts: 31
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: socket.recv Not Getting All Data Sent

Postby mouseroot » Tue Feb 19, 2013 12:55 am

you need to keep reading untill the end
also you may want to close the socket on the audrino
after all the data is sent(I believe its part of the http spec...could be wrong tho)

Code: Select all
all_data = ""
running = 1
while running:
    data = socket.recv(1024) # read 1024 byte chunks
        while data:
            all_data += data
    print all_data
mouseroot
 
Posts: 17
Joined: Sat Feb 16, 2013 1:14 am

Re: socket.recv Not Getting All Data Sent

Postby jogl8 » Tue Feb 19, 2013 4:17 pm

I lazied out of the problem by assembling all of the data that I want to send into a string and sending it in one print statement from the Arduino.

Now I will just have to deconstruct it in Python.

JP.
jogl8
 
Posts: 31
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 3 guests

cron