Sockets on Unix

A

Anonymous

Hi Folks,

Perhaps someone knows the answer to this. I wrote a small socket app on
fedora linux that simply loops and sends data until the connection is lost
(by the remote peer). I look for a lost connection by waiting for send to
return -1 or 0; however, what seems to be happening is that the application
crashes (no core file or anything) before I get a -1. Some sample code ...
if I connect and disconnect before the 3 seconds is up (note the sleep(3)),
then the output is something along the lines of :

**OUTPUT***
send 1: 2
errno 1: 0

If I connect and wait to receive the data then the output is:

***OUTPUT***
send 1: 2
errno 1: 0
send 2: 2
errno 2: 0
Hello world!

So it looks like the program is crashing on the second send in the first
scenario; however, I have no core file or any indication that it's anything
other than the end of the program. Could someone help?

***CODE***
int main(int argc, char *argv[]) {

// open

int fd = socket(AF_INET, SOCK_STREAM, 0);



// bind

struct sockaddr_in address;

memset(&address, 0, sizeof(address));

address.sin_family=AF_INET;

address.sin_port=htons(1025);

address.sin_addr.s_addr=htonl(INADDR_ANY);

bind(fd, (struct sockaddr*)&address, sizeof(address));



//listen

listen(fd, 16);



// accept

unsigned int n = sizeof(sockaddr);

sockaddr_in clientAddress;

int cfd = accept(fd, (sockaddr*)&clientAddress, &n);



sleep(3);



char buffer[512];

cout << "send 1: " << send(cfd, buffer, 2, 0) << endl;

cout << "errno 1: " << errno << endl;

cout << "send 2: " << send(cfd, buffer, 2, 0) << endl;

cout << "errno 2: " << errno << endl;



cout << "Hello world!\n";



close(fd);



return 0;

}
 
T

Thomas J. Gritzan

Anonymous said:
Hi Folks,

Perhaps someone knows the answer to this. I wrote a small socket app on
fedora linux that simply loops and sends data until the connection is lost
(by the remote peer). I look for a lost connection by waiting for send to
return -1 or 0; however, what seems to be happening is that the application
crashes (no core file or anything) before I get a -1.
[...]

A crash? What a crash? A segfault? A bluescreen?

However, nothing of this has to do with C++. Read here:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9
 
A

Anonymous

To be honest, I'm not sure. From my perspective I see the program running
and then, all of a sudden, I see the command prompt. When I have a seg
fault I normally see "SEGMENTATION FAULT" before seeing the command prompt
again, but here it just drops to the command prompt, just as it does in the
second case, except I get the additional printouts before it does.


Thomas J. Gritzan said:
Anonymous said:
Hi Folks,

Perhaps someone knows the answer to this. I wrote a small socket app on
fedora linux that simply loops and sends data until the connection is
lost
(by the remote peer). I look for a lost connection by waiting for send
to
return -1 or 0; however, what seems to be happening is that the
application
crashes (no core file or anything) before I get a -1.
[...]

A crash? What a crash? A segfault? A bluescreen?

However, nothing of this has to do with C++. Read here:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9
 
L

Larry Smith

Anonymous said:
Hi Folks,

Perhaps someone knows the answer to this. I wrote a small socket app on
fedora linux that simply loops and sends data until the connection is lost
(by the remote peer). I look for a lost connection by waiting for send to
return -1 or 0; however, what seems to be happening is that the application
crashes (no core file or anything) before I get a -1. Some sample code ...
if I connect and disconnect before the 3 seconds is up (note the sleep(3)),
then the output is something along the lines of :

**OUTPUT***
send 1: 2
errno 1: 0

If I connect and wait to receive the data then the output is:

***OUTPUT***
send 1: 2
errno 1: 0
send 2: 2
errno 2: 0
Hello world!

So it looks like the program is crashing on the second send in the first
scenario; however, I have no core file or any indication that it's anything
other than the end of the program. Could someone help?

***CODE***
int main(int argc, char *argv[]) {

// open

int fd = socket(AF_INET, SOCK_STREAM, 0);



// bind

struct sockaddr_in address;

memset(&address, 0, sizeof(address));

address.sin_family=AF_INET;

address.sin_port=htons(1025);

address.sin_addr.s_addr=htonl(INADDR_ANY);

bind(fd, (struct sockaddr*)&address, sizeof(address));



//listen

listen(fd, 16);



// accept

unsigned int n = sizeof(sockaddr);

sockaddr_in clientAddress;

int cfd = accept(fd, (sockaddr*)&clientAddress, &n);



sleep(3);



char buffer[512];

cout << "send 1: " << send(cfd, buffer, 2, 0) << endl;

cout << "errno 1: " << errno << endl;

cout << "send 2: " << send(cfd, buffer, 2, 0) << endl;

cout << "errno 2: " << errno << endl;



cout << "Hello world!\n";



close(fd);



return 0;

}

Try one of these newsgroups:

comp.os.linux.development.apps
comp.os.linux.development.system
 
M

Mehturt

Anonymous said:
To be honest, I'm not sure. From my perspective I see the program running
and then, all of a sudden, I see the command prompt. When I have a seg
fault I normally see "SEGMENTATION FAULT" before seeing the command prompt
again, but here it just drops to the command prompt, just as it does in the
second case, except I get the additional printouts before it does.

You can try to use http://nnl.sf.net
m.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top