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;
}
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;
}