基于UDP的C/S网络编程模型(使用sendto和recvfrom函数)

服务端代码:

#include  
#include  
#include 
#include  
#include 
#include 

#define MAX_CLIENT_NUM  10

#define LOG_ERROR(module_id, format...) \
do{ \
	printf(format); \
}while(0)


int main(int argc, char *argv[])
{
	int server_sockfd, client_sockfd;
	int server_len, client_len;
	struct sockaddr_un server_sockaddr;
	struct sockaddr_un client_address;
	int i;
	int bytes;
	char ch_send,ch_recv;
	
	unlink ("/home/user/test/server");
	
	server_sockfd = socket(AF_UNIX, SOCK_DGRAM, 0); //创建 socket, 通信协议为AF_UNIX, SOCK_DGRAM 数据方式
	if (server_sockfd < 0)
	{
		LOG_ERROR(SERVER_DAEMON_ID, "Socket create failed, error: %s\n",  strerror(errno));
		exit (EXIT_FAILURE);
	}

	bzero(&server_sockaddr, sizeof(server_sockaddr));
	server_sockaddr.sun_family = AF_UNIX ; //配置服务器信息(通信协议)
	strcpy(server_sockaddr.sun_path, "/home/user/test/server");
	server_len = sizeof(server_sockaddr); //配置服务器信息(服务器地址长度)
	
	if (bind(server_sockfd, (struct sockaddr *)&server_sockaddr, server_len) < 0)
	{
		LOG_ERROR(SERVER_DAEMON_ID, "Socket bind failed, error: %s\n", strerror(errno));
		exit (EXIT_FAILURE);
	}


	client_len = sizeof(client_address);

	for (i = 0, ch_send = '1'; i < 5; i++, ch_send++) 
	{
		if ((bytes = recvfrom(server_sockfd, &ch_recv, 1, 0, (struct sockaddr *)&client_address, (socklen_t *)&client_len)) == -1) 
		{
			perror ("recvfrom");
			exit (EXIT_FAILURE);
		}


		printf ("The character receiver from client[%s] is %c\n", client_address.sun_path, ch_recv);
		sleep (1);
		if ((bytes = sendto(server_sockfd, &ch_send, 1, 0, (struct sockaddr *)&client_address, client_len)) == -1) 
		{
			perror ("sendto");
			exit (EXIT_FAILURE);
		}
			
	}
	
	close (client_sockfd);
	unlink ("/home/user/test/server");

}

客户端代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include  

int main (int argc, char *argv[])
{
    struct sockaddr_un address,client_address;
    int sockfd;
    int len;
    int i, bytes;
    int result;
    char ch_recv, ch_send;

    unlink ("FuckYouServer");
	
	/*创建socket,AF_UNIX通信协议,SOCK_DGRAM数据方式*/
    if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) {
            perror ("socket");
            exit (EXIT_FAILURE);
    }
	
    client_address.sun_family = AF_UNIX;
    strncpy (client_address.sun_path, "FuckYouServer", strlen("FuckYouServer")+1);
    if (bind(sockfd, (struct sockaddr *)&client_address, sizeof(client_address)) < 0)
    {
    	printf("Socket bind failed, error: %s\n", strerror(errno));
    	exit (EXIT_FAILURE);
    }
	
    address.sun_family = AF_UNIX;
    strcpy (address.sun_path, "/home/user/test/server");
    len = sizeof (address);

	result = sizeof (address);
	for (i = 0, ch_send = 'A'; i < 5; i++, ch_send++) {
		if ((bytes = sendto(sockfd, &ch_send, 1, 0, (struct sockaddr *)&address, len))  == -1 ){ /*发消息给服务器*/
            perror ("sendto");
            exit (EXIT_FAILURE);
        }
		
		sleep (2);    /*休息二秒钟再发一次*/

        if ((bytes = recvfrom(sockfd, &ch_recv, 1, 0, (struct sockaddr *)&address, &result)) == -1) { /*接收消息*/
            perror ("recvfrom");
            exit (EXIT_FAILURE);
        }

        printf ("receive from server data is %c\n", ch_recv);
	}

    close (sockfd);

    return (0);
}

你可能感兴趣的:(网络编程,sendto,recvfrom)