Socket programming in IPv6?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 6
Reputation: kelvinskk is an unknown quantity at this point 
Solved Threads: 0
kelvinskk kelvinskk is offline Offline
Newbie Poster

Socket programming in IPv6?

 
0
  #1
Feb 18th, 2008
Hi,
I'm still pretty new to socket programming and I'm using Fedora Core 5. Currently I need a simple IPv6 client to send text messages to an IPv6 Server. From some sample codes that I have gotten, it could work in IPv4, but the code could not work in IPv6. I've tried changing some parts of the code but it still can't work properly. Can someone help to see where's the problem?

Thanks,
KiaN



Client side:
1)For IPv4:
./client 172.19.16.50 30000
Output:
Please enter the message: test
I got your message

2)For IPv6
./client 2001:106:2700::1 30000
Output:
ERROR, no such host




Server side:
./server 30000



Client Code:

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h>
  6.  
  7. void error(char *msg)
  8. {
  9. perror(msg);
  10. exit(0);
  11. }
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. int sockfd, portno, n;
  16. struct sockaddr_in serv_addr;
  17. struct hostent *server;
  18.  
  19. char buffer[256];
  20. if (argc < 3) {
  21. fprintf(stderr,"usage %s hostname port\n", argv[0]);
  22. exit(0);
  23. }
  24. portno = atoi(argv[2]);
  25. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  26. if (sockfd < 0)
  27. error("ERROR opening socket");
  28. server = gethostbyname(argv[1]);
  29. if (server == NULL) {
  30. fprintf(stderr,"ERROR, no such host\n");
  31. exit(0);
  32. }
  33. bzero((char *) &serv_addr, sizeof(serv_addr));
  34. serv_addr.sin_family = AF_INET;
  35. bcopy((char *)server->h_addr,
  36. (char *)&serv_addr.sin_addr.s_addr,
  37. server->h_length);
  38. serv_addr.sin_port = htons(portno);
  39. if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
  40. error("ERROR connecting");
  41. printf("Please enter the message: ");
  42. bzero(buffer,256);
  43. fgets(buffer,255,stdin);
  44. n = write(sockfd,buffer,strlen(buffer));
  45. if (n < 0)
  46. error("ERROR writing to socket");
  47. bzero(buffer,256);
  48. n = read(sockfd,buffer,255);
  49. if (n < 0)
  50. error("ERROR reading from socket");
  51. printf("%s\n",buffer);
  52. return 0;
  53. }

Server Code:
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5.  
  6. void error(char *msg)
  7. {
  8. perror(msg);
  9. exit(1);
  10. }
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. int sockfd, newsockfd, portno, clilen;
  15. char buffer[256];
  16. struct sockaddr_in serv_addr, cli_addr;
  17. int n;
  18. if (argc < 2) {
  19. fprintf(stderr,"ERROR, no port provided\n");
  20. exit(1);
  21. }
  22. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  23. if (sockfd < 0)
  24. error("ERROR opening socket");
  25. bzero((char *) &serv_addr, sizeof(serv_addr));
  26. portno = atoi(argv[1]);
  27. serv_addr.sin_family = AF_INET;
  28. serv_addr.sin_addr.s_addr = INADDR_ANY;
  29. serv_addr.sin_port = htons(portno);
  30. if (bind(sockfd, (struct sockaddr *) &serv_addr,
  31. sizeof(serv_addr)) < 0)
  32. error("ERROR on binding");
  33. listen(sockfd,5);
  34. clilen = sizeof(cli_addr);
  35. newsockfd = accept(sockfd,
  36. (struct sockaddr *) &cli_addr,
  37. &clilen);
  38. if (newsockfd < 0)
  39. error("ERROR on accept");
  40. bzero(buffer,256);
  41. n = read(newsockfd,buffer,255);
  42. if (n < 0) error("ERROR reading from socket");
  43. printf("Here is the message: %s\n",buffer);
  44. n = write(newsockfd,"I got your message",18);
  45. if (n < 0) error("ERROR writing to socket");
  46. return 0;
  47. }
Last edited by WolfPack; Feb 18th, 2008 at 6:22 am. Reason: Added [CODE=cpp][/CODE] tags
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,824
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Socket programming in IPv6?

 
0
  #2
Feb 18th, 2008
You have to install and run windows xp ipv6 service package first.
Also you need to use AF_INET6 ,etc in lieuf AF_INET and pass an ipv6 sturcture, read data in an ipv6 packet format.
Last edited by ithelp; Feb 18th, 2008 at 4:42 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 58
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: Socket programming in IPv6?

 
0
  #3
Feb 18th, 2008
try this
hope it helps
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: kelvinskk is an unknown quantity at this point 
Solved Threads: 0
kelvinskk kelvinskk is offline Offline
Newbie Poster

Re: Socket programming in IPv6?

 
0
  #4
Feb 18th, 2008
Thanks for the quick reply. I've got the IPv6 part working now. Hope it helps others in the future =)
Reference at: http://publib.boulder.ibm.com/iserie...oxoverview.htm

However in my network, my clients do not know the IPv6 address of the server. Is there a way to do a broadcast to all servers in the network? I've tried entering the link-scope all-hosts multicast address, ff02::1 as the server's IP address but it couldn't work.

./Client11 ff02::1
connect() failed: Network is unreachable

Thanks,
KiaN
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC