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

/******************************
*
* server.c
*
******************************/
#include<stdio.h>  
#include<stdlib.h>  
#include<sys/socket.h>  
#include<sys/types.h>  
#include<errno.h>  
#include<string.h>  
#include<netinet/in.h>  
#include<sys/wait.h>  
#include<unistd.h>  
#include<arpa/inet.h>  
#include<pthread.h>  
   
   
#define MAXBUF 1024  
#define LISNUM 10  
#define DEFULT_PORT 8787  
   
void *connect_to_client(void *data);  
struct sockaddr_in6 serv_addr,client_addr;  
/**************************/
int main(int argc,char *argv[])  
{  
       int sockfd,new_fd;  
       unsigned int serv_port;  
       unsigned int lisnum;  
       char buf[MAXBUF+1];  
         
       printf( "the command is %s [port] [listen_num]\n", argv[0] );  
         
       // 监听起始端口  
       if ( argv[1] != NULL )  
       {  
              serv_port = atoi( argv[1] );  
       }  
       else
       {  
              serv_port = DEFULT_PORT;  
       }  

// 监听端口个数  
       if ( argv[2] != NULL )  
       {  
              lisnum = atoi(argv[2]);  
       }  
       else
       {  
              lisnum = LISNUM;  
       }  
         
       // 创建socket  
       if ( (sockfd = socket(PF_INET6,SOCK_STREAM,0)) < 0 )  
       {  
              perror("creat socket");  
              exit(1);  
       }  
         
       // 配置服务器地址与端口  
       bzero(&serv_addr, sizeof(serv_addr));  
       serv_addr.sin6_family = AF_INET6;  
       serv_addr.sin6_port = htons(serv_port);  
       serv_addr.sin6_addr = in6addr_any;  
       // 地址绑定操作  
       if ( bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr) ) <0 )  
       {  
              perror("bind");  
              exit(1);  
       }  
         
       // 服务器监听  
       if ( listen(sockfd, lisnum) < 0 )  
       {  
              perror("listen");  
              exit(1);  
       }  
         
       while(1)  
       {  
              int len = sizeof(struct sockaddr_in6);  
              if ( (new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &len)) < 0 )  
              {  
                     perror("accept");  
                     exit(errno);  
              }  
              printf("receive the request form %s\n", inet_ntop(AF_INET6, &client_addr, buf, sizeof(buf)));  
                
              // 接收到1个TCP连接则创建1个线程用于数据通讯  
              pthread_t child_thread;  
              if ( pthread_create( &child_thread, NULL, connect_to_client, (void *)new_fd) < 0)  
              {  
                     perror("create thread");  
                     continue;  
              }  
       }  
       close( sockfd );  
         
       return(0);  
}  

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

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