UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » receiving packages with \r\n in it

#1 2007-10-25 11:05 AM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: receiving packages with \r\n in it

fd:5 <<
BLP 7 BL                           <-- that line is another package !!
Hotmail 1250
MIME-Version: 1.0
Content-Type: text/x-msmsgsprofile; charset=UTF-8
LoginTime: 1193306538
EmailEnabled: 1
MemberIdHigh: 245757
MemberIdLow: -2014386164
lang_preference: 1043
preferredEmail: 
country: NL
PostalCode: 
Gender: 
Kid: 0
Age: 
BDayPre: 
Birthday: 
Wallet: 
Flags: 1073743427
sid: 72652
MSPAuth: EwBoAswbAQAUs1/VcBU2sH7mwYy3BysWZ71CRDGAAF/cwqmXwR5aalkSR5YFj3aDtKTpd1fdMUbFO+OVksQUrWE3w1NpHenixElqyBJd9+yadOrLvGs+xWqh3KgjoL7yUfqJS0yC8DxJ7krYjgczhI8mogwnFIN74W+JIpSXAeO4g/XTRqi7L6lv9qF7iMAp0gpyU1hOD1V/rDSAVhQbA2YAAAjNF5+b8t5FYbgBVeZVa5JXCPOUdpLnwCOamyY5+AWBKHyhMj8DVT/5bzmJOUVR/96PTaLPr4Ctu7gnOXCobIHugiPvvXp8j5yQZFy6O+wB0XPzMtVLSyPYaO5lmtG2/b0VqLEPdUIps/Upd/ylQ0GvpvpabNgbOTxKQnr3clvB/tRcvxuQEc2IShe4+rvoCX/ke5n5ijwk7QcmpDoGhA677I5BbjJGPer/1XAdTYr8bEEck9lsIcW8V5CEgINO26OnkG8SW7UDlLicOE+RtLOQJwDaQuBnRQcM/xr+L1/H7DFSToZxG4y0B0ao12BcDmn9Gow0BKKigyXNZzNbqVN75T3/9lZTOxUVtZa5x1Lj4CXWgpKRcL7fMGS3C76quR7j2q/6pWRc7tNkGoY/nWITGnpoEgh5khCl5XAjvxtVurv7m11D//QXlr2jj7TH7DITwHw8+gBxBXnELsH8Ky8nbTxW2Ngj+mayRMI4+hX/o/ImPrbKe+YTwPxkN48XaFKyRfjMAT6C6PrYvWIMNlP/94Ka+/X2IFZ6f3o3SU/ryheghB/UXcMchDLESW/HKozQ5bqX+lhwJDm7y0m/MKVVWlPFAQ==&amp;p=
ClientIP: 80.255.240.1
ClientPort: 16022
ABCHMigrated: 1
Nickname: abc
MPOPEnabled: 0

Offline

#2 2007-10-25 01:41 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: receiving packages with \r\n in it

For one thing, I'm going to guess you're not adding a terminating null character at
the end of your receive buffer (based on the received length returned by that delim
function)...  You need to manually add the null, if you need to treat it as a null-terminated
string (which printf(%s) requires); that function doesn't do it for you...

For another thing, you really should JUST look for "\n" as the delimiter, rather than
strictly requiring "\r\n"...  Looking for just "\n" will work in both cases: a strictly
conforming peer who sends "\r\n", or a loose peer who sends only "\n"...  You should
always send "\r\n" yourself, but don't be so strict as to require everyone else to send
it...  You are likely to eventually run into loosely conforming implementations which
only send "\n"...

Offline

#3 2007-10-25 01:53 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: receiving packages with \r\n in it

Well. I've added a strcat with the \0 char. and things are still running trogh each other. :(

Offline

#4 2007-10-25 01:58 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: receiving packages with \r\n in it

strcat()??  No, no, no...  strcat() requires the destination ALREADY be null-terminated!

The proper thing to do is use the return value from the delim receiving function as
an index to stuff a null char at...  Eg:

len = sock_read_delim (sockfd, buf, sizeof (buf) - 1, "\n");
if (len <= 0) /* die or do whatever... */
buf[len] = '\0';

Offline

#5 2007-10-25 02:05 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: receiving packages with \r\n in it

Offline

#6 2007-10-25 02:23 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: receiving packages with \r\n in it

Sure, change the function to add the null before it returns...  But, be careful not to
overflow the buffer...  You'll want to have it leave room so it can add the null char
at the end...  Or, if you don't mind trashing the delim, you can have it simply overwrite
the delim with null when it finds it...

Offline

#7 2007-10-25 02:35 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: receiving packages with \r\n in it

yeah, but my compiler doesn't like it. Because it is a void. the buf var. But when i change it to a char, it isn't okay too .

Offline

#8 2007-10-25 09:26 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: receiving packages with \r\n in it

Well, that's what casting is for...  No need to change the parameter to really be
char*...

Or, better yet, just use the existing "bufp" char* pointer you already have, and
which is already pointing at the end of received data inside the buffer, at the point
you break out of the loop and are about to return:

*bufp = '\0';

But, again, don't just blindly throw this change in, or you risk writing off the end of
your buffer by a byte...  Unless you always pass in a buffer size of 1 less than the
true size, anyway...  Otherwise, you'll want to modify the function to assume it's got
one less byte for data storage than the passed size, instead...  (Simplest way: just
do "bufsz--;" at the top somewhere...)

Offline

#9 2007-10-28 02:14 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: receiving packages with \r\n in it

oh, forgotten to place a reply here, but Yeah! It works!

Offline

  • Index
  • » C
  • » receiving packages with \r\n in it

Board footer

Powered by FluxBB