功能描述:程序应用多线程技术,可是实现1对N进行网络通信聊天。但至今没想出合适的退出机制,除了用Ctr+C。出于演示目的,这里采用UNIX域协议(文件系统套接字),程序分为客户端和服务端。应用select函数来实现异步的读写操作。
先说一下服务端:首先先创建套接字,然后绑定,接下进入一个无限循环,用accept函数,接受“连接”请求,然后调用创建线程函数,创造新的线程,进入下一个循环。这样每当有一个新的“连接”被接受都会创建一个新的线程,实现1对N的网络通信。在服务端程序中线程中用一个buffer读写,为了避免错误,这时就要给关键代码加上互斥锁work_mutex,具体见代码。
服务器代码
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<pthread.h> #include<sys/socket.h> #include<sys/un.h> #include<unistd.h> #include<semaphore.h> //这里没有用二进制信号量可以删掉 char buffer[1024]; //读写用的区域 sem_t bin_sem; //没用到的二进制信号量,可以删掉 void *pthread_function(void *arg); //线程入口函数声明 pthread_mutex_t work_mutex; //声明互斥锁 int main(){ int result; //整数变量用来储存调用函数的返回值 struct sockaddr_un server_address, client_address; //UNIX域的套接字,server_address用于服务端的监听,client_address用于客户端连接后的套接字 int client_len; //连接后,accept函数会把客户端的地址的长度储存在这 int server_socketfd, client_socketfd;//服务端和客户端的套接字文件描述符 pthread_t a_thread; //线程ID标志 pthread_attr_t thread_attr; //线程的属性,后面可以看的,被我注释掉了,没用到,可以删掉。 result = sem_init(&bin_sem, 0, 1); //初始化二进制信号量,因为用了互斥锁,所以没用到,可以删掉 if(result != 0){ perror("sem_init"); exit(EXIT_FAILURE); } result = pthread_mutex_init(&work_mutex, NULL);//初始化互斥锁 if(result != 0){ perror("pthread_mutex_init"); exit(EXIT_FAILURE); } server_socketfd = socket(AF_UNIX, SOCK_STREAM, 0);//创建套接字,用TCP连接方式,出于演示目的只用UNIX域套接字。 server_address.sun_family = AF_UNIX; strcpy(server_address.sun_path, "server_socket"); unlink("server_socket"); //在绑定之前,把以前存在当前目录下的套接字删除 result = bind(server_socketfd, (struct sockaddr*)&server_address, sizeof(server_address)); //绑定 if(result != 0){ perror("bind"); exit(EXIT_FAILURE); } result = listen(server_socketfd, 5);//监听,最多允许5个连接请求 if(result != 0){ perror("listen"); exit(EXIT_FAILURE); } client_len = sizeof(client_address); while(1){ //开始进入无限循环 /* printf("If you want to quit, please enter 'quit'\n"); printf("Do you want to accept a connectiong\n"); memset(buffer, '\0', sizeof(buffer)); fgets(buffer, sizeof(buffer), stdin); if((strncmp("quit", buffer, 4))==0) break; */ client_socketfd = accept(server_socketfd, (struct sockaddr*)&client_address, &client_len); //接受一个连接请求 /* result = pthread_attr_init(&thread_attr); if(result != 0){ perror("pthread_attr_init"); exit(EXIT_FAILURE); } result = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); if(result != 0){ perror("pthread_attr_setdetachstate"); exit(EXIT_FAILURE); } */ result = pthread_create(&a_thread, NULL, pthread_function, (void *)client_socketfd); //成功接受一个请求后,就会创建一个线程,然后主线程又进入accept函数,如果此时没有连接请求,那么主线程会阻塞 if(result != 0){ perror("pthread_create"); exit(EXIT_FAILURE); } } } void *pthread_function(void *arg){ //线程入口函数,每调用一次pthread_create,都会创建一个新的线程 int fd = (int) arg; //把函数参数,即连接成功后的套接字,赋给fd. int result; fd_set read_fds; //文件描述符集合,用于select函数 int max_fds; //文件描述符集合的最大数 printf("%d id has connected!!\n", fd); while (1){ FD_ZERO(&read_fds);//清空集合 FD_SET(0, &read_fds);//将标准输入放入监听的文件描述符集合, 这个用于读取标准输入,即键盘的输入 FD_SET(fd, &read_fds);//将连接后的客户文件描述符放入监听的文件描述符集合, 这个用于向客户端读取数据 max_fds = fd + 1; // sem_wait(&bin_sem); pthread_mutex_lock(&work_mutex); //对关键区域上锁 printf("%d has get the lock\n", fd); result = select(max_fds, &read_fds, (fd_set *)NULL, (fd_set *)NULL, (struct timeval*)NULL); //开始监听那些文件描述符出于可读状态 if(result < 1){ printf("select"); } if(FD_ISSET(0, &read_fds)){ //如果标准输入处于可读状态,说明键盘有所输入,将输入的数据存放在buffer中,然后向客户端写回,如果输入“quit”将会退出一个聊天线程 memset(buffer, '\0', sizeof(buffer)); //保险起见,清零 fgets(buffer, sizeof(buffer), stdin); if((strncmp("quit", buffer, 4))==0){ printf("You have terminaled the chat\n"); // sem_post(&bin_sem); pthread_mutex_unlock(&work_mutex); break; } else{ result=write(fd, buffer, sizeof(buffer)); if(result==-1){ perror("write"); exit(EXIT_FAILURE); } } } if(FD_ISSET(fd, &read_fds)){ //如果客户套接字符可读,那么读取存放在buffer中,然后显示出来,如果对方中断聊天,那么result==0 memset(buffer, '\0', sizeof(buffer)); result = read(fd, buffer, sizeof(buffer)); if(result == -1){ perror("read"); exit(EXIT_FAILURE); } else if(result == 0){ printf("The other side has terminal the chat\n"); // sem_post(&bin_sem); pthread_mutex_unlock(&work_mutex); break; } else{ printf("receive message: %s", buffer); } } pthread_mutex_unlock(&work_mutex); //解锁 sleep (1); //如果没有这一行,当前线程会一直占据buffer.让当前线程暂停一秒可以实现1对N的功能。 // sem_post(&bin_sem); // sleep (1); } // printf("I am here\n"); close(fd); pthread_exit(NULL); }
1
#include
<
stdio.h
>
2 #include < stdlib.h >
3 #include < string .h >
4 #include < pthread.h >
5 #include < sys / socket.h >
6 #include < sys / un.h >
7 #include < unistd.h >
8 #include < semaphore.h > // 这里没有用二进制信号量可以删掉
9
10 char buffer[ 1024 ]; // 读写用的区域
11 sem_t bin_sem; // 没用到的二进制信号量,可以删掉
12 void * pthread_function( void * arg); // 线程入口函数声明
13 pthread_mutex_t work_mutex; // 声明互斥锁
14
15 int main(){
16 int result; // 整数变量用来储存调用函数的返回值
17 struct sockaddr_un server_address, client_address; // UNIX域的套接字,server_address用于服务端的监听,client_address用于客户端连接后的套接字
18 int client_len; // 连接后,accept函数会把客户端的地址的长度储存在这
19 int server_socketfd, client_socketfd; // 服务端和客户端的套接字文件描述符
20 pthread_t a_thread; // 线程ID标志
21 pthread_attr_t thread_attr; // 线程的属性,后面可以看的,被我注释掉了,没用到,可以删掉。
22
23 result = sem_init( & bin_sem, 0 , 1 ); // 初始化二进制信号量,因为用了互斥锁,所以没用到,可以删掉
24 if (result != 0 ){
25 perror( " sem_init " );
26 exit(EXIT_FAILURE);
27 }
28
29 result = pthread_mutex_init( & work_mutex, NULL); // 初始化互斥锁
30 if (result != 0 ){
31 perror( " pthread_mutex_init " );
32 exit(EXIT_FAILURE);
33 }
34
35 server_socketfd = socket(AF_UNIX, SOCK_STREAM, 0 ); // 创建套接字,用TCP连接方式,出于演示目的只用UNIX域套接字。
36
37 server_address.sun_family = AF_UNIX;
38 strcpy(server_address.sun_path, " server_socket " );
39
40 unlink( " server_socket " ); // 在绑定之前,把以前存在当前目录下的套接字删除
41
42 result = bind(server_socketfd, ( struct sockaddr * ) & server_address, sizeof (server_address)); // 绑定
43 if (result != 0 ){
44 perror( " bind " );
45 exit(EXIT_FAILURE);
46 }
47
48 result = listen(server_socketfd, 5 ); // 监听,最多允许5个连接请求
49 if (result != 0 ){
50 perror( " listen " );
51 exit(EXIT_FAILURE);
52 }
53
54 client_len = sizeof (client_address);
55 while ( 1 ){ // 开始进入无限循环
56 /* printf("If you want to quit, please enter 'quit'\n");
57 printf("Do you want to accept a connectiong\n");
58 memset(buffer, '\0', sizeof(buffer));
59 fgets(buffer, sizeof(buffer), stdin);
60 if((strncmp("quit", buffer, 4))==0) break; */
61
62 client_socketfd = accept(server_socketfd, ( struct sockaddr * ) & client_address, & client_len); // 接受一个连接请求
63
64 /* result = pthread_attr_init(&thread_attr);
65 if(result != 0){
66 perror("pthread_attr_init");
67 exit(EXIT_FAILURE);
68 }
69 result = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
70 if(result != 0){
71 perror("pthread_attr_setdetachstate");
72 exit(EXIT_FAILURE);
73 } */
74 result = pthread_create( & a_thread, NULL, pthread_function, ( void * )client_socketfd); // 成功接受一个请求后,就会创建一个线程,然后主线程又进入accept函数,如果此时没有连接请求,那么主线程会阻塞
75 if (result != 0 ){
76 perror( " pthread_create " );
77 exit(EXIT_FAILURE);
78 }
79
80 }
81 }
82
83 void * pthread_function( void * arg){ // 线程入口函数,每调用一次pthread_create,都会创建一个新的线程
84 int fd = ( int ) arg; // 把函数参数,即连接成功后的套接字,赋给fd.
85 int result;
86 fd_set read_fds; // 文件描述符集合,用于select函数
87 int max_fds; // 文件描述符集合的最大数
88
89 printf( " %d id has connected!!\n " , fd);
90 while ( 1 ){
91
92 FD_ZERO( & read_fds); // 清空集合
93 FD_SET( 0 , & read_fds); // 将标准输入放入监听的文件描述符集合, 这个用于读取标准输入,即键盘的输入
94 FD_SET(fd, & read_fds); // 将连接后的客户文件描述符放入监听的文件描述符集合, 这个用于向客户端读取数据
95 max_fds = fd + 1 ;
96
97 // sem_wait(&bin_sem);
98 pthread_mutex_lock( & work_mutex); // 对关键区域上锁
99 printf( " %d has get the lock\n " , fd);
100 result = select(max_fds, & read_fds, (fd_set * )NULL, (fd_set * )NULL, ( struct timeval * )NULL); // 开始监听那些文件描述符出于可读状态
101 if (result < 1 ){
102 printf( " select " );
103 }
104 if (FD_ISSET( 0 , & read_fds)){ // 如果标准输入处于可读状态,说明键盘有所输入,将输入的数据存放在buffer中,然后向客户端写回,如果输入“quit”将会退出一个聊天线程
105 memset(buffer, ' \0 ' , sizeof (buffer)); // 保险起见,清零
106 fgets(buffer, sizeof (buffer), stdin);
107 if ((strncmp( " quit " , buffer, 4 )) == 0 ){
108 printf( " You have terminaled the chat\n " );
109 // sem_post(&bin_sem);
110 pthread_mutex_unlock( & work_mutex);
111 break ;
112 }
113 else {
114 result = write(fd, buffer, sizeof (buffer));
115 if (result ==- 1 ){
116 perror( " write " );
117 exit(EXIT_FAILURE);
118 }
119 }
120 }
121 if (FD_ISSET(fd, & read_fds)){ // 如果客户套接字符可读,那么读取存放在buffer中,然后显示出来,如果对方中断聊天,那么result==0
122 memset(buffer, ' \0 ' , sizeof (buffer));
123 result = read(fd, buffer, sizeof (buffer));
124 if (result == - 1 ){
125 perror( " read " );
126 exit(EXIT_FAILURE);
127 }
128 else if (result == 0 ){
129 printf( " The other side has terminal the chat\n " );
130 // sem_post(&bin_sem);
131 pthread_mutex_unlock( & work_mutex);
132 break ;
133 }
134 else {
135 printf( " receive message: %s " , buffer);
136 }
137 }
138 pthread_mutex_unlock( & work_mutex); // 解锁
139 sleep ( 1 ); // 如果没有这一行,当前线程会一直占据buffer.让当前线程暂停一秒可以实现1对N的功能。
140 // sem_post(&bin_sem);
141 // sleep (1);
142 }
143 // printf("I am here\n");
144 close(fd);
145 pthread_exit(NULL);
146
147 }
148
2 #include < stdlib.h >
3 #include < string .h >
4 #include < pthread.h >
5 #include < sys / socket.h >
6 #include < sys / un.h >
7 #include < unistd.h >
8 #include < semaphore.h > // 这里没有用二进制信号量可以删掉
9
10 char buffer[ 1024 ]; // 读写用的区域
11 sem_t bin_sem; // 没用到的二进制信号量,可以删掉
12 void * pthread_function( void * arg); // 线程入口函数声明
13 pthread_mutex_t work_mutex; // 声明互斥锁
14
15 int main(){
16 int result; // 整数变量用来储存调用函数的返回值
17 struct sockaddr_un server_address, client_address; // UNIX域的套接字,server_address用于服务端的监听,client_address用于客户端连接后的套接字
18 int client_len; // 连接后,accept函数会把客户端的地址的长度储存在这
19 int server_socketfd, client_socketfd; // 服务端和客户端的套接字文件描述符
20 pthread_t a_thread; // 线程ID标志
21 pthread_attr_t thread_attr; // 线程的属性,后面可以看的,被我注释掉了,没用到,可以删掉。
22
23 result = sem_init( & bin_sem, 0 , 1 ); // 初始化二进制信号量,因为用了互斥锁,所以没用到,可以删掉
24 if (result != 0 ){
25 perror( " sem_init " );
26 exit(EXIT_FAILURE);
27 }
28
29 result = pthread_mutex_init( & work_mutex, NULL); // 初始化互斥锁
30 if (result != 0 ){
31 perror( " pthread_mutex_init " );
32 exit(EXIT_FAILURE);
33 }
34
35 server_socketfd = socket(AF_UNIX, SOCK_STREAM, 0 ); // 创建套接字,用TCP连接方式,出于演示目的只用UNIX域套接字。
36
37 server_address.sun_family = AF_UNIX;
38 strcpy(server_address.sun_path, " server_socket " );
39
40 unlink( " server_socket " ); // 在绑定之前,把以前存在当前目录下的套接字删除
41
42 result = bind(server_socketfd, ( struct sockaddr * ) & server_address, sizeof (server_address)); // 绑定
43 if (result != 0 ){
44 perror( " bind " );
45 exit(EXIT_FAILURE);
46 }
47
48 result = listen(server_socketfd, 5 ); // 监听,最多允许5个连接请求
49 if (result != 0 ){
50 perror( " listen " );
51 exit(EXIT_FAILURE);
52 }
53
54 client_len = sizeof (client_address);
55 while ( 1 ){ // 开始进入无限循环
56 /* printf("If you want to quit, please enter 'quit'\n");
57 printf("Do you want to accept a connectiong\n");
58 memset(buffer, '\0', sizeof(buffer));
59 fgets(buffer, sizeof(buffer), stdin);
60 if((strncmp("quit", buffer, 4))==0) break; */
61
62 client_socketfd = accept(server_socketfd, ( struct sockaddr * ) & client_address, & client_len); // 接受一个连接请求
63
64 /* result = pthread_attr_init(&thread_attr);
65 if(result != 0){
66 perror("pthread_attr_init");
67 exit(EXIT_FAILURE);
68 }
69 result = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
70 if(result != 0){
71 perror("pthread_attr_setdetachstate");
72 exit(EXIT_FAILURE);
73 } */
74 result = pthread_create( & a_thread, NULL, pthread_function, ( void * )client_socketfd); // 成功接受一个请求后,就会创建一个线程,然后主线程又进入accept函数,如果此时没有连接请求,那么主线程会阻塞
75 if (result != 0 ){
76 perror( " pthread_create " );
77 exit(EXIT_FAILURE);
78 }
79
80 }
81 }
82
83 void * pthread_function( void * arg){ // 线程入口函数,每调用一次pthread_create,都会创建一个新的线程
84 int fd = ( int ) arg; // 把函数参数,即连接成功后的套接字,赋给fd.
85 int result;
86 fd_set read_fds; // 文件描述符集合,用于select函数
87 int max_fds; // 文件描述符集合的最大数
88
89 printf( " %d id has connected!!\n " , fd);
90 while ( 1 ){
91
92 FD_ZERO( & read_fds); // 清空集合
93 FD_SET( 0 , & read_fds); // 将标准输入放入监听的文件描述符集合, 这个用于读取标准输入,即键盘的输入
94 FD_SET(fd, & read_fds); // 将连接后的客户文件描述符放入监听的文件描述符集合, 这个用于向客户端读取数据
95 max_fds = fd + 1 ;
96
97 // sem_wait(&bin_sem);
98 pthread_mutex_lock( & work_mutex); // 对关键区域上锁
99 printf( " %d has get the lock\n " , fd);
100 result = select(max_fds, & read_fds, (fd_set * )NULL, (fd_set * )NULL, ( struct timeval * )NULL); // 开始监听那些文件描述符出于可读状态
101 if (result < 1 ){
102 printf( " select " );
103 }
104 if (FD_ISSET( 0 , & read_fds)){ // 如果标准输入处于可读状态,说明键盘有所输入,将输入的数据存放在buffer中,然后向客户端写回,如果输入“quit”将会退出一个聊天线程
105 memset(buffer, ' \0 ' , sizeof (buffer)); // 保险起见,清零
106 fgets(buffer, sizeof (buffer), stdin);
107 if ((strncmp( " quit " , buffer, 4 )) == 0 ){
108 printf( " You have terminaled the chat\n " );
109 // sem_post(&bin_sem);
110 pthread_mutex_unlock( & work_mutex);
111 break ;
112 }
113 else {
114 result = write(fd, buffer, sizeof (buffer));
115 if (result ==- 1 ){
116 perror( " write " );
117 exit(EXIT_FAILURE);
118 }
119 }
120 }
121 if (FD_ISSET(fd, & read_fds)){ // 如果客户套接字符可读,那么读取存放在buffer中,然后显示出来,如果对方中断聊天,那么result==0
122 memset(buffer, ' \0 ' , sizeof (buffer));
123 result = read(fd, buffer, sizeof (buffer));
124 if (result == - 1 ){
125 perror( " read " );
126 exit(EXIT_FAILURE);
127 }
128 else if (result == 0 ){
129 printf( " The other side has terminal the chat\n " );
130 // sem_post(&bin_sem);
131 pthread_mutex_unlock( & work_mutex);
132 break ;
133 }
134 else {
135 printf( " receive message: %s " , buffer);
136 }
137 }
138 pthread_mutex_unlock( & work_mutex); // 解锁
139 sleep ( 1 ); // 如果没有这一行,当前线程会一直占据buffer.让当前线程暂停一秒可以实现1对N的功能。
140 // sem_post(&bin_sem);
141 // sleep (1);
142 }
143 // printf("I am here\n");
144 close(fd);
145 pthread_exit(NULL);
146
147 }
148
读者可以对比一下http://blog.csdn.net/hwz119/archive/2007/03/19/1534233.aspx
读者可以发现,链接网络中的程序需要结束当前一个聊天才能进行下一个聊天,而这个服务端可以同时对N个人进行聊天,尽管有些bug(如果客户端对方回复太快太频繁,服务端的锁就会切换来切换去,无法回复到正确的客户端)。
客户端跟服务端很像,但比较简单。这里面就不注释了。这两个程序我都运行过。。。没什么基本大的问题。。但是功能很不完善。。。还需改进。。。。。
客户端代码
#include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<sys/un.h> #include<string.h> #include<sys/types.h> #include<sys/time.h> int main() { int result; int socketfd; int len; struct sockaddr_un address; fd_set read_fds, test_fds; int fd; int max_fds; char buffer[1024]; socketfd = socket(AF_UNIX, SOCK_STREAM, 0); address.sun_family = AF_UNIX; strcpy(address.sun_path, "server_socket"); len = sizeof(address); result = connect(socketfd, (struct sockaddr*)&address, len); if(result == -1) { perror("connect"); exit(EXIT_FAILURE); } FD_ZERO(&read_fds); FD_SET(0, &read_fds); FD_SET(socketfd, &read_fds); max_fds = socketfd +1; printf("Chat now!!\n"); while(1) { test_fds = read_fds; result = select(max_fds, &test_fds, (fd_set *)NULL, (fd_set *)NULL, (struct timeval*)NULL); if(result < 1) { perror("select"); exit(EXIT_FAILURE); } if(FD_ISSET(0, &test_fds)) { memset(buffer, '\0', sizeof(buffer)); // printf("send:"); fgets(buffer, sizeof(buffer), stdin); if((strncmp("quit", buffer, 4))== 0) { printf("\nYou are going to quit\n"); break; } result = write(socketfd, buffer, sizeof(buffer)); if(result == -1) { perror("write"); exit(EXIT_FAILURE); } } if(FD_ISSET(socketfd, &test_fds)) { memset(buffer, '\0', sizeof(buffer)); result = read(socketfd, buffer, sizeof(buffer)); if(result == -1) { perror("read"); exit(EXIT_FAILURE); } else if(result == 0) { printf("The other side has termianl chat!\n"); break; } else { printf("recieve: %s", buffer); } } } close(socketfd); exit(EXIT_SUCCESS); }
1
#include
<
stdio.h
>
2 #include < stdlib.h >
3 #include < sys / socket.h >
4 #include < sys / un.h >
5 #include < string .h >
6 #include < sys / types.h >
7 #include < sys / time.h >
8
9 int main(){
10 int result;
11 int socketfd;
12 int len;
13 struct sockaddr_un address;
14 fd_set read_fds, test_fds;
15 int fd;
16 int max_fds;
17 char buffer[ 1024 ];
18
19 socketfd = socket(AF_UNIX, SOCK_STREAM, 0 );
20
21 address.sun_family = AF_UNIX;
22 strcpy(address.sun_path, " server_socket " );
23 len = sizeof (address);
24
25 result = connect(socketfd, ( struct sockaddr * ) & address, len);
26 if (result == - 1 ){
27 perror( " connect " );
28 exit(EXIT_FAILURE);
29 }
30
31 FD_ZERO( & read_fds);
32 FD_SET( 0 , & read_fds);
33 FD_SET(socketfd, & read_fds);
34 max_fds = socketfd + 1 ;
35
36 printf( " Chat now!!\n " );
37
38 while ( 1 ){
39 test_fds = read_fds;
40 result = select(max_fds, & test_fds, (fd_set * )NULL, (fd_set * )NULL, ( struct timeval * )NULL);
41 if (result < 1 ){
42 perror( " select " );
43 exit(EXIT_FAILURE);
44 }
45
46 if (FD_ISSET( 0 , & test_fds)){
47 memset(buffer, ' \0 ' , sizeof (buffer));
48 // printf("send:");
49 fgets(buffer, sizeof (buffer), stdin);
50 if ((strncmp( " quit " , buffer, 4 )) == 0 ){
51 printf( " \nYou are going to quit\n " );
52 break ;
53 }
54 result = write(socketfd, buffer, sizeof (buffer));
55 if (result == - 1 ){
56 perror( " write " );
57 exit(EXIT_FAILURE);
58 }
59 }
60 if (FD_ISSET(socketfd, & test_fds)){
61 memset(buffer, ' \0 ' , sizeof (buffer));
62 result = read(socketfd, buffer, sizeof (buffer));
63 if (result == - 1 ){
64 perror( " read " );
65 exit(EXIT_FAILURE);
66 } else if (result == 0 ){
67 printf( " The other side has termianl chat!\n " );
68 break ;
69 } else {
70 printf( " recieve: %s " , buffer);
71 }
72 }
73 }
74 close(socketfd);
75 exit(EXIT_SUCCESS);
76 }
77
2 #include < stdlib.h >
3 #include < sys / socket.h >
4 #include < sys / un.h >
5 #include < string .h >
6 #include < sys / types.h >
7 #include < sys / time.h >
8
9 int main(){
10 int result;
11 int socketfd;
12 int len;
13 struct sockaddr_un address;
14 fd_set read_fds, test_fds;
15 int fd;
16 int max_fds;
17 char buffer[ 1024 ];
18
19 socketfd = socket(AF_UNIX, SOCK_STREAM, 0 );
20
21 address.sun_family = AF_UNIX;
22 strcpy(address.sun_path, " server_socket " );
23 len = sizeof (address);
24
25 result = connect(socketfd, ( struct sockaddr * ) & address, len);
26 if (result == - 1 ){
27 perror( " connect " );
28 exit(EXIT_FAILURE);
29 }
30
31 FD_ZERO( & read_fds);
32 FD_SET( 0 , & read_fds);
33 FD_SET(socketfd, & read_fds);
34 max_fds = socketfd + 1 ;
35
36 printf( " Chat now!!\n " );
37
38 while ( 1 ){
39 test_fds = read_fds;
40 result = select(max_fds, & test_fds, (fd_set * )NULL, (fd_set * )NULL, ( struct timeval * )NULL);
41 if (result < 1 ){
42 perror( " select " );
43 exit(EXIT_FAILURE);
44 }
45
46 if (FD_ISSET( 0 , & test_fds)){
47 memset(buffer, ' \0 ' , sizeof (buffer));
48 // printf("send:");
49 fgets(buffer, sizeof (buffer), stdin);
50 if ((strncmp( " quit " , buffer, 4 )) == 0 ){
51 printf( " \nYou are going to quit\n " );
52 break ;
53 }
54 result = write(socketfd, buffer, sizeof (buffer));
55 if (result == - 1 ){
56 perror( " write " );
57 exit(EXIT_FAILURE);
58 }
59 }
60 if (FD_ISSET(socketfd, & test_fds)){
61 memset(buffer, ' \0 ' , sizeof (buffer));
62 result = read(socketfd, buffer, sizeof (buffer));
63 if (result == - 1 ){
64 perror( " read " );
65 exit(EXIT_FAILURE);
66 } else if (result == 0 ){
67 printf( " The other side has termianl chat!\n " );
68 break ;
69 } else {
70 printf( " recieve: %s " , buffer);
71 }
72 }
73 }
74 close(socketfd);
75 exit(EXIT_SUCCESS);
76 }
77