From: "John T. Williams" <jtwilliams@vt.edu>
To: linux-c-programming@vger.kernel.org
Subject: SEGFAULT
Date: Wed, 20 Aug 2003 14:56:12 -0400 [thread overview]
Message-ID: <001301c3674c$bc7dc3c0$ed64a8c0@descartes> (raw)
In-Reply-To: 3F3D20F3.7030304@fi.uba.ar
I can't figure out why this code sigfaults when it exits.
gdb only tells me
Program received signal SIGSEGV
0xda64a8c0 in ? ()
and shows the step as being the final "}" in main ().
as far as I can tell this means either one of the linux libraries is wrong
or my code is doing something funny with a buffer.
I'll gladly e-mail a dollar to anyone who can tell me what is wrong with
this short bit of code. Well, I mean what is wrong that causes the SIGSEGV.
_________________CODE______________________
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#define HOST_NAME_MAX 255
int main( ) {
int loop;
struct sockaddr_in *active;
struct sockaddr_in passive;
struct hostent *host;
struct protoent *proto;
int port, activeLen, newSkt, skt;
char HOSTNAME[HOST_NAME_MAX];
int hostnamelength;
int pid;
host = NULL;
proto = getprotobyname("tcp");
if( ( skt = socket(PF_INET, SOCK_STREAM, proto->p_proto ) ) == -1 ) {
printf("Cannot create socket\n");
exit(1);
}
if(gethostname(HOSTNAME, hostnamelength) ) {
printf("fuck\n");
exit(1);
}
host = gethostbyname(HOSTNAME);
passive.sin_family = host->h_addrtype;
passive.sin_port = htons( 4023 );
bcopy(host->h_addr, &passive.sin_addr, host->h_length);
if(bind(skt, (struct sockaddr *) &passive, sizeof(passive) ) ) {
printf("bind failed\n");
exit(1);
}
if( listen(skt, 10) ) {
printf("listen failed\n");
close(skt);
exit(1);
}
activeLen = sizeof(active);
for(loop=0;loop<2;loop++) {
newSkt = accept(skt, (struct sockaddr *) active, &activeLen);
if( (pid = fork() ) == 0 ) {
close(skt);
printf("connection accepted\n");
fflush(NULL);
close(newSkt);
_exit(0);
} else close(newSkt);
}
wait(NULL);
sleep(1);
close(skt);
return 0;
}
_______________________END CODE________________
next prev parent reply other threads:[~2003-08-20 18:56 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-08-15 18:05 Parsing multipart MIME Darío Mariani
2003-08-20 18:56 ` John T. Williams [this message]
2003-08-20 19:35 ` SEGFAULT Glynn Clements
-- strict thread matches above, loose matches on Subject: below --
2003-08-20 19:21 SEGFAULT Sandro Dangui
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='001301c3674c$bc7dc3c0$ed64a8c0@descartes' \
--to=jtwilliams@vt.edu \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).