LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2004, 08:07 AM   #1
Ohmu
LQ Newbie
 
Registered: Jan 2004
Location: Estonia
Distribution: Gentoo
Posts: 20

Rep: Reputation: 0
Receiving all data from socket


Hi!

I tried to receive all data from socket like:
1) Receive the first portion of data
2) Set socket non-blocking
3) Receive data until it gives error 11 (no more data)

But that gives strange characters in the result...
Is there some better(100% working) way to receive all data?

Thank's,
Ohmu
 
Old 03-08-2004, 08:27 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
are you sure those strange characters aren't actually being recieved?
 
Old 03-08-2004, 08:37 AM   #3
Ohmu
LQ Newbie
 
Registered: Jan 2004
Location: Estonia
Distribution: Gentoo
Posts: 20

Original Poster
Rep: Reputation: 0
nope... I am 100% sure of that...
 
Old 03-08-2004, 10:41 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
What language are you using?

And are you checking how many bytes you receive with each block of data you receive, and making sure to use ONLY that number of bytes? For instance in c, you could end up doing something like this:

Code:
char buffer[256];

recv(sock, buffer, 256, 0);

printf("%s\n", buffer);


And if you actually only recv 5 bytes the first 5 bytes will be what you received and the last of them would be garbage and probably displayed if the data wasn't sent as a null-terminated string. (e.g. if the sending app sent "hello" instead of "hello\0" your buffer would end up having so mething like "hellowhatevergarbagewasherebeforethereceiveetc...").

Instead, you would do something like so:

Code:
char buffer[256];
int n;

n = recv(sock, buffer, 256, 0);

// NULL terminate the string
buffer[n] = '\0';

printf("%s\n", buffer);
Obviously, if you are receiving binary data, you probably wouldn't be null terminating it like that, but you should still be checking how many bytes are received and only operating on that number of bytes.

Last edited by deiussum; 03-08-2004 at 10:45 AM.
 
Old 03-08-2004, 10:49 AM   #5
Ohmu
LQ Newbie
 
Registered: Jan 2004
Location: Estonia
Distribution: Gentoo
Posts: 20

Original Poster
Rep: Reputation: 0
well.. I fill the array of chars with \0 and then recv data.. but the strange characters aren't at the end only.. they are in the middle too

Last edited by Ohmu; 03-08-2004 at 10:50 AM.
 
Old 03-08-2004, 01:45 PM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Hmmm... showing us a little bit of your code might be useful.
 
Old 03-08-2004, 01:58 PM   #7
Ohmu
LQ Newbie
 
Registered: Jan 2004
Location: Estonia
Distribution: Gentoo
Posts: 20

Original Poster
Rep: Reputation: 0
OK.. got it working.. ty all for help

Last edited by Ohmu; 03-09-2004 at 12:34 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Not receiving data from serial modem. ejoe Linux - Hardware 18 04-01-2007 12:35 PM
receiving unwanted data from internet mohtasham1983 Linux - General 2 11-28-2005 12:37 PM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
modem stops receiving data positron Linux - Newbie 3 07-21-2004 03:32 AM
closed computer receiving data cristi1979 Linux - Security 2 07-11-2003 12:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration