Question: Your task is modifythe sever tolisten on an IPv6 socket. You should also change the code so that it is robust against clients failing to

Your task is modifythe sever tolisten on an IPv6 socket. You should also change the code so that it is robust against clients failing to resolve.

You can test the sever with telnet, specifying an IP6 address. Recall that ::1 is the loopbackaddress in IPv6.

Sample Server code

#include
#include
#include
#include
#include
#include
#include
#include
#include
int
main(intargc,char*argv[])
{
structaddrinfo*ai;
structaddrinfohints;
memset(&hints,'\0',sizeof(hints));
hints.ai_flags=AI_PASSIVE|AI_ADDRCONFIG;
hints.ai_socktype=SOCK_STREAM;
inte=getaddrinfo(NULL,"echo",&hints,&ai);
if(e!=0)
error(EXIT_FAILURE,0,"getaddrinfo:%s",gai_strerror(e));
intnfds=0;
structaddrinfo*runp=ai;
while(runp!=NULL)
{
++nfds;
runp=runp->ai_next;
}
structpollfdfds[nfds];
for(nfds=0,runp=ai;runp!=NULL;runp=runp->ai_next)
{
fds[nfds].fd=socket(runp->ai_family,runp->ai_socktype,runp->ai_protocol);
if(fds[nfds].fd==-1)
error(EXIT_FAILURE,errno,"socket");
fds[nfds].events=POLLIN;
intopt=1;
setsockopt(fds[nfds].fd,SOL_SOCKET,SO_REUSEADDR,
&opt,sizeof(opt));
if(bind(fds[nfds].fd,
runp->ai_addr,runp->ai_addrlen)!=0)
{
if(errno!=EADDRINUSE)
error(EXIT_FAILURE,errno,"bind");
close(fds[nfds].fd);
}
else
{
if(listen(fds[nfds].fd,SOMAXCONN)!=0)
error(EXIT_FAILURE,errno,"listen");
++nfds;
}
}
freeaddrinfo(ai);
while(1)
{
intn=poll(fds,nfds,-1);
if(n>0)
for(inti=0;i
if(fds[i].revents&POLLIN)
{
structsockaddr_storagerem;
socklen_tremlen=sizeof(rem);
intfd=accept(fds[i].fd,(structsockaddr*)&rem,&remlen);
if(fd!=-1)
{
charbuf1[200];
if(getnameinfo((structsockaddr*)&rem,remlen,
buf1,sizeof(buf1),NULL,0,0)!=0)
strcpy(buf1,"???");
charbuf2[100];
(void)getnameinfo((structsockaddr*)&rem,remlen,
buf2,sizeof(buf2),NULL,0,
NI_NUMERICHOST);
printf("connectionfrom%s(%s) ",buf1,buf2);
charbuf[1000];
ssize_tl=read(fd,buf,sizeof(buf));
write(fd,buf,l);
close(fd);
}
}
}
}

Sample Client Code-

#include
#include
#include
#include
#include
#include
#include
#include
int
main(intargc,char*argv[])
{
intresult=0;
structaddrinfo*ai;
structaddrinfohints;
memset(&hints,'\0',sizeof(hints));
hints.ai_flags=AI_ADDRCONFIG;
hints.ai_socktype=SOCK_STREAM;
inte=getaddrinfo(argv[1],"echo",&hints,&ai);
if(e!=0)
error(EXIT_FAILURE,0,"getaddrinfo:%s",gai_strerror(e));
structaddrinfo*runp=ai;
while(runp!=NULL)
{
intsock=socket(runp->ai_family,runp->ai_socktype,
runp->ai_protocol);
if(sock!=-1)
{
if(connect(sock,
runp->ai_addr,runp->ai_addrlen)==0)
{
char*line=NULL;
size_tlen=0;
ssize_tn=getline(&line,&len,stdin);
write(sock,line,n);
n=read(sock,line,len);
write(STDOUT_FILENO,line,n);
close(sock);
gotoout;
}
close(sock);
}
runp=runp->ai_next;
}
error(0,0,"cannotcontact%s",argv[1]);
result=1;
out:
freeaddrinfo(ai);
returnresult;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Computer Network Questions!