小妹求教!IPV6下的udp網路程式設計問題!(轉)

BSDLite發表於2007-08-12
小妹求教!IPV6下的udp網路程式設計問題!(轉)[@more@]請教各位高手大哥,我以一個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 "
"-f[46]:specify family "
"-p:specify port(default 9999) "
"-d:display verbosely "
"-h:help ",
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 ",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 ,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 ",host,serv);}
break;}
if(s<0)
{
fprintf(stderr,"can't connect %s ",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 ",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更不會改了……

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617542/viewspace-949558/,如需轉載,請註明出處,否則將追究法律責任。

相關文章