| | |
Socket programming in IPv6?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
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:
Server Code:
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:
cpp Syntax (Toggle Plain Text)
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> void error(char *msg) { perror(msg); exit(0); } int main(int argc, char *argv[]) { int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if (argc < 3) { fprintf(stderr,"usage %s hostname port\n", argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); printf("Please enter the message: "); bzero(buffer,256); fgets(buffer,255,stdin); n = write(sockfd,buffer,strlen(buffer)); if (n < 0) error("ERROR writing to socket"); bzero(buffer,256); n = read(sockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("%s\n",buffer); return 0; }
Server Code:
cpp Syntax (Toggle Plain Text)
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> void error(char *msg) { perror(msg); exit(1); } int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; int n; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); bzero(buffer,256); n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); n = write(newsockfd,"I got your message",18); if (n < 0) error("ERROR writing to socket"); return 0; }
Last edited by WolfPack; Feb 18th, 2008 at 6:22 am. Reason: Added [CODE=cpp][/CODE] tags
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.
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.
try this
hope it helps
hope it helps
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Indian Developer
http://falaque.wordpress.com/
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- pop ups are back (Viruses, Spyware and other Nasties)
- This is a wierd one! (Windows NT / 2000 / XP)
- Zone Alarm Security Alerts. (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Error receiving message
- Next Thread: Display String on the console redirection
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






