UNIX本地套接字 示例

server.c:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>


#if 0
#define SOCK_UNIX_FILE "/tmp/video1_sock"
#else
#define SOCK_UNIX_FILE "/tmp/video_sock"
#endif
#define MAX_TRANSMIT_DATA_LEN 10240
#define S_TIME_OUT 2000 //2s

int tcp_write(int socketfd, char* buf, int len,unsigned int timeout_ms)
{
	int ret;
	int total_lenth = 0;
	int len_remain = len;
	char *write_position = buf;
	struct timeval timeout;
	fd_set  wset, eset;
	int status = 1;

	timeout.tv_sec =timeout_ms/1000;
	timeout.tv_usec = timeout_ms%1000;
		printf("%s  %d \n",__func__,__LINE__);
	while(1){
		
		FD_ZERO(&wset);
		FD_ZERO(&eset);
		FD_SET(socketfd, &eset);
		FD_SET(socketfd, &wset);	
		ret = select(socketfd+1, NULL, &wset, &eset, &timeout);
		printf("%s  %d \n",__func__,__LINE__);
		if( ( timeout.tv_sec == 0)  && ( timeout.tv_usec == 0) ){
			//timeout
			printf(" write timeout \n");
			break;
		}
		printf("%s  %d \n",__func__,__LINE__);
		if(FD_ISSET(socketfd, &wset)){
		printf("%s  %d \n",__func__,__LINE__);
			ret = send(socketfd, write_position, len_remain, 0);
			printf("%s  %d \n",__func__,__LINE__);
			if(ret < 0){
				printf(" write err \n");
				return -1;
			}

			if(ret == 0){
				printf(" write ==0 ... \n");

			}
		printf("%s  %d \n",__func__,__LINE__);
			write_position += ret;
			total_lenth += ret;
			len_remain -= ret;
			if(0 == len_remain) {
				break;
			}
			
		}

		if(FD_ISSET(socketfd, &eset)){
			printf("write err \n");
			break;

		}


	}

	//printf("write %d \n",len -len_remain);
	return len -len_remain ;	
}


int tcp_read(int socketfd, char* buf, int len,unsigned int timeout_ms)
{
	int ret;
	int total_lenth = 0;
	int len_remain = len;
	char *read_position = buf;
	struct timeval timeout;
	fd_set  rset, eset;

	timeout.tv_sec =timeout_ms/1000;
	timeout.tv_usec = timeout_ms%1000;
	//printf("read timeout set %d %d\n",timeout.tv_sec,timeout.tv_usec);

		
	while(1){
		
		FD_ZERO(&rset);
		FD_ZERO(&eset);
		FD_SET(socketfd, &eset);
		FD_SET(socketfd, &rset);	

		
		ret = select(socketfd+1, &rset, NULL, &eset, &timeout);
		
		if( ( timeout.tv_sec == 0)  && ( timeout.tv_usec == 0) ){
			//timeout
			//printf(" read timeout \n");
			break;
		}

		if(FD_ISSET(socketfd, &rset)){
			ret = recv(socketfd, read_position, len_remain, 0);
			if(ret < 0){
				//error
				//printf("read err\n");
				break;
			}
			if(ret == 0){
				//printf(" read %d..\n",ret);
				// peer close
				//printf(" peer close ?\n");
				break;
			}
				//printf(" read %d...\n",ret);
			
			read_position += ret;
			total_lenth += ret;
			len_remain -= ret;
			if(len_remain <=0) break;
			
		}

		if(FD_ISSET(socketfd, &eset)){
			printf("read err \n");
			break;

		}

	}
	//printf("time remain %d %d\n",timeout.tv_sec,timeout.tv_usec);
	return total_lenth;	
}


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>

int peer_sock_status = 0;
static void sig_handle(int signo)
{
	switch(signo)
	{

	case SIGTERM:
		printf("receive SIGTERM!\n");
		exit(0);
		break;
	case SIGKILL:
		printf("receive SIGKILL!\n");
		exit(0);
		break;			
	case SIGINT:
		printf("\nreceive Ctrl+c!Then, the server will exit after 3 seconds !\n");
		sleep(3);
		exit(0);
		break;
	case SIGALRM:
		printf("receive SIGALRM!\n");
		break;
	case SIGILL:
		printf("receive SIGILL!\n");
		break;
	case SIGSEGV:
			printf("receive SIGSEGV!\n");
		break;
	case SIGPIPE:
			peer_sock_status = 0;
			printf("receive SIGPIPE!\n");
			break;
	default:
		printf("receive unknown signal(%d)!\n", signo);
		break;
	}
}

int main (int argc, char *argv[])
{
        int server_sockfd, client_sockfd;
        int server_len, client_len;
        static int i_tmp = 0;
        struct sockaddr_un server_address;     
        struct sockaddr_un client_address;
        int i, bytes;
        char ch_send, ch_recv;
				char s_buf[MAX_TRANSMIT_DATA_LEN];
				char r_buf[8];
				unsigned int len = 0;
        unlink (SOCK_UNIX_FILE);//delete the file link for the function of bind   

       
        server_sockfd = socket (AF_UNIX, SOCK_STREAM, 0);

       
        server_address.sun_family = AF_UNIX;

       
        strcpy (server_address.sun_path, SOCK_UNIX_FILE);

       
        server_len = sizeof (server_address);

       
        bind (server_sockfd, (struct sockaddr *)&server_address, server_len);

       
        listen (server_sockfd, 5);//the num of the client is five

        printf ("Server is waiting for client connect...\n");

        client_len = sizeof (client_address);
#if 1
	for(i=1; i<=SIGIO; i++) signal(i, sig_handle);
#endif
accept_again:
				if(i_tmp != 0)
				{
        	printf ("\nServer is waiting for client connect...\n");
					}
        client_sockfd = accept (server_sockfd, (struct sockaddr *)&server_address, (socklen_t *)&client_len);
        
        if (client_sockfd == -1) {
                perror ("accept");
                exit (EXIT_FAILURE);
        }
				peer_sock_status =1;
        printf ("The server is waiting for client data...\n");
        strcpy(s_buf,"the tcp send stream to jss_server");

	while(1){

								memset(s_buf,0,MAX_TRANSMIT_DATA_LEN);
								memset(r_buf,0,8);
								sprintf(s_buf,"the %dth packet to jss_server !",i_tmp++);
		 						printf("write data :");

                if ((bytes = tcp_write(client_sockfd, s_buf, MAX_TRANSMIT_DATA_LEN,S_TIME_OUT)) <= 0) {
                        perror ("WWrite");
                        goto accept_again;

          			}
          			

								len += bytes;
								printf(" %d %d\n",bytes,len);
							
								usleep(2);
								printf(" %s %d The status of the client is %s \n",__FUNCTION__,__LINE__,r_buf);
	}
                close (client_sockfd);
                unlink ("server socket");
				 				goto accept_again;
}


 

client.c:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>

#if 1
#define SOCK_UNIX_FILE "/tmp/video1_sock"
#else
#define SOCK_UNIX_FILE "/tmp/video_sock"
#endif
#define MAX_TRANSMIT_DATA_LEN 10240
#define C_TIME_OUT 2000 //2s

int tcp_read(int socketfd, char* buf, int len,unsigned int timeout_ms)
{
	int ret;
	int total_lenth = 0;
	int len_remain = len;
	char *read_position = buf;
	struct timeval timeout;
	fd_set  rset, eset;

	timeout.tv_sec =timeout_ms/1000;
	timeout.tv_usec = timeout_ms%1000;
	//printf("read timeout set %d %d\n",timeout.tv_sec,timeout.tv_usec);

		
	do{
		
		FD_ZERO(&rset);
		FD_ZERO(&eset);
		FD_SET(socketfd, &eset);
		FD_SET(socketfd, &rset);	

		
		ret = select(socketfd+1, &rset, NULL, &eset, &timeout);
		
		if( ( timeout.tv_sec == 0)  && ( timeout.tv_usec == 0) ){
			//timeout
			//printf(" read timeout \n");
			break;
		}

		if(FD_ISSET(socketfd, &rset)){
			ret = recv(socketfd, read_position, len_remain, 0);
			if(ret < 0){
				//error
				//printf("read err\n");
				break;
			}
			if(ret == 0){
				//printf(" read %d..\n",ret);
				// peer close
				//printf(" peer close ?\n");
				break;
			}
				//printf(" read %d...\n",ret);
			
			read_position += ret;
			total_lenth += ret;
			len_remain -= ret;
			if(len_remain <=0) break;
			
		}

		if(FD_ISSET(socketfd, &eset)){
			printf("read err \n");
			break;

		}

	}while(0);
	
	
	//printf("time remain %d %d\n",timeout.tv_sec,timeout.tv_usec);
	return total_lenth;	
}

int tcp_write(int socketfd, char* buf, int len,unsigned int timeout_ms)
{
	int ret;
	int total_lenth = 0;
	int len_remain = len;
	char *write_position = buf;
	struct timeval timeout;
	fd_set  wset, eset;

	timeout.tv_sec =timeout_ms/1000;
	timeout.tv_usec = timeout_ms%1000;

	while(1){
		
		FD_ZERO(&wset);
		FD_ZERO(&eset);
		FD_SET(socketfd, &eset);
		FD_SET(socketfd, &wset);	
		ret = select(socketfd+1, NULL, &wset, &eset, &timeout);
		
		if( ( timeout.tv_sec == 0)  && ( timeout.tv_usec == 0) ){
			//timeout
			printf(" write timeout \n");
			break;
		}
		
		if(FD_ISSET(socketfd, &wset)){
			ret = send(socketfd, write_position, len_remain, 0);
			
			if(ret < 0){
				printf(" write err \n");
		
			}

			if(ret == 0){
				printf(" write ==0 ... \n");

			}

			write_position += ret;
			total_lenth += ret;
			len_remain -= ret;
			if(0 == len_remain) {
				break;
			}
			
		}

		if(FD_ISSET(socketfd, &eset)){
			printf(" err \n");
			break;

		}


	}

	//printf("write %d \n",len -len_remain);
	return len -len_remain ;	
}


int main (int argc, char *argv[])
{

    struct sockaddr_un address;
    int sockfd;
    int len;
    int i, bytes;
    int result;
    char ch_recv, ch_send;
    char r_buf[MAX_TRANSMIT_DATA_LEN];
    char s_buf[8];
    strcpy(s_buf,"LIVING");
    len = 0;
   
    if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
            perror ("socket");
            exit (EXIT_FAILURE);
    }

    address.sun_family = AF_UNIX;
    strcpy (address.sun_path, SOCK_UNIX_FILE);
    len = sizeof (address);

	    result = connect (sockfd, (struct sockaddr *)&address, len);
	    if (result == -1) {
	        printf ("ensure the server is up\n");
	        perror ("connect");
		 			exit (EXIT_FAILURE);
	    }
printf("%s %d Connect succeed !\n",__FUNCTION__,__LINE__);


	while(1){
		//memset(r_buf,0,MAX_TRANSMIT_DATA_LEN);
					bytes = tcp_read (sockfd, r_buf, MAX_TRANSMIT_DATA_LEN,C_TIME_OUT);
				//bytes = recv(sockfd, r_buf, 10240, 0);
	        if (bytes <= 0) {
	            perror ("read");
	            exit (EXIT_FAILURE);
	        }
		
					len +=bytes;
					printf("read data %d, %d\n",bytes,len);

					//sleep(5);
	}

    close (sockfd);

    return (0);
	
}




 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值