欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【linux】59、Internet Domain

程序员文章站 2022-05-26 23:53:45
...

1、网络字节序(大端)

【linux】59、Internet Domain

2、主机和网络字节序之间转换整数

#include <arpa/inet.h>
uint16_t htons(uint16_t host_unint16);
uint32_t htonl(uint32_t host_unint32);
uint16_t ntohs(uint16_t host_unint16);
uint32_t ntohl(uint32_t host_unint32);

3、Internet socket 地址(ipv4、ipv6)

【linux】59、Internet Domain
【linux】59、Internet Domain
ipv4通配地址(0.0.0.0):INADDR_ANY
ipv6通配地址(0::0):IN6ADDR_ANY_INIT
ipv6环回地址(::1): IN6ADDR_LOOPBACK_INIT

inet_pton()和inet_ntop()

#include <arpa/inet.h>
int inet_pton(int domain, const char *src_str, void *addrptr);
//success,1; error format(src_str), 0;error -1
const char *inet_ntop(int domain, const void *addrptr, char *dst_str, size_t len);
//success, *det_str; error, NULL

domain: AF_INET or AF_INET6
(1)inet_pton() 函数将src_str 中的字符串转换成网络字节序的二进制IP地址。转换得到的地址放在addrptr指向的结构中。
(2)inet_ntop()函数 执行逆向转换。addrptr:待转换的in_addr或in6_addr结构。dst_str:得到以null结尾的字符串。len:dst_str的大小
(3)ipv4和ipv6展示字符串的最大长度:
【linux】59、Internet Domain
inet6_sv.c

#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>

#define LENGTH 30
#define PORT_NUM 31001

int main(int argc, char *argv[])
{
	struct sockaddr_in6 sv_addr, cl_addr;
	int sfd, len, j;
	ssize_t num;
	char buf[INET6_ADDRSTRLEN], msg[LENGTH];

	sfd = socket(AF_INET6, SOCK_DGRAM, 0);
	if (sfd == -1)
		exit(EXIT_FAILURE);
	memset(&sv_addr, 0, sizeof(struct sockaddr_in6));
	sv_addr.sin6_family = AF_INET6;
	sv_addr.sin6_addr = in6addr_any;
	sv_addr.sin6_port = htons(PORT_NUM);

	if (bind(sfd, (struct sockaddr *) &sv_addr, sizeof(struct sockaddr_in6)) == -1)
		exit(EXIT_FAILURE);

	len = sizeof(struct sockaddr_in6);

	for (;;) {
		memset(&msg, 0, sizeof(msg));
		num = recvfrom(sfd, msg, sizeof(msg), 0, (struct sockaddr *) &cl_addr, &len);
		if (num == -1)
			exit(EXIT_FAILURE);
		if (inet_ntop(AF_INET6, &cl_addr.sin6_addr, buf, INET6_ADDRSTRLEN) == NULL)
			exit(EXIT_FAILURE);
		else
			printf("Server received %ld bytes from (%s, %u):%s\n", num, buf, ntohs(cl_addr.sin6_port), msg);
		for (j = 0; j < num; j++) 
			msg[j] = toupper(msg[j]);

		if (sendto(sfd, msg, num, 0, (struct sockaddr *) &cl_addr, len) != num)
			exit(EXIT_FAILURE);
	}
}

inet6_cl.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#define LENGTH 30
#define PORT_NUM 31001

int main(int argc, char *argv[]) 
{
	struct sockaddr_in6 sv_addr, cl_addr;
	int sfd, j, msgLen;
	char msg[LENGTH], addrBuf[INET6_ADDRSTRLEN];
	socklen_t num;

	sfd = socket(AF_INET6, SOCK_DGRAM, 0);
	if (sfd == -1)
		exit(EXIT_FAILURE);

	memset(&sv_addr, 0, sizeof(sv_addr));
	sv_addr.sin6_family = AF_INET6;
	sv_addr.sin6_port = htons(PORT_NUM);
	if (inet_pton(AF_INET6, "::1", &sv_addr.sin6_addr) <= 0)
		exit(EXIT_FAILURE);
	num = sizeof(struct sockaddr_in6);

 	for (j = 1; j < argc; j++) {
 		msgLen = strlen(argv[j]);
 		memset(&msg, 0, sizeof(msg));
 		if (sendto(sfd, argv[j], msgLen, 0, (struct sockaddr *) &sv_addr, sizeof(struct sockaddr_in6)) != msgLen)
 			exit(EXIT_FAILURE);
 		if ((msgLen = recvfrom(sfd, msg, LENGTH, 0, (struct sockaddr *) &cl_addr, &num)) == -1)
 			exit(EXIT_FAILURE);
 		if (inet_ntop(AF_INET6, &cl_addr.sin6_addr, addrBuf, INET6_ADDRSTRLEN) == NULL)
 			exit(EXIT_FAILURE);
 		printf("Client received %d bytes from %s, the msg is %s\n", msgLen, addrBuf, msg);
 	}	
}

【linux】59、Internet Domain
【linux】59、Internet Domain

相关标签: 程序 c语言