首页 > 解决方案 > TCP Echo Server 使用线程命令行未显示

问题描述

在我的项目中,当客户端像 LIST 一样写入时,服务器端将目录列出文档,然后当我像 GET filename.txt 一样写入但在服务器端没有显示任何东西我认为是停止我该如何解决这个问题我的项目描述器在照片中在此处输入图像描述

在此处输入图像描述

源代码是

#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdbool.h>



#include <dirent.h> 

/* Definations */
#define DEFAULT_BUFLEN 1024
#define PORT 1888

void PANIC(char* msg);
#define PANIC(msg)  { perror(msg); exit(-1); }

 
 
bool StartsWith(const char *a, const char *b)
{
   if(strncmp(a, b, strlen(b)) == 0) return 1;
   return 0;
}
 

void* Child(void* arg)
{   char line[DEFAULT_BUFLEN];
    int bytes_read;
     
    int client = *(int *)arg;


    
    
 send(client,"Welcome to Vepa Server\n",strlen("Welcome to Vepa Server\n"),0);
    do
    {
           

         bytes_read = recv(client, line, sizeof(line), 0);
          
     
       
        if (bytes_read > 0) {

        printf("%s",line);  

 
 // list command in here
       if (strcmp(line,"LIST\n") ==0)
        {
           while(1)
           {
               DIR *d;
               struct dirent *dir;
               d = opendir(".");
              if (d) {
               while ((dir = readdir(d)) != NULL) {
              
                 send(client,dir->d_name,strlen(dir->d_name),0);
                 send(client,"\n",strlen("\n"),0);

                
             }
             closedir(d);
       }
       return(0);

           }
        }

        if (StartsWith(line,"GET") == 1)
        {
             printf(line);
        }
        
 
        
      
        
 

     if ( (bytes_read=send(client, line, bytes_read, 0)) < 0 ) {
                        printf("Send failed\n");
                        break;
                }
        } else if (bytes_read == 0 ) {
                printf("Connection closed by client\n");
                break;
        } else {
                printf("Connection has problem\n");
                break;
        }
    } while (bytes_read > 0);
    close(client);
    return arg;
}

 
int main(int argc, char *argv[])
{  
    
   
     int sd,opt,optval;
    struct sockaddr_in addr;
    unsigned short port=0;

    while ((opt = getopt(argc, argv, "p:")) != -1) {
        switch (opt) {
        case 'p':
                port=atoi(optarg);
                break;
        }
    }


    if ( (sd = socket(PF_INET, SOCK_STREAM, 0)) < 0 )
        PANIC("Socket");
    addr.sin_family = AF_INET;

    if ( port > 0 )
                addr.sin_port = htons(port);
    else
                addr.sin_port = htons(PORT);

    addr.sin_addr.s_addr = INADDR_ANY;

   // set SO_REUSEADDR on a socket to true (1):
   optval = 1;
   setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);


    if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
        PANIC("Bind");
    if ( listen(sd, SOMAXCONN) != 0 )
        PANIC("Listen");

    printf("System ready on port %d\n",ntohs(addr.sin_port));

 
    while (1)
    {
        int client, addr_size = sizeof(addr);
        pthread_t child;

        client = accept(sd, (struct sockaddr*)&addr, &addr_size);
      
        
         
        printf("Connected: %s:%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
         
       
        
          

        if ( pthread_create(&child, NULL, Child, &client) != 0 )
            perror("Thread creation");
        else
            pthread_detach(child);  /* disassociate from parent */
    }
    return 0;
}

标签: c++cservertcp

解决方案


推荐阅读