实验目标:
1 实现基于流的unix域套接字通信cs模型
2 实现基于数据报的unix域套接字通信cs模型
3 可以观察到CS两端的完整启动退出流程,为了实现这一目标仅进行一次通信
实验心得:
1 使用unlink避免地址冲突 清理资源
2 传统udp在首次sendto时系统临时分配端口,在套接字关闭|程序终止|显式解绑时端口生命周期结束,而unix域套接字则需要手动绑定,否则客户端无法收到服务器的回信
3 复制文件路径时,sun_path的长度无法更改,容易出现越界的情况
基于流:
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <string.h>
#include <unistd.h>
#define SOCK_PATH "/home/u22/socket/addr1"
// 服务端
int main()
{
int server_sockfd, client_sockfd;
struct sockaddr_un server_sockaddr, client_sockaddr;
memset(&server_sockaddr, 0, sizeof(server_sockaddr));
memset(&client_sockaddr, 0, sizeof(client_sockaddr));
socklen_t client_sockaddr_len = sizeof(client_sockaddr);
ssize_t send_bytes, recv_bytes;
char send_buf[1024] = "server say : How can I help you today ?";
char recv_buf[1024] = {0};
// unix域套接字 使用流式传输,不能叫TCP
server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (server_sockfd == -1)
{
perror("socket");
}
server_sockaddr.sun_family = AF_UNIX;
strcpy(server_sockaddr.sun_path, SOCK_PATH);
// 为什么要删除这个文件?因为再次启动代码将无法访问这个文件
unlink(SOCK_PATH);
if ((bind(server_sockfd, (struct sockaddr *)&server_sockaddr, sizeof(server_sockaddr))) == -1)
{
perror("bind");
}
if (listen(server_sockfd, 16) == -1)
{
perror("listen");
}
printf("waiting for connect...\n");
// accept返回后将收到客户端地址信息
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_sockaddr, &client_sockaddr_len);
if (client_sockfd == -1)
{
perror("accept");
}
// 同tcp一样 recv send使用accept返回的socket通信
recv_bytes = recv(client_sockfd, recv_buf, sizeof(recv_buf), 0);
if (recv_bytes == -1)
{
perror("recv");
}
if (recv_bytes > 0)
{
printf("%s\n", recv_buf);
}
send_bytes = send(client_sockfd, send_buf, strlen(send_buf), 0);
if (send_bytes == -1)
{
perror("send");
}
if (send_bytes > 0)
{
printf("%s\n", send_buf);
}
close(client_sockfd);
close(server_sockfd);
printf("server close\n");
//用完了 还要删一遍
unlink(SOCK_PATH);
return 0;
}
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <string.h>
#include <unistd.h>
#define SOCK_PATH "/home/u22/socket/addr1"
//客户端
int main()
{
int client_sockfd;
struct sockaddr_un server_sockaddr;
memset(&server_sockaddr, 0, sizeof(server_sockaddr));
ssize_t send_bytes, recv_bytes;
char send_buf[1024] = "client say : hello server !";
char recv_buf[1024] = {0};
//创建基于流的unix域套接字
client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (client_sockfd == -1)
{
perror("socket");
}
server_sockaddr.sun_family = AF_UNIX;
strcpy(server_sockaddr.sun_path, SOCK_PATH);
//尝试连接server
printf("connect server...\n");
if ((connect(client_sockfd, (struct sockaddr *)&server_sockaddr, sizeof(server_sockaddr))) == -1)
{
perror("connect");
}
//与TCP一样 使用socket()返回的socket通信
send_bytes = send(client_sockfd, send_buf, strlen(send_buf), 0);
if (send_bytes == -1)
{
perror("send");
}
printf("%s\n", send_buf);
recv_bytes = recv(client_sockfd, recv_buf, sizeof(recv_buf), 0);
if (recv_bytes == -1)
{
perror("recv");
}
printf("%s\n", recv_buf);
close(client_sockfd);
printf("client close\n");
return 0;
}
基于数据报:
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <string.h>
#include <unistd.h>
#define SOCK_PATH "/home/u22/socket/addr2"
int main()
{
int server_sockfd;
struct sockaddr_un server_sockaddr, client_sockaddr;
memset(&server_sockaddr, 0, sizeof(server_sockaddr));
memset(&client_sockaddr, 0, sizeof(client_sockaddr));
socklen_t client_sockaddr_len = sizeof(client_sockaddr);
ssize_t send_bytes, recv_bytes;
char send_buf[1024] = "server say : How can I help you today ?";
char recv_buf[1024] = {0};
// unix域套接字 使用数据报传输,不能叫UDP
server_sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (server_sockfd == -1)
{
perror("socket");
}
// 无法再次访问此文件,必须删除
unlink(SOCK_PATH);
server_sockaddr.sun_family = AF_UNIX;
// 容易越界,本系统为108字节
strncpy(server_sockaddr.sun_path, SOCK_PATH, sizeof(server_sockaddr.sun_path));
// 不bind,peer使用sendto也可以正常发送,但是本endpoint无法接收
if ((bind(server_sockfd, (struct sockaddr *)&server_sockaddr, sizeof(server_sockaddr))) == -1)
{
perror("bind");
}
printf("waiting for recv...\n");
// 接收的客户端地址信息存在client_sockaddr中,供sendto使用
recv_bytes = recvfrom(server_sockfd, recv_buf, sizeof(recv_buf),
0, (struct sockaddr *)&client_sockaddr, &client_sockaddr_len);
if (recv_bytes == -1)
{
perror("recv");
}
if (recv_bytes > 0)
{
printf("%s\n", recv_buf);
}
send_bytes = sendto(server_sockfd, send_buf, strlen(send_buf),
0, (struct sockaddr *)&client_sockaddr, client_sockaddr_len);
if (send_bytes == -1)
{
perror("send");
}
if (send_bytes > 0)
{
printf("%s\n", send_buf);
}
close(server_sockfd);
printf("server close\n");
// 用完删除SOCK_PATH这个文件
unlink(SOCK_PATH);
return 0;
}
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <string.h>
#include <unistd.h>
// 客户端使用的socket文件
#define SOCK_PATH_C "/home/u22/socket/addr3"
// 服务器使用的socket文件
#define SOCK_PATH_S "/home/u22/socket/addr2"
// 客户端
int main()
{
int client_sockfd;
struct sockaddr_un server_sockaddr, client_sockaddr;
memset(&server_sockaddr, 0, sizeof(server_sockaddr));
memset(&client_sockaddr, 0, sizeof(client_sockaddr));
socklen_t server_sockaddr_len = sizeof(server_sockaddr);
ssize_t send_bytes, recv_bytes;
char send_buf[1024] = "client say : hello server !";
char recv_buf[1024] = {0};
// 基于数据报的unix域套接字
client_sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (client_sockfd == -1)
{
perror("socket");
}
// 必须删除,否则下次不让用
unlink(SOCK_PATH_C);
client_sockaddr.sun_family = AF_UNIX;
// 容易越界,本系统为108
strncpy(client_sockaddr.sun_path, SOCK_PATH_C, sizeof(client_sockaddr.sun_path));
// 传统udp在首次sendto时系统临时分配端口,在套接字关闭|程序终止|显式解绑时端口生命周期结束
// 而unix域套接字则需要手动绑定
if ((bind(client_sockfd, (struct sockaddr *)&client_sockaddr, sizeof(client_sockaddr))) == -1)
{
perror("bind");
}
server_sockaddr.sun_family = AF_UNIX;
// 容易越界,本系统为108
strncpy(server_sockaddr.sun_path, SOCK_PATH_S, sizeof(server_sockaddr.sun_path));
printf("send message\n");
// 向服务器发送信息,由server_sockaddr指定地址
send_bytes = sendto(client_sockfd, send_buf, strlen(send_buf),
0, (struct sockaddr *)&server_sockaddr, server_sockaddr_len);
if (send_bytes == -1)
{
perror("send");
}
if (send_bytes > 0)
{
printf("%s\n", send_buf);
}
// 两个null是服务器的地址信息和长度,不需要则设为null
recv_bytes = recvfrom(client_sockfd, recv_buf, sizeof(recv_buf),
0, NULL, NULL);
if (recv_bytes == -1)
{
perror("recv");
}
if (recv_bytes > 0)
{
printf("%s\n", recv_buf);
}
close(client_sockfd);
printf("client close\n");
// 用完必须删除文件
unlink(SOCK_PATH_C);
return 0;
}
标签:sockaddr,域套,send,server,UNIX,client,sockfd,C语言,recv
From: https://blog.csdn.net/qq_43689451/article/details/136983093