As we all know, with the increase of the number of hosts on the Internet, the existing 32-bit IP address is not enough, so the introduction of the next generation of IP address IPv6, write network program to slightly change the existing network program to adapt to IPV6 network is quite easy.
For us is the IP address changes, so the program in the use of IP address where the corresponding changes can be.
Remember: The main change in the program to set the IP address and port parts of the code.
The server-side source code is as follows:
/***********************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#define MAXBUF 1024
/************ about this document ********************************************
*filename:ipv6-server.c
*purpose: Demonstrate the most basic IPV6 network programming steps, turn on the service receive client connection and communicate with the client, send and receive messages to each other
*wrote By:zhoulifa ([email protected]) Zhou Lifa (http://zhoulifa.bokee.com)
Linux Enthusiasts Linux knowledge propagator Soho developers are best at C language
*date time:2007-01-29 13:06
*note: Anyone can copy code and use these documents, of course, including your commercial use
* But please follow the GPL
*thanks To:google
*hope: I hope more and more people contribute their strength for the development of science and technology.
* Science and technology stand on the shoulders of giants progress faster! Thanks to the contributions of the source of the predecessors!
Inet_aton () Convert network host address CP is a binary value, and stored in the struct IN_ADDR structure, that is, the second parameter *INP, the function returns non 0 indicates that the CP host is valid, return 0 indicates that the host address is invalid.
The INET_ADDR function converts a network host address (such as 192.168.1.10) to a network byte-order binary value, and if the argument char *CP is invalid, the function returns-1 (Inaddr_none), which returns 1 when the address is 255.255.255.255. , 255.255.255.255 is a valid address, but inet_addr cannot handle it;
The Inet_ntoa function converts the address of a network byte sort to a standard ASCII point-separated address, which returns a pointer to a point-separated string address that is statically allocated, meaning that the last call will be overridden (overwritten) when the function is called the second time. So if you need to save the string last copy it out of your own management!
*********************************************************************/
int main (int argc, char **argv)
{
int SOCKFD, NEW_FD;
Socklen_t Len;
/* struct sockaddr_in my_addr, their_addr; *///IPV4
struct sockaddr_in6 my_addr, their_addr; IPv6
unsigned int myport, lisnum; MyPort: Port number, lisnum maximum number of connections
Char buf[maxbuf + 1];
if (Argv[1])
MyPort = Atoi (argv[1]);//atoi/convert string to shaping,
Else
MyPort = 7838;
if (argv[2])
Lisnum = Atoi (argv[2]);
Else
Lisnum = 2;
/* if ((SOCKFD = socket (pf_inet, sock_stream, 0)) = = = 1) {///IPV4
if ((SOCKFD = socket (Pf_inet6, sock_stream, 0)) = = = 1) {//IPV6
Perror ("socket");
Exit (1);
} else
printf ("Socket created\n");
Bzero (&my_addr, sizeof (MY_ADDR));
/* my_addr.sin_family = pf_inet; *///IPV4
my_addr.sin6_family = Pf_inet6; IPv6
/* My_addr.sin_port = htons (MyPort); *///IPV4
My_addr.sin6_port = htons (MyPort); IPV6 htons: Converts the unsigned short shape number of a host into a network byte order.
if (Argv[3])
/* my_addr.sin_addr.s_addr = inet_addr (argv[3]); *///IPV4
Inet_pton (Af_inet6, argv[3], &my_addr.sin6_addr); IPv6
/* #include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int Inet_pton (int af, const char *src, void *DST);
This function converts a string to a network address, the first parameter AF is the address family, and after the conversion exists in DST
Inet_pton is an extension of inet_addr, and the supported multi-address families have the following:
Af_inet
SRC is the address that points to the character type, that is, the first address of the ASCII address (in DDD.DDD.DDD.DDD format), and the function converts the address
Structure converted to in_addr, and copied in *DST
Af_inet6
SRC is the address that points to IPV6, and the function adds the address
Structure converted to IN6_ADDR, and copied in *DST
If a function error returns a negative value and the errno is set to Eafnosupport, the function returns 0 if the address family specified by the parameter AF is not the same as the SRC format.
The function inet_ntop the reverse transformation prototype as follows
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
const char *inet_ntop (int af, const void *SRC, char *dst, socklen_t CNT);
This function converts the network binary structure to the ASCII type address, the function of the parameter is the same as above, just one more parameter socklen_t cnt, he is the size of the buffer DST, avoid overflow, if the buffer is too small to store the value of the address, then return a null pointer, and set the errno to ENOSPC.
*/
Else
/* my_addr.sin_addr.s_addr = Inaddr_any; *///IPV4
MY_ADDR.SIN6_ADDR = In6addr_any; IPv6
/* if (bind (SOCKFD, struct sockaddr *) &my_addr, sizeof (struct sockaddr)) *//IPV4
if (Bind (SOCKFD, (struct sockaddr *) &my_addr, sizeof (struct sockaddr_in6))//IPV6
= =-1) {
Perror ("bind");
Exit (1);
} else
printf ("binded\n");
if (Listen (SOCKFD, lisnum) = =-1) {
Perror ("Listen");
Exit (1);
} else
printf ("Begin listen\n");
while (1) {
len = sizeof (struct sockaddr);
if (NEW_FD =
Accept (SOCKFD, (struct sockaddr *) &their_addr,
&len)) = =-1) {
Perror ("accept");
Exit (errno);
} else
printf ("Server:got connection from%s, port%d, socket%d\n",
/* INET_NTOA (THEIR_ADDR.SIN_ADDR), *///Ipv4
Inet_ntop (Af_inet6, &their_addr.sin6_addr, buf, sizeof (BUF)),//IPV6
/* NTOHS (Their_addr.sin_port), NEW_FD); *///IPV4
Their_addr.sin6_port, NEW_FD); IPv6
/* Start processing data on each new connection */
Bzero (buf, maxbuf + 1);
strcpy (BUF,
"This is the first message sent to the client after the success of the connection, \ n only to new_fd this new socket with the Accept function message, not to SOCKFD this listener socket to send messages, listen to the socket can not be used to receive or send messages \ n");
/* Send message to client */
Len = Send (NEW_FD, buf, strlen (BUF), 0);
if (Len < 0) {
Printf
("Message '%s ' failed to send!") The error code is%d and the error message is '%s ' \ n ',
BUF, errno, Strerror (errno));
} else
printf ("Message '%s ' sent successfully, total%d bytes sent!") \ n ",
BUF, Len);
Bzero (buf, maxbuf + 1);
/* Receive messages from clients */
Len = recv (new_fd, buf, maxbuf, 0);
if (len > 0)
printf ("Received message succeeded: '%s ',%d bytes of data \ n",
BUF, Len);
Else
Printf
("Message received failed!") The error code is%d and the error message is '%s ' \ n ',
errno, Strerror (errno));
/* processing end of data on each new connection */
}
Close (SOCKFD);
return 0;
}
The "//ipv4" after each line of the program means that this line of code is used in the IPV4 network.
and "//ipv6" means that this line of code is used in the IPV6 network, compared, it will be easy to see the difference.
The client source code is as follows:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>//?
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXBUF 1024
/************ about this document ********************************************
*filename:ipv6-client.c
*purpose: Demonstrates the most basic IPV6 network programming step, this is a client program, and the server to send and receive messages to each other
*wrote By:zhoulifa ([email protected]) Zhou Lifa (http://zhoulifa.bokee.com)
Linux Enthusiasts Linux knowledge propagator Soho developers are best at C language
*date time:2007-01-29 12:56
*note: Anyone can copy code and use these documents, of course, including your commercial use
* But please follow the GPL
*thanks To:google
*hope: I hope more and more people contribute their strength for the development of science and technology.
* Science and technology stand on the shoulders of giants progress faster! Thanks to the contributions of the source of the predecessors!
*********************************************************************/
int main (int argc, char **argv)
{
int SOCKFD, Len;
/* struct sockaddr_in dest; *///IPV4
struct SOCKADDR_IN6 dest; IPv6
Char buffer[maxbuf + 1];
if (argc! = 3) {
Printf
("parameter format is wrong! The correct usage is as follows: \n\t\t%s IP address Port \n\t For example: \t%s 127.0.0.1 80\n This program is used to receive up to maxbuf bytes of messages from a server on a certain IP address ",
Argv[0], argv[0]);
Exit (0);
}
/* Create a socket for TCP communication */
/* if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0) {///IPV4
if ((SOCKFD = socket (Af_inet6, sock_stream, 0)) < 0) {//IPV6
Perror ("Socket");
Exit (errno);
}
printf ("Socket created\n");
/* Initialize the address and port information for the server side (each other) */
Bzero (&dest, sizeof (dest));
/* dest.sin_family = af_inet; *///IPV4
dest.sin6_family = Af_inet6; IPv6
/* Dest.sin_port = htons (Atoi (argv[2])); *///IPV4
Dest.sin6_port = htons (Atoi (argv[2])); IPv6
/* IF (Inet_aton (argv[1], (struct in_addr *) &dest.sin_addr.s_addr) = = 0) {*//IPV4
if (Inet_pton (Af_inet6, argv[1], &dest.sin6_addr) < 0) {//IPV6
Perror (argv[1]);
Exit (errno);
}
printf ("Address created\n");
/* Connect to Server */
if (Connect (SOCKFD, (struct sockaddr *) &dest, sizeof (dest))! = 0) {
Perror ("Connect");
Exit (errno);
}
printf ("Server connected\n");
/* Receive a message from each other, up to maxbuf bytes */
Bzero (buffer, maxbuf + 1);
/* Receive messages from the server */
Len = recv (sockfd, buffer, maxbuf, 0);
if (len > 0)
printf ("Received message succeeded: '%s ',%d bytes of data \ n",
Buffer, Len);
Else
Printf
("Message received failed!") The error code is%d and the error message is '%s ' \ n ',
errno, Strerror (errno));
Bzero (buffer, maxbuf + 1);
strcpy (Buffer, "This is the message sent to the server by the client \ n");
/* Send a message to the server */
Len = Send (sockfd, buffer, strlen (buffer), 0);
if (Len < 0)
Printf
("Message '%s ' failed to send!") The error code is%d and the error message is '%s ' \ n ',
Buffer, errno, strerror (errno));
Else
printf ("Message '%s ' sent successfully, total%d bytes sent!") \ n ",
Buffer, Len);
/* Close Connection */
Close (SOCKFD);
return 0;
}
Compile the program with the following command:
Reference:
Gcc-wall Ipv6-server.c-o Ipv6server
Gcc-wall Ipv6-client.c-o ipv6client
LINUX under IPv6 socket programming