【无标题】

流式域套接字>> 

服务器端代码:

#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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值