红联Linux门户
Linux帮助

小妹求教!IPV6下的udp网络编程问题!

发布时间:2006-04-03 16:54:02来源:红联作者:warmecho
请教各位高手大哥,我以一个ipv6下的tcp客户端程序为基础,想改为udp客户端程序,因为对ipv6理解很浅,现编译完后有些问题解决不了,清大家帮我改改哪里不对,谢谢!!!程序如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEFAULT_PORT 9999
struct s_opt
{
sa_family_t family;
int debug;
char *host;
char *service;
};
void print_help(char *pname)
{
fprint(stderr,"%s[[option] host\n"
"-f[46]:specify family\n"
"-p:specify port(default 9999)\n"
"-d:display verbosely\n"
"-h:help\n",
pname);
}
int main(int argc,char**argv)
{
struct s_opt sopt;
int c,n;
struct addrinfo hints,*res,*res0;
int err;
int s;
char host[NI_MAXHOST];
char serv[NI_MAXSERV];
char rbuf[BUFSIZ];
char wbuf[BUFSIZ];
memset(&sopt,0,sizeof(sopt));
sopt.family = PF_UNSPEC;
while((c=getopt(argc,argv,"p:f:dh:"))!=EOF)
{
switch(c)
{
case'p':
sopt.service=(char*)malloc(strlen(optarg)+1);
strcpy(sopt.service,optarg);
break;
case'f':
if(!strncmp("4",optarg,1))
{
sopt.family = PF_INET;}
else if(!strncmp("6",optarg,1))
{
sopt.family = PF_INET6;}
else
{
print_help(argv[0]);
exit(EXIT_FAILURE);}
break;
case'd':
sopt.debug=1;
break;
case'h':
default:
print_help(argv[0]);
exit(EXIT_SUCCESS);
}
}
if(optind {
sopt.host=(char*)malloc(strlen(argv[optind])+1);
strcpy(sopt.host,argv[optind]);}
else
{
print_help(argv[0]);
exit(EXIT_FAILURE);}
if(sopt.service==NULL)
{
sopt.service=(char*)malloc(strlen(DEFAULT_PORT)+1);
strcpy(sopt.service,DEFAULT_PORT);}
memset(&hints,0,sizeof(hints));
hints.ai_family=sopt.family;
hints.ai_socktype=SOCK_DGRAM;
err=getaddrinfo(sopt.host,sopt.service,&hints,&res0);
if(err)
{
fprintf(stderr,"getaddrinfo:%s\n",gai_streeor(err));
exit(EXIT_FAILURE);}
for(res=res0;res;res=res->ai_next)
{
s=socket(res->ai_family,res->ai_socktype,0);
if(s<0)
{
fprintf(stderr,"socket()faild:%d\n,s");
continue;}
if(connect(s,res->ai_addr,res->ai_addrlen)<0)
{
perror("connect");
close(s);
s=-1;
continue;}
if(sopt.debug)
{
getnameinfo(res->ai_addr,res->ai_addrlen,
host,sizeof(host),
serv,sizeof(serv),
NI_NUMERICHOST|NI_NUMERICSERV);
fprinft(stderr,"connect:[%s]:%s\n",host,serv);}
break;}
if(s<0)
{
fprintf(stderr,"can't connect %s\n",sopt.host);
exit(EXIT_FAILURE);}
while(fegets(wbuf,sizeof(wbuf),stdin)!=NULL)
{
write(s,wbuf,strlen(wbuf));
n=read(s,rbuf,sizeof(wbuf));
if(n=-1)
{
fprintf(stderr,"can't read");
exit(EXIT_FAILURE);}
rbuf[n]=0;
fputs(rbuf,stdout);}
if(argc!=2)
{
fprintf(stderr,"usage:%s server_ip server_port\n",argv[0]);
exit(1);}
if(sopt.service)free(sopt.service);
if(sopt.host)free(sopt.service);
freeaddrinfo(res0);
return 0;
}

编译后的错误是:
[root@localhost ~]# gcc -o cli cli.c
cli.c: In function `main':
cli.c:78: warning: passing arg 1 of `strlen' makes pointer from integer without a cast
cli.c:79: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast
cli.c:113: warning: comparison between pointer and integer
/tmp/ccEYQtwL.o(.text+0x18): In function `print_help':
: undefined reference to `fprint'
/tmp/ccEYQtwL.o(.text+0x26c): In function `main':
: undefined reference to `gai_streeor'
/tmp/ccEYQtwL.o(.text+0x371): In function `main':
: undefined reference to `fprinft'
/tmp/ccEYQtwL.o(.text+0x3c7): In function `main':
: undefined reference to `fegets'
collect2: ld returned 1 exit status

我现在知道是少了一个库文件,但具体是什么库也没查到,而且有一句不知道是什么:

getnameinfo(res->ai_addr,res->ai_addrlen,
host,sizeof(host),
serv,sizeof(serv),
NI_NUMERICHOST|NI_NUMERICSERV);
对于这个函数,查了查还是不懂,盼望各位高手哥哥赐教!!
最绝望的是,我需要的是一个ipv6下的udp服务器端程序T.T更不会改了……
文章评论

共有 0 条评论