linux socket tcp 为什么总是:newfd = 0,出现还很多-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

linux socket tcp 为什么总是:newfd = 0,出现还很多

a123456678 2016-06-12 15:07:05 2176

client.c

#include   <stdio.h>
#include   <stdlib.h>
#include   <errno.h>
#include   <string.h>
#include   <sys/types.h>
#include   <netinet/in.h>
#include   <arpa/inet.h>
#include   <sys/socket.h>
#include   <sys/wait.h>
#include   <unistd.h>
#include   <strings.h>
#include   <netdb.h>
#include   <pthread.h>
 
#define   BUF   100
#define   BACKLOG 20
 
struct   sockaddr_in   server_addr;
 
void*   pthfunction(void*   fd)     //thread   for   a   server   link
{
        int   newfd  =  (int)fd;  //new pthread fd newfd=*(int*)fd
         
        printf( "newfd:%d\n ",newfd);
     
        while(1)  
        {
                fd_set   rfds;
                FD_ZERO( &rfds);
                FD_SET(newfd, &rfds);
                 
                select(FD_SETSIZE, &rfds,   NULL,   NULL,   NULL);
                 
                if   (FD_ISSET(newfd, &rfds))
                {
                        FD_CLR(newfd, &rfds);
                         
                        char   data[BUF];
                        scanf( "%s ",data);
                         
                        memset(data, '\0', sizeof(data));//clean 0
                         
                        if(connect(newfd,(struct   sockaddr   *)&server_addr,sizeof(struct   sockaddr))   ==   -1)
                {
                        printf( "connect   error\n ");
                        exit(1);
                }
                         
                        int   res = write(newfd,data,strlen(data));
                         
                        printf( "the   mes   is   %d\n ",res);
                         
                        if   (res < 0)  //write fail
                        {
                                perror( "Communication   error\n ");
                                return   NULL;
                        }
                        else   if(res == 0)  //interrupt
                        {
                                printf( "Communication   end\n ");
                                shutdown(newfd,SHUT_RDWR);
                                return   NULL;
                        }
                        else           //success
                        {
                                printf( "date:%s\n ",data);
                        }
                }
                else
                {
                        continue;
                }
        }
        
} 
 
int   main(int argc,char *argv[])
{
         struct   hostent   *host;
         
        if(argc < 2)  
        {
                perror( "Please   enter   the   server 's   hostname!\n ");
                exit(1);
        }
         
        if((host = gethostbyname(argv[1])) == NULL)
        {
                perror( "gethostbyname   error!\n ");
                exit(1);
        }
         
        bzero(&server_addr,sizeof(server_addr));
         
        server_addr.sin_family   =   AF_INET;
         
        server_addr.sin_port   =   htons(1620);
         
        server_addr.sin_addr   =   *((struct   in_addr   *)host-> h_addr);
         
        int  fid = socket(AF_INET,   SOCK_STREAM,   0);
         
        if(fid == -1)
        {
            perror( "socket  error!\n ");
                exit(1);
        }
         
        while(1)
        {                                     
                pthread_attr_t   attr;
                 
                pthread_attr_init(&attr); 
                 
                pthread_create(&fid,&attr,pthfunction,NULL);   
        }
         
    return   1;
}

server.c

#include   <stdio.h>
#include   <stdlib.h>
#include   <errno.h>
#include   <string.h>
#include   <sys/types.h>
#include   <netinet/in.h>
#include   <arpa/inet.h>
#include   <sys/socket.h>
#include   <sys/wait.h>
#include   <unistd.h>
#include   <strings.h>
#include   <pthread.h>
 
#define   SERVPORT   1620   /*   server   listen   port   */
#define   BACKLOG  20   /*   max   number   of   linking   at   a   time   */
#define   BUF   100
 
int   main()
{
        void*   tcp_thread   (void*   );
        int   sockfd,client_fd;   /*sock_fd:listen   socket;client_fd:data   transport   socket   */
         
        struct   sockaddr_in   my_addr;   /*   information   of   server   address   */
        struct   sockaddr_in   remote_addr;/*   information   of   client   address   */
         
        if   ((sockfd   =   socket(AF_INET,   SOCK_STREAM,   0))   <   0)/*   socket   create   */
        {
                perror( "socker   create   error\n ");
                return   0;
        }
         
        bzero(&my_addr,sizeof(my_addr));
         
        my_addr.sin_family   =   AF_INET;
         
        my_addr.sin_port   =   htons(SERVPORT);
         
        my_addr.sin_addr.s_addr   =   htonl(INADDR_ANY);
         
        setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(void   *)&on,sizeof(int));
         
        if   (bind(sockfd,   (struct   sockaddr   *)&my_addr,   sizeof(struct   sockaddr))   ==   -1)   //bind
        {
                perror( "bind   error\n ");
                return   0;
        }
        if   (listen(sockfd,   BACKLOG)   <   0)   //listen
        {
                perror( "listen   error\n ");
                return   0;
        }
         
        printf( "sockfd:%d\n ",   sockfd);
         
        while(1)  
        {
                int   sin_size   =   sizeof(struct   sockaddr);
                 
                if((client_fd=accept(sockfd,(struct   sockaddr   *)&remote_addr,(socklen_t*)&sin_size))   <   0)
                {
                        perror( "accept   error\n ");
                        continue;
                }
                 
                printf( "client_fd:%d\n ",client_fd);
                 
                char   ptr[32];
                 
                inet_ntop(AF_INET,(void   *)&remote_addr.sin_addr,ptr,32);
                 
                printf( "received   a   connection   from   %s\n ",   ptr);
                 
                pthread_t   thread_id;
                pthread_attr_t   attr;
                 
                pthread_attr_init(&attr); 
                  
                pthread_attr_setdetachstate(&attr,     PTHREAD_CREATE_DETACHED);   /*   the   attribute   of   the   thread   */
                 
                pthread_create(&thread_id,&attr,tcp_thread,(void   *)client_fd);
                 
                pthread_attr_destroy(&attr);
        }
         
        return   1;
}
 
void*   tcp_thread   (void*   fd)     //thread   for   a   client   link
{
        int   newfd   =   (int)fd;
         
        printf( "newfd:%d\n ",newfd);
         
        while   (1)  
        {
                fd_set   rfds;
                FD_ZERO(   &rfds);
                FD_SET(newfd,   &rfds);
                select(FD_SETSIZE,   &rfds,   NULL,   NULL,   NULL);
                if   (FD_ISSET(newfd,   &rfds))
                {
                        FD_CLR(newfd,   &rfds);
                        char   data[BUF];
                        memset(data, '\0', sizeof(data));
                        int   res   =   read(newfd,   data,   sizeof(data));
                        printf( "the   mes   is   %d\n ",res);
                        if   (res   <   0)
                        {
                                perror( "Communication   error\n ");
                                return   NULL;
                        }
                        else   if(res   ==   0)
                        {
                                printf( "Communication   end\n ");
                                shutdown(newfd,SHUT_RDWR);
                                return   NULL;
                        }
                        else
                        {
                                printf( "date:%s\n ",data);
                        }
                }
                else
                {
                        continue;
                }
        }
        
}
tcp是什么 什么是socket socket是什么 new是什么
分享到
取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:34:07

    client.c : 109 ~ 116

    为什么有很多:因为你一直循环创建线程

    为什么总是0:因为你pthread_create时,总是传空值过去,应该是传套接口描述符

    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

相似问题
最新问题
推荐课程