1、流式域套接字
1>tcp服务端实现
#include<myhead.h>
int main(int argc, const char *argv[])
{//1、创建套接字int sfd = socket(AF_UNIX, SOCK_STREAM, 0);if(sfd == -1){perror("socket error");return -1;}//2、判断套接字文件是否存在,如果存在则需要删除if(access("./mysocket", F_OK) == 0){//说明存在,将该文件删除if(unlink("./mysocket") == -1){perror("unlink error");return -1;}}//绑定套接字文件//2.1 填充地址信息结构体struct sockaddr_un sun;sun.sun_family = AF_UNIX;strcpy(sun.sun_path, "./mysocket");//2.2 绑定 if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) == -1){perror("bind error");return -1;}printf("bind success\n");//3、监听if(listen(sfd,128) == -1){perror("listen 128");return -1;}printf("listen success\n");//4、阻塞等待客户端连接需求//定义容器接受客户端地址信息struct sockaddr_un cun;socklen_t socklen = sizeof(cun);int newfd = -1;if((newfd = accept(sfd, (struct sockaddr*)&cun, &socklen)) == -1){perror("accept error");return -1;}printf("有新客户发来连接请求\n");//5、数据收发char rbuf[128]= "";while(1){//清空bzero(rbuf, sizeof(rbuf));recv(newfd, rbuf, sizeof(rbuf), 0);printf("[%s]:%s\n",cun.sun_path, rbuf);//连接一个笑脸回复strcat(rbuf, "*_*");//发送回去send(newfd, rbuf, strlen(rbuf), 0);printf("发送成功\n");}//6、关闭套接字close(sfd);close(newfd);return 0;
}
客户端实现
#include<myhead.h>int main(int argc, const char *argv[])
{//1、创建用于通信的套接字文件描述符int cfd = socket(AF_UNIX, SOCK_STREAM, 0);if(cfd == -1){perror("socket error");return -1;}printf("cfd = %d\n", cfd); //3//判断套接字文件是否存在,如果存在则需要删除if(access("./mysocket1", F_OK) == 0){//说明存在,将该文件删除if(unlink("./mysocket1") == -1){perror("unlink error");return -1;}}//2、绑定(非必须)//2.1 填充地址信息结构体struct sockaddr_un cun;cun.sun_family = AF_UNIX;strcpy(cun.sun_path, "./mysocket1");//2.2 绑定if(bind(cfd, (struct sockaddr*)&cun, sizeof(cun)) == -1){perror("bind error");return -1;}printf("bind success\n");//3、连接服务器//3.1填充要连接的服务器地址信息结构体struct sockaddr_un sun;sun.sun_family = AF_UNIX; //地址族strcpy(sun.sun_path, "./mysocket"); //套接字文件//3.2 连接服务器if(connect(cfd, (struct sockaddr*)&sun, sizeof(sun))==-1){perror("connect error");return -1;}printf("connect success\n");//4、数据收发char wbuf[128] = "";while(1){//清空数据bzero(wbuf, sizeof(wbuf));printf("请输入>>>");fgets(wbuf, sizeof(wbuf), stdin); //从终端输入wbuf[strlen(wbuf)-1] = 0;//将数据发送给服务器send(cfd, wbuf, strlen(wbuf), 0);printf("发送成功\n");//判断发送的数据if(strcmp(wbuf, "quit") == 0){break;}//接收服务器发来的消息//清空数据bzero(wbuf, sizeof(wbuf));recv(cfd, wbuf, sizeof(wbuf), 0); printf("收到消息为:%s\n", wbuf);}//5、关闭套接字close(cfd);return 0;
}
2>报式域套接字
服务端实现:
#include<myhead.h>int main(int argc, const char *argv[])
{//1、创建用于通信的套接字int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);if(sfd == -1){perror("socket error");return -1;}printf("sfd = %d\n", sfd); //3//判断套接字文件是否存在,如果存在则需要删除if(access("./linux1", F_OK) == 0){//说明存在,将该文件删除if(unlink("./linux1") == -1){perror("unlink error");return -1;}}//2、绑定IP地址和端口号//2.1填充地址信息结构体struct sockaddr_un sun;sun.sun_family = AF_UNIX; //地址族strcpy(sun.sun_path, "./linux1");//2,2绑定if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) == -1){perror("bind error");return -1;}printf("bind success\n");//3、收发数据char rbuf[128] = "";//定义容器接收对端地址信息结构体struct sockaddr_un cun;socklen_t socklen = sizeof(cun);while(1){//清空数组bzero(rbuf, sizeof(rbuf));//接收数据recvfrom(sfd, rbuf, sizeof(rbuf), 0, (struct sockaddr*)&cun, &socklen);printf("收到消息为:%s\n", rbuf);//将消息加个笑脸回过去strcat(rbuf, "*_*");if(sendto(sfd, rbuf, strlen(rbuf), 0, (struct sockaddr*)&cun, socklen) == -1){perror("sendto error");return -1;}}//4、关闭套接字close(sfd);return 0;
}
客户端实现:
#include<myhead.h>int main(int argc, const char *argv[])
{//1、创建用于通信的套接字int cfd = socket(AF_UNIX, SOCK_DGRAM, 0);if(cfd == -1){perror("socket error");return -1;}printf("cfd = %d\n", cfd); //3//判断套接字文件是否存在,如果存在则需要删除if(access("./linux2", F_OK) == 0){//说明存在,将该文件删除if(unlink("./linux2") == -1){perror("unlink error");return -1;}}//2、绑定IP地址和端口号(如果要接收服务器消息必须绑定)struct sockaddr_un cun;cun.sun_family = AF_UNIX;strcpy(cun.sun_path, "./linux2");if(bind(cfd, (struct sockaddr*)&cun, sizeof(cun))==-1){perror("bind error");return -1;}//3、收发数据char wbuf[128] = "";//定义容器,记录服务器的地址信息结构体struct sockaddr_un sun;sun.sun_family = AF_UNIX;strcpy(sun.sun_path,"./linux1");while(1){//清空数组bzero(wbuf, sizeof(wbuf));//从终端获取数据printf("请输入>>>");fgets(wbuf, sizeof(wbuf), stdin);wbuf[strlen(wbuf)-1] = 0;//将数据发送给服务器sendto(cfd, wbuf, sizeof(wbuf), 0, (struct sockaddr*)&sun, sizeof(sun));printf("发送成功\n");//接收服务器回复的消息bzero(wbuf, sizeof(wbuf));recvfrom(cfd, wbuf, sizeof(wbuf), 0, NULL, NULL);printf("收到消息为:%s\n", wbuf);}//4、关闭套接字close(cfd);return 0;
}