C에서 클라이언트/서버 프로그램 (두 프로그램이 서로 다른 컴퓨터에 있지만 localhost에는 있지 않음)을 만들려고하지만 사용 된 프로토콜은 IPv6 만 사용합니다. 클라이언트를 실행할 때 connect()에서 일시 중지 된 다음 fails.Why connect()가 실패합니다.소켓 프로그래밍 클라이언트 및 서버 프로그램이 다른 컴퓨터에있는 경우 IPv6를 사용하는 프로그래밍
서버 코드 :
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<unistd.h>
#include<netdb.h>
int main()
{
int sockfd,connfd,rv;
struct addrinfo hints,*servinfo,*p;
struct sockaddr_in6 client_addr;
memset(&hints,0,sizeof hints);
hints.ai_family=AF_INET6;
hints.ai_socktype=SOCK_STREAM;
hints.ai_flags=AI_PASSIVE;
if((rv=getaddrinfo(NULL,"8888",&hints,&servinfo))!=0)
{ printf("\n Error 1 \n"); return 0; }
for(p=servinfo;p!=NULL;p=p->ai_next)
{
if((sockfd=socket(servinfo->ai_family,servinfo->ai_socktype,0))==-1)
{ perror("socket"); continue;}
if(bind(sockfd,servinfo->ai_addr,servinfo->ai_addrlen)==-1)
{ close(sockfd); perror("bind"); continue;}
break;
}
if(p==NULL)
{
fprintf(stderr,"failed to bind");
return 0;
}
listen(sockfd,8);
printf("\n\n Waiting for connection....\n");
socklen_t size=sizeof(client_addr);
if((connfd=accept(sockfd,(struct sockaddr *)&client_addr,&size))<0)
{ printf("\n Error 4 \n"); return 0; }
else
{
char ch[50];
inet_ntop(AF_INET6,&(client_addr.sin6_addr),ch,50);
printf("\n Connected to %s \n",ch);
}
close(sockfd);
close(connfd);
return 0;
}
클라이언트 코드 : 첫째
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<unistd.h>
#include<netdb.h>
int main()
{
int i,s;
struct addrinfo hints,*res,*p;
memset(&hints,0,sizeof (hints));
hints.ai_family=AF_INET6;
hints.ai_socktype=SOCK_STREAM;
i=getaddrinfo("fe80::20c:29ff:fe60:7593%eth0","8888",&hints,&res);//because the system in which server code is has IPv6 address fe80::20c:29ff:fe60:7593
if(i!=0)
{ printf("\n Fail 1 \n"); return 0;}
for(p=res;p!=NULL;p=p->ai_next)
{
if((s=socket(res->ai_family,res->ai_socktype,0))==-1)
{perror("socket"); continue;}
if(connect(s,p->ai_addr,p->ai_addrlen)==-1)
{ close(s); perror("connect"); continue;}
break;
}
if(p==NULL)
{
fprintf(stderr,"failed to connect\n");
return 0;
}
close(s);
return 0;
}
왜 실제로? 왜 당신은 이미 페러()가 인쇄 한 것을 알 때 우리에게 왜 묻고 있습니까? 주의 : close()와 같은 다른 시스템 호출보다 먼저 호출해야합니다. – EJP
사실은 오류를 인쇄하지 못했지만, 내가 perror()를 즉시 호출했다. 오류가 발생했습니다 : 연결 시간이 초과되었습니다. –
실제로 오류가 인쇄되었지만 잘못되었습니다. 당신은 귀하의 질문에 모든 정보를 포함시켜야합니다. 그것없이, 그것은 대답 할 수 없다. – EJP