网络编程之UNIX本地服务器socket通信

UNIX本地服务器通信

UNIX服务器

#include <stdio.h>
#include <sys/types.h>	    
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/socket.h>
#include <linux/un.h>


#define PROTOCO 0
#define ERRO_ -1
#define ERR_LOG(VAL) do{perror(VAL); exit(EXIT_FAILURE);}while(0)


//进程前后台通信,可以先收后发
int main(int argc,const char *argv[])
{
	int sockfd = socket(AF_UNIX,SOCK_STREAM,PROTOCO);
	int connfd;
	char buf[1024] = {0};
	int ret;

	if(sockfd == ERRO_){
		ERR_LOG("socket fail");
	}

	printf("socket success! sockfd = %d\n",sockfd);

	struct sockaddr_un myaddr;
	bzero(&myaddr,sizeof(myaddr));
	myaddr.sun_family = AF_UNIX;
	unlink("/tmp/mysocket");
	strcpy(myaddr.sun_path,"/tmp/mysocket");
	
	ret = bind(sockfd,(struct sockaddr *)&myaddr,sizeof(myaddr));

	if(ret == ERRO_){
		ERR_LOG("bind");
	}

	printf("success bind!\n");

	ret = listen(sockfd,10);//将套结字转为监听套接字

	if(ret == ERRO_){
		ERR_LOG("listen fail");
	}
	
	printf("success listen\n");

	while(1){
		connfd = accept(sockfd,NULL,NULL);
		if(connfd == ERRO_){
			ERR_LOG("accept");
		}

		printf("accept success\n");
		char client_buf[64] = {0};

		while(1){
			ret = read(connfd,buf,sizeof(buf));
			if(ret <= 0){
				printf("客户端异常退出\n");
				break;
			}

			if(strncmp(buf,"quit",4) == 0){
				printf("客户端退出\n");
				break;
			}

			printf("client buf :%s\n",buf);
			write(connfd,buf,ret);
			bzero(buf,sizeof(buf));
	}
	close(connfd);

	}
	close(sockfd);	
	return 0;
}

UNIX服务器客户端

#include <stdio.h>
#include <sys/types.h>	    
#include <sys/socket.h>
#include <stdlib.h>
#include <strings.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/un.h>
//UNIX域套接字
#define ERR_LOG(VAL) do{perror(VAL);exit(EXIT_FAILURE);}while(0)
#define Maxsize 1024
#define ERR_VAL -1

int main(int argc,const char* argv[])
{
	int sockfd;//套接字
	char buf[Maxsize] = {0};

	int connect_return = 0;
	int ret = 0;
	
	sockfd = socket(AF_UNIX,SOCK_STREAM,0);
	if(sockfd == ERR_VAL){
		ERR_LOG("SOCKET");
	}
	
	printf("socket success !socket = %d\n",sockfd);
	
	if(access("/tmp/mysocket",F_OK) == -1){
		perror("");
		exit(1);
	}
	
	struct sockaddr_un myaddr;
	bzero(&myaddr,sizeof(myaddr));
	myaddr.sun_family = AF_UNIX;
	strcpy(myaddr.sun_path,"/tmp/mysocket");

	connect_return = connect(sockfd,(struct sockaddr*)&myaddr,sizeof(myaddr));	


	if(connect_return == ERR_VAL){//向服务器发起连接
		ERR_LOG("connect");
	}

//	printf("connect success!\n");

#if 1 
	while(1){
		printf("INPUT >");
		fflush(stdout);
		ret = read(STDIN_FILENO,buf,sizeof(buf) - 1);
		buf[ret] = '\0';

		if(buf[strlen(buf) - 1] == '\n'){
			buf[strlen(buf) - 1] = '\0';
		}
		ret = write(sockfd,buf,ret);
		if(ret <= 0){
			printf("客户端未写数据退出\n");
			break;
		}
		
		if(strncmp(buf,"quit",4) == 0){
			printf("客户端退出\n");
			break;
		}

		bzero(buf,sizeof(buf));
		ret = read(sockfd,buf,sizeof(buf) - 1);
		buf[ret] = '\0';
		if(buf[strlen(buf) - 1] == '\n'){
			buf[strlen(buf) - 1] = '\0';
		}
		printf("服务器回发%s\n",buf);

	}
#endif
	close(sockfd);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值