流式域套接字>>
服务器端代码:
#include<myhead.h>
int main(int argc, const char *argv[])
{
int sfd=socket(AF_UNIX,SOCK_STREAM,0);
if(sfd==-1)
{
perror("socket error");
return -1;
}
if(access("./mysocket",F_OK)==0)
{
//说明存在,将该文件删除
if(unlink("./mysocket")==-1)
{
perror("unlink error");
return -1;
}
}
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./mysocket");
if(bind(sfd,(struct sockaddr *)&sun,sizeof(sun))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
if(listen(sfd,128)==-1)
{
perror("listen error");
return -1;
}
printf("listen success\n");
int newfd=-1;
if((newfd=accept(sfd,NULL,NULL))==-1)
{
perror("accept error");
return -1;
}
printf("有新客户发来连接请求\n");
char rbuf[128];
while(1)
{
bzero(rbuf,sizeof(rbuf));
recv(newfd,rbuf,sizeof(rbuf),0);
printf("收到的客户端消息:%s\n",rbuf);
strcat(rbuf,"收到");
send(newfd,rbuf,strlen(rbuf),0);
printf("发送成功\n");
}
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("sock error");
return -1;
}
printf("cfd=%d\n",cfd);
if(access("./mysocket1",F_OK)==0)
{
if(unlink("./mysocket1")==-1)
{
perror("unlink error");
return -1;
}
}
//2、绑定(非必须)
struct sockaddr_un cun;
cun.sun_family=AF_UNIX;
strcpy(cun.sun_path,"./mysocket1");
if(bind(cfd,(struct sockaddr *)&cun,sizeof(cun))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
//3、链接服务器
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./mysocket");
if(connect(cfd,(struct sockaddr *)&sun,sizeof(sun))==-1)
{
perror("const 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;
}
报式域套接字>>
服务器端代码:
#include<myhead.h>
int main(int argc, const char *argv[])
{
int sfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(sfd==-1)
{
perror("socket error");
return -1;
}
printf("sfd=%d\n",sfd);
if(access("./linux1",F_OK)==0)
{
if(unlink("./linux1")==-1)
{
perror("unlink error");
return -1;
}
}
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./linux1");
if(bind(sfd,(struct sockaddr *)&sun,sizeof(sun))==-1)
{
perror("bind error");
return -1;
}
printf("bind access\n");
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;
}
}
close(sfd);
return 0;
}
客户端代码:
#include<myhead.h>
int main(int argc, const char *argv[])
{
int cfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(cfd==-1)
{
perror("socket error");
return -1;
}
printf("cfd=%d\n",cfd);
if(access("./linux2",F_OK)==0)
{
if(unlink("./linux2")==-1)
{
perror("unlink error");
return -1;
}
}
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;
}
printf("bind access\n");
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);
}
close(cfd);
return 0;
}