服务端代码:
#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_STREAM, 0); //创建 socket, 通信协议为AF_UNIX, SCK_STREAM 数据方式 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); } if (listen(server_sockfd, MAX_CLIENT_NUM) < 0) { LOG_ERROR(SERVER_DAEMON_ID, "Socket listen failed, error: %s\n", strerror(errno)); exit (EXIT_FAILURE); } printf ("Server is listening and waiting for client connect...\n"); client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, (socklen_t *)&client_len); if (client_sockfd == -1) { perror ("accept"); exit (EXIT_FAILURE); } printf ("Client[%s] is connecting with server...\n", client_address.sun_path); for (i = 0, ch_send = '1'; i < 5; i++, ch_send++) { if ((bytes = read (client_sockfd, &ch_recv, 1)) == -1) { perror ("read"); exit (EXIT_FAILURE); } printf ("The character receiver from client is %c\n", ch_recv); sleep (1); if ((bytes = write (client_sockfd, &ch_send, 1)) == -1) { perror ("write"); 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_STREAM数据方式*/ if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 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 = connect (sockfd, (struct sockaddr *)&address, len); if (result == -1) { printf ("ensure the server is up\n"); perror ("connect"); exit (EXIT_FAILURE); } for (i = 0, ch_send = 'A'; i < 5; i++, ch_send++) { if ((bytes = write(sockfd, &ch_send, 1)) == -1) { /*发消息给服务器*/ perror ("write"); exit (EXIT_FAILURE); } sleep (2); /*休息二秒钟再发一次*/ if ((bytes = read (sockfd, &ch_recv, 1)) == -1) { /*接收消息*/ perror ("read"); exit (EXIT_FAILURE); } printf ("receive from server data is %c\n", ch_recv); } close (sockfd); return (0); }