Linux下IP v6 tcp服务器端源码示例(2)

/************************************
*
* 子程序:用于处理一个与客户端的连接,由一个线程来执行
*
************************************/
void *connect_to_client(void *data)  
{  
       int i;  
       int client_fd=(int)data;  
       char recv_buf[MAXBUF+1];  
       char send_buf[MAXBUF+1];  
       char buf[MAXBUF+1];  
       while(1)  
       {  
              sprintf( send_buf, ">>" );  
              if ( send(client_fd, send_buf, strlen(send_buf), 0) < 0 )  
              {  
                     perror("send");  
                     continue;  
              }  
                
              // 从客户端接收数据,直到接收到换行符   
              while ( i < MAXBUF-1 )  
              {  
                     if ( recv(client_fd, &recv_buf[i], 1, 0) < 0 )  
                     {  
                            perror("recv");  
                            break;  
                     }  
                       
                     if ( recv_buf[i] == '\n' )  
                     {  
                            break;  
                     }  
                       
                     i++;  
              }  
                
              recv_buf[i] = '\0';  
              i = 0;  
              // 将接收到的数据输出到标准输出  
              printf("receive message from '%s':%s\n",  
                      inet_ntop(AF_INET6, &client_addr.sin6_addr, buf, sizeof(buf)),  
                      recv_buf);  
       }  
}//end sub_function

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsxzp.html