[LCP]Problem with GNU C sockets
James McLean
james at adam.com.au
Mon Sep 8 13:09:01 UTC 2003
Hi All,
I have previously written a small IRC bot in perl to get a handle on
perl sockets, which worked well and was easy to learn.
In an attempt to learn more about C, I am re-writing the same bot from
scratch in C.
The basic C structure is not a problem, but I have been looking at the
GNU C Sockets documentation to learn how to program the sockets, and
borrowed the example outlined on the site.
I have struck a problem, which my be easy to fix, but also might not be.
I am writing the program on FreeBSD (hope that doesnt exclude me from
this list...)
I think the problem lies in the way im calling my 'irc_connect()'
function, as this is where the errors are generated from the compiler.
I have attached a uname output, gcc version, compiler errors and the
program source. The program is not complete, just trying to get it to
compile cleanly in the various steps along the way.
I would appreciate any pointers or tips as to where I am going wrong.
Cheers
Uname
-----
-bash-2.05b$ uname -a
FreeBSD wan-gw.lanpad.org 4.8-RELEASE FreeBSD 4.8-RELEASE #0: Sun Jul 20
20:19:47 CST 2003
jamesmc at wan-gw.lanpad.org:/usr/src/sys/compile/WIRELESSGW i386
GCC Version.
------------
-bash-2.05b$ gcc -v
Using builtin specs.
gcc version 2.95.4 20020320 [FreeBSD]
Compile Errors
--------------
-bash-2.05b$ make ircbot
cc -O -pipe ircbot.c -o ircbot
In file included from ircbot.c:3:
/usr/include/sys/socket.h:52: syntax error before `sa_family_t'
/usr/include/sys/socket.h:52: warning: data definition has no type or
storage class
/usr/include/sys/socket.h:163: syntax error before `u_char'
/usr/include/sys/socket.h:174: syntax error before `u_short'
/usr/include/sys/socket.h:188: syntax error before `u_char'
/usr/include/sys/socket.h:190: `int64_t' undeclared here (not in a function)
/usr/include/sys/socket.h:190: `u_char' undeclared here (not in a function)
/usr/include/sys/socket.h:190: size of array `__ss_pad1' is too large
/usr/include/sys/socket.h:191: syntax error before `int64_t'
/usr/include/sys/socket.h:192: `u_char' undeclared here (not in a function)
/usr/include/sys/socket.h:192: `int64_t' undeclared here (not in a function)
/usr/include/sys/socket.h:192: `u_char' undeclared here (not in a function)
/usr/include/sys/socket.h:192: `int64_t' undeclared here (not in a function)
/usr/include/sys/socket.h:359: syntax error before `pid_t'
/usr/include/sys/socket.h:364: syntax error before `gid_t'
/usr/include/sys/socket.h:399: syntax error before `u_short'
/usr/include/sys/socket.h:407: syntax error before `caddr_t'
/usr/include/sys/socket.h:411: syntax error before `caddr_t'
/usr/include/sys/socket.h:444: syntax error before `recv'
/usr/include/sys/socket.h:444: warning: data definition has no type or
storage class
/usr/include/sys/socket.h:445: syntax error before `recvfrom'
/usr/include/sys/socket.h:445: warning: data definition has no type or
storage class
/usr/include/sys/socket.h:446: syntax error before `recvmsg'
/usr/include/sys/socket.h:446: warning: data definition has no type or
storage class
/usr/include/sys/socket.h:447: syntax error before `send'
/usr/include/sys/socket.h:447: warning: data definition has no type or
storage class
/usr/include/sys/socket.h:448: syntax error before `sendto'
/usr/include/sys/socket.h:449: warning: data definition has no type or
storage class
/usr/include/sys/socket.h:450: syntax error before `sendmsg'
/usr/include/sys/socket.h:450: warning: data definition has no type or
storage class
/usr/include/sys/socket.h:451: syntax error before `off_t'
In file included from ircbot.c:4:
/usr/include/netinet/in.h:235: syntax error before `in_addr_t'
/usr/include/netinet/in.h:287: syntax error before `u_char'
In file included from /usr/include/netinet/in.h:469,
from ircbot.c:4:
/usr/include/netinet6/in6.h:122: syntax error before `u_int8_t'
/usr/include/netinet6/in6.h:144: syntax error before `u_int8_t'
/usr/include/netinet6/in6.h:149: syntax error before `u_int32_t'
/usr/include/netinet6/in6.h:612: syntax error before `*'
/usr/include/netinet6/in6.h:614: syntax error before `*'
/usr/include/netinet6/in6.h:614: warning: data definition has no type or
storage class
/usr/include/netinet6/in6.h:615: syntax error before `u_int8_t'
/usr/include/netinet6/in6.h:616: syntax error before `u_int8_t'
/usr/include/netinet6/in6.h:631: syntax error before `u_int8_t'
/usr/include/netinet6/in6.h:636: syntax error before `u_int8_t'
/usr/include/netinet6/in6.h:638: syntax error before `u_int8_t'
ircbot.c:29: syntax error before `uint16_t'
ircbot.c: In function `irc_connect':
ircbot.c:33: `hostname' undeclared (first use in this function)
ircbot.c:33: (Each undeclared identifier is reported only once
ircbot.c:33: for each function it appears in.)
ircbot.c:33: `port' undeclared (first use in this function)
ircbot.c:35: `name' undeclared (first use in this function)
ircbot.c: In function `main':
ircbot.c:53: warning: initialization makes integer from pointer without
a cast
*** Error code 1
Stop in /usr/home/jamesmc/src/ircbot.
Program Source.
---------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
/*
* simple IRC bot.
*
*/
void
intro (void) {
static char intro[] =
" \n"
"SimpleBot - super simple IRC bot. \n"
"Copyright 2003 James Mclean <james at lanpad.org> \n"
"\n";
printf("%s",intro);
}
void
irc_connect(struct sockaddr_in *name,
const char *hostname,
uint16_t port) {
struct hostent *hostinfo;
printf("Connecting to %s:%s... \n",hostname,port);
name->sin_family = AF_INET;
name->sin_port = htons (port);
hostinfo = gethostbyname (hostname);
if (hostinfo == NULL) {
fprintf (stderr, "Unknown host %s.\n", hostname);
exit (EXIT_FAILURE);
}
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
}
int
main (int argc, char *argv[])
{
char BOTNAME[16] = "fred";
char SERVERNAME[32] = "irc.enterthegame.com";
int SERVERPORT = "6667";
intro();
printf("Initialising %s... \n",BOTNAME);
irc_connect(SERVERNAME,SERVERNAME,SERVERPORT);
/*
* supply name and connection details here
*/
for(;;) { /* infinite loop */
}
return 0;
}
More information about the linuxCprogramming
mailing list