流式域服务
#include <myhead.h>
#define BACKLOG 10
int main(int argc, const char *argv[])
{
int oldfd = socket(AF_UNIX,SOCK_STREAM,0);//1、创建流式域套接字
if(oldfd == -1)
{
perror("socket");
return -1;
}
if(access("./myserver",F_OK)==0)//2、判断文件存在存在就删除
{
if(unlink("./myserver")==-1)
{
perror("unlink");
return -1;
}
}
printf("删除文件成功\n");
struct sockaddr_un server;//3、定义结构体信息并绑定
server.sun_family = AF_UNIX;
strcpy(server.sun_path,"./myserver");
if(bind(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("bind");
return -1;
}
printf("绑定成功\n");
if(listen(oldfd,BACKLOG)==-1)//4、监听
{
perror("listen");
return -1;
}
printf("监听成功\n");
int newfd = accept(oldfd,NULL,NULL);//5、接收客户端请求
if(newfd==-1)
{
perror("accept");
return -1;
}
printf("接收客户端请求成功\n");
char buff[1024];
while(1)//6、数据收发
{
int len = recv(newfd,buff,sizeof(buff),0);
if(len==0)
{
printf("客户端跑了\n");
break;
}
strcat(buff,"^_^");
send(newfd,buff,sizeof(buff),0);
printf("转发成功\n");
}
close(newfd);
close(oldfd);
return 0;
}
流式域客户端
#include <myhead.h>
int main(int argc, const char *argv[])
{
int oldfd = socket(AF_UNIX,SOCK_STREAM,0);//1、创建套接字
if(oldfd==-1)
{
perror("socket");
return -1;
}
if(access("./myclient",F_OK)==0)//2、判断文件是否存在,存在就删除
{
if(unlink("./myclient")==-1)
{
perror("unlink");
return -1;
}
}
struct sockaddr_un client;
client.sun_family =AF_UNIX;
strcpy(client.sun_path,"./myclient");
if(bind(oldfd,(struct sockaddr *)&client,sizeof(client))==-1)//3、绑定(可选)
{
perror("bind");
return -1;
}
struct sockaddr_un server;
server.sun_family =AF_UNIX;
strcpy(server.sun_path,"./myserver");//很重要,连接服务器的文件,而不是自己的文件
if(connect(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)//4、连接服务器
{
perror("connect");
return -1;
}
printf("连接服务器成功\n");
char buff[1024];
while(1)
{
bzero(buff,sizeof(buff));
printf("请输入要发送的信息:");
fgets(buff,sizeof(buff),stdin);
buff[strlen(buff)-1] = '\0';
send(oldfd,buff,sizeof(buff),0);//发送信息
printf("发送成功\n");
if(strcmp(buff,"quit")==0)
{
break;
}
recv(oldfd,buff,sizeof(buff),0);//接收服务器信息
printf("接收服务器的信息:%s\n",buff);//输出接收到的信息
}
close(oldfd);
return 0;
}
报式域服务
#include <myhead.h>
int main(int argc, const char *argv[])
{
int oldfd = socket(AF_UNIX,SOCK_DGRAM,0);//1、创建报式域套接字
if(oldfd==-1)
{
perror("socket");
return -1;
}
if(access("./myserver",F_OK)==0)//2、判断文件存在就删除
{
if(unlink("./myserver")==-1)
{
perror("unlink");
return -1;
}
}
struct sockaddr_un server;
server.sun_family = AF_UNIX;
strcpy(server.sun_path,"./myserver");
if(bind(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)//3、绑定
{
perror("bind");
}
//4、收发消息
struct sockaddr_un client;//定义客户端信息结构体
int clientlen = sizeof(client);
char buff[1024];
while(1)
{
bzero(buff,sizeof(buff));
recvfrom(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&client,&clientlen);//接收客户端信息
printf("接收到%s的消息\n",client.sun_path);
printf("收到客户端信息:%s\n",buff);
strcat(buff,"*_*");
sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&client,sizeof(client));
printf("转发成功\n");
}
close(oldfd);//关闭文件描述符
return 0;
}
报式域客户端
int main(int argc, const char *argv[])
{
int oldfd = socket(AF_UNIX,SOCK_DGRAM,0);//1、创建报式域套接字
if(oldfd==-1)
{
perror("socket");
return -1;
}
if(access("./myclient",F_OK)==0)//2、判断文件存在就删除
{
if(unlink("./myclient")==-1)
{
perror("unlink");
return -1;
}
}
//3、一定绑定
struct sockaddr_un client;//定义客户端信息结构体
client.sun_family = AF_UNIX;
strcpy(client.sun_path,"./myclient");
if(bind(oldfd,(struct sockaddr *)&client,sizeof(client))==-1)
{
perror("bind");
return -1;
}
//4、收发消息
struct sockaddr_un server;//定义服务器端信息结构体
server.sun_family = AF_UNIX;
strcpy(server.sun_path,"./myserver");//注意这里填充服务器端的文件
char buff[1024];
while(1)
{
printf("请输入要发送的信息:");
fgets(buff,sizeof(buff),stdin);//键盘输入信息
buff[strlen(buff)-1]='\0';
int len = sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&server,sizeof(server));//发送信息
if(len==0)
{
printf("发送失败\n");
break;
}
recvfrom(oldfd,buff,sizeof(buff),0,NULL,NULL);//接收服务器消息
printf("服务器发来信息:%s\n",buff);
}
return 0;
}