【linux】59、Internet Domain
程序员文章站
2022-05-26 23:53:45
...
1、网络字节序(大端)
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)
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展示字符串的最大长度:
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);
}
}
上一篇: 二维字符数组按长度排序
下一篇: CPU Cache一探究竟
推荐阅读
-
Linux系统下使用mail发送Internet邮件的配置方法
-
windows的docker开始支持linux的镜像 ,Version 18.03.0-ce-win59 (16762)
-
linux一切皆文件之Unix domain socket描述符(二)
-
Linux系统下使用mail发送Internet邮件的配置方法
-
Linux下如何实现与Internet时间同步
-
【linux】59、Internet Domain
-
每天一个linux命令(59):rcp命令 DOSDebianSolarisFreeBSDWindows
-
linux一切皆文件之Unix domain socket描述符(二)
-
windows的docker开始支持linux的镜像 ,Version 18.03.0-ce-win59 (16762)
-
程序员必须掌握Linux常用的59种命令