473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Sockets on Unix

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;

}
Oct 15 '06 #1
4 1678
Anonymous schrieb:
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-lit...t.html#faq-5.9

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 15 '06 #2
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" <Ph*************@gmx.dewrote in message
news:eg**********@newsreader2.netcologne.de...
Anonymous schrieb:
>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-lit...t.html#faq-5.9

--
Thomas
http://www.netmeister.org/news/learn2quote.html

Oct 15 '06 #3
Anonymous wrote:
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
Oct 16 '06 #4

Anonymous wrote:
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.

Oct 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: afalanga | last post by:
Hi, I seem to be unable to connect to my PostgreSQL database over UNIX domain sockets, i.e. pg_connect("dbname=mydb"); No matter what I do to the pg_hba.conf file, or anything else, PHP...
5
by: Fossie | last post by:
Hi folks, I am unable to solve the following problem, which should not be new, but I cannot find an appropriate solution anywhere: One python process ("daemon") waits for messages. Another...
5
by: Virgil Green | last post by:
An associate has asked me to take a look a problem he's having with starting mysql. I thought I'd give it a shot to learn some more about mySQL before having him turn it over to someone more...
4
by: Garam | last post by:
I need to write a substantial amount of network code in C. The thing is, it has to be able to run on both Unix and Windows. Rather than writing separate code for each, I was looking for something...
1
by: Klaus | last post by:
Hello, I did post this message already recently, but do have the impression, that this message never has never been pushed to other news servers. Well.
3
by: John P | last post by:
Hi, I know that some older Unix programs that compile with GCC use socket.h to connect to and execute other programs. However, I am using Visual C++ 6.0 and it doesn't seem to have socket.h...
15
by: kernel.lover | last post by:
Hello, i want to know to have multiple clients connects to same server program does following is correct code #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include...
1
by: Karim | last post by:
Hi, I am working on a c++ project involving sockets and threads and I need to ask a pretty basic question. If you have a socket that every time it accepts a connection (accept()) it spawns a...
3
by: call_me_anything | last post by:
Hi, We had a library linked dynamically in our 32 bit mode. Now due to capacity issues, we are planning to move on to 64 bit but the library is still 32 bit and we cannot make it 64 bit without...
25
by: 4.4.bsd | last post by:
Is there any possible way to use sockets in C, so i can finish a instant messenger program?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.