Linux网络编程---本地套接字
1.概述
本地套接字
1:作用:本地的进程间通信
2.有关系的进程间通信
3.没有关系的进程间的通信
本地套结字实现流程和网络套结字实现相似,一般采用tcp
二.通信流程
本地套结字通信的流程:
1.服务器端:
1.1 int fd = socket(AF_UNIX/AF_LOCAL,SOCKET_STREAM,0)
1.2 监听的套结字需要绑定本地的套结字文件
struct sockaddr_un addr;
绑定成功后指定的sun_path中的套结字文件会自动生成
bind(fd,addr,len);
1.3 监听
listen(fd,100);
1.4等待并接收连接请求
struct sockaddr_un cliaddr;
int cfd = accept(fd,&cliaddr,&len)
1.5通信
recv
send
1.6关闭
客户端
1. int fd = socket(AF_LOCAL,SOCKET_STREAM,0)
2. 绑定成功后指定的sun_path中的套结字文件会自动生成
struct sockaddr_un addr;
bind(fd,addr,len)
3.连接
struct sockaddr_un serveraddr;
connect(fd,&serveraddr,len);
4.通信
5.关闭
三.实例代码实现
3.1 server
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/un.h>
int main()
{
unlink("server.sock");//解决报错:bind: Address already in use
int sockfd = socket(AF_LOCAL,SOCK_STREAM,0);
if(sockfd == -1)
{
perror("socket");
exit(-1);
}
struct sockaddr_un server;
server.sun_family = AF_LOCAL;
strcpy(server.sun_path,"server.sock");
if(bind(sockfd,(const struct sockaddr*)&server,sizeof(server)) == -1)
{
perror("bind");
exit(-1);
}
if(listen(sockfd, 100) == -1)
{
perror("listen");
exit(-1);
}
struct sockaddr_un client;
int len = sizeof(client);
int client_fd = accept(sockfd,(struct sockaddr*)&client,&len);
if(client_fd == -1)
{
perror("accept");
exit(-1);
}
char recvbuff[1024] = {0};
char *server_data = "i am server";
while(1)
{
memset(recvbuff,0,sizeof(recvbuff));
len = recv(client_fd,recvbuff,sizeof(recvbuff),0);
if(len == -1)
{
perror("recv");
break;
}
else if(len == 0)
{
printf("客户端断开连接\n");
}
else if(len > 0)
{
printf("recv client data : %s\n",recvbuff);
}
send(client_fd,server_data,strlen(server_data),0);
}
close(sockfd);
close(client_fd);
return 0;
}
3.2 client
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/un.h>
int main()
{
unlink("client.sock"); //解决报错:bind: Address already in use
int sockfd = socket(AF_LOCAL,SOCK_STREAM,0);
if(sockfd == -1)
{
perror("socket");
exit(-1);
}
struct sockaddr_un server;
server.sun_family = AF_LOCAL;
strcpy(server.sun_path,"client.sock");
if(bind(sockfd,(const struct sockaddr*)&server,sizeof(server)) == -1)
{
perror("bind");
exit(-1);
}
struct sockaddr_un client;
client.sun_family = AF_LOCAL;
memcpy(client.sun_path,"server.sock",sizeof("server.sock"));
int len = sizeof(client);
int ret = connect(sockfd,(const struct sockaddr*)&client,len);
if(ret == -1)
{
perror("connect");
exit(-1);
}
int num = 0;
char buff[1024];
while(1)
{
memset(buff,0,sizeof(buff));
printf("client say: %s\n",buff);
sprintf(buff,"hello i am clinet------%d\n",num++);
len = send(sockfd,buff,strlen(buff)+1,0);
if(len == -1)
{
perror("send");
break;
}
sleep(1);
memset(buff,0,sizeof(buff));
len = recv(sockfd,buff,sizeof(buff),0);
if(len == -1)
{
perror("recv");
exit(-1);
}
else if(len == 0)
{
printf("服务端断开连接\n");
}
else if(len > 0)
{
printf("recv server data : %s\n",buff);
}
}
close(sockfd);
return 0;
}
原文地址:https://blog.csdn.net/zbz123456789_/article/details/144334299
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!