Socket reading problem

M

Matta

Hi,

I'm writing an NNTP based program that downloads articles from Usenet.
I've run into a weird problem. It seems as if there is a bug somewhere
that causes certain articles to download incorrectly (just by a byte or
two). It happens to the same articles and about 1 out of 5 have issues. So
everything seems to work ok, but somewhere something causes this weird
behavior.

The code that should work but doesn't (sometimes) downloads an article and
returns it.

Any help would be appreciated.

Matta

- - - - -

string NNTP::net_recv()
{
int numbytes;
string buffer;
char buf[MAXDATASIZE];

numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0);

if (numbytes < 1) {
return "";
}

buf[numbytes] = '\0';

buffer = buf;
return buffer;
}

extern "C" string NNTP::get_article(string msgid)
{
string rdata, sendstr, tmp;
string::size_type pos;

sendstr = "ARTICLE ";
sendstr += msgid;
sendstr += "\r\n";

net_send(sendstr);

rdata = "";
do {
rdata += net_recv();
} while (rdata.substr(rdata.length()-5, 5) != "\r\n.\r\n");

rdata = rdata.substr(0, rdata.length() - 3);

pos = rdata.find("\r\n\r\n", 0);
rdata = rdata.substr(pos + 4, rdata.length() - (pos + 4));

if (rdata.substr(0, 2) == "..") {
rdata.erase(0, 1);
}

pos = 0;
while ((pos = rdata.find("\r\n..", pos)) != string::npos) {
rdata.erase(pos + 2, 1);
}

return rdata;
}
 
M

Mark A. Odell

I'm writing an NNTP based program that downloads articles from Usenet.
I've run into a weird problem. It seems as if there is a bug somewhere
that causes certain articles to download incorrectly (just by a byte or
two). It happens to the same articles and about 1 out of 5 have issues.
So everything seems to work ok, but somewhere something causes this
weird behavior.

The code that should work but doesn't (sometimes) downloads an article
and returns it.

Any help would be appreciated.

Matta

- - - - -

string NNTP::net_recv()
{

This is C++ code, not C code, and it's a networking question (probably)
which is outside the scope of comp.lang.c++. Regardless, it's off-topic
here.
 
R

Richard Tobin

Your program appears to be in C++, not C. And you'll probably also
be flamed for asking about code that uses system-specific socket
functions.

As a general debugging hint, I suggest you start by separating out the
socket code from the text processing code. Put one of the failing
articles in a file and read it from there, so you don't have to worry
about the network aspect.

-- Richard
 
C

CBFalconer

Matta said:
.... snip ...

The code that should work but doesn't (sometimes) downloads an
article and returns it.

The code you posted is in C++ (which is OffTopic here) and also
contains calls to non-standard C functions. You need a newsgroup
that deals with your particular system. Here we deal only with
portable C compliant with the ISO standard.
 

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,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top