我有一个程序,它为每个对服务器的调用创建一个线程。它编译时没有错误,但是它从未将其转换为printf('before while')语句,但是它确实传递了bind

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <pthread.h>
#include <fcntl.h>
#include <dirent.h>

#define BufferSize 1024 // for parsing request
#define BIG_ENUF 4096 // For request header

void error(char *); // prototype for quick error exit
void *connection_handler(void * newsockfd);

int main(int argc, char *argv[]) // arv[1] has port #
{

    // We Have Ourselves Some Declarations Old School
 int sockfd, newsockfd, portno, clilen,Connect_Count=0;
 int BufferNdx,n ;// workaday subscripts
 char * TmpBuffer, *SavePtr, *FileName;
 char * GetToken;
 FILE * F;  // for streaming file when GET served
 struct stat S;// to find file length
 struct sockaddr_in serv_addr, cli_addr;
 pthread_t tid;

     if (argc < 2) {  // looking for port #
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }

     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     // specifies TCP IP flow
     if (sockfd < 0)
        error("ERROR opening socket");
     memset( (char *) &serv_addr, 0, sizeof(serv_addr));
     // Now fill the zeroed server structure //
     // consistent with socket setup
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno); // proper byte order
    if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
                    perror("ERROR on binding");
            }

    GetToken = strtok_r(TmpBuffer," ",&SavePtr);
    printf("before while\n");
    listen(sockfd,5);
    while (Connect_Count < 10) // Limit on Number of Connections
    {

      clilen = sizeof(cli_addr);
      newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
    // listen blocks until someone knocks and when we accept

  // the client structure is filled by accept
     if (newsockfd < 0) // exit server if bad accept
          error( "ERROR on accept");
       Connect_Count++;
     printf("%d\n",Connect_Count );
    if(pthread_create(&tid, NULL, connection_handler,(void *)newsockfd )<0){
        perror("could not create thread");
        return 1;
    }
    }
   pthread_exit(NULL);
// Now close up this client's shop
     close(newsockfd);
     return 0;
}





void *connection_handler(void* newsock)
{
    int newsockfd = (int) newsock;
    char buffer[BufferSize];
    char * BigBuffer;
    char * TmpBuffer, *SavePtr, *FileName, *GetToken;
    int n;
    struct stat S;
    memset(buffer, 0,BufferSize);
    FILE * F;
     n = read(newsockfd,buffer,BufferSize-1); // This leaves null at end
    if (n < 0) error("ERROR reading from socket");

     printf("%s\n",(TmpBuffer=strtok_r(buffer,"\n",&SavePtr)));
     GetToken = strtok_r(TmpBuffer," ",&SavePtr);
     printf("%s\n",GetToken);
     GetToken = strtok_r(NULL," ",&SavePtr);
     printf("%s After Get\n",GetToken); // file name token begins '/'
     GetToken++; // Point to first character of actual file name
    // now open the file and send it to client ?
     if ((F =  fopen(GetToken,"r")) == NULL) error("Bad\n");
    else printf("Good\n");
        int FileSize;
    if ((fstat(fileno(F),&S)==-1)) error("failed fstat\n"); // Need file size
    FileSize = S.st_size;
// Looks ok -- now let's write the request header
 // Let's just fill a buffer with header info using sprintf()
    char Response[BIG_ENUF];int HeaderCount=0;
    HeaderCount=0;//Use to know where to fill buffer with sprintf
        HeaderCount+=sprintf( Response+HeaderCount,"HTTP/1.0 200 OK\r\n");
        HeaderCount+=sprintf( Response+HeaderCount,"Server: Flaky Server/1.0.0\r\n");
        HeaderCount+=sprintf( Response+HeaderCount,"Content-Type: image/jpeg\r\n");
        HeaderCount+=sprintf( Response+HeaderCount,"Content-Length:%d\r\n",FileSize);
 // And finally to delimit header
        HeaderCount+=sprintf( Response+HeaderCount,"\r\n");
        // Let's echo that to stderr to be sure !
        fprintf(stderr,"HeaderCount %d and Header\n",HeaderCount);
        write(STDERR_FILENO, Response, HeaderCount);
        write(newsockfd,Response,HeaderCount); // and send to client
// Now Serve That File in one write to socket
        BigBuffer = malloc(FileSize+2);// Just being OCD -- Slack is 2
        fread(BigBuffer,1,FileSize,F);
        write(newsockfd,BigBuffer,FileSize);
        free(BigBuffer);
    pthread_exit(NULL);
    return NULL;
}





// Bad Error routine
void error(char *msg)
{
    perror(msg);
    exit(1);
}

我不知道为什么这会是一个问题,因为它在一个过程版本的这个我有。如果有任何变化,我将使用gcc-pthread filename.c进行编译。

最佳答案

这行GetToken = strtok_r(TmpBuffer," ",&SavePtr);似乎是个问题,因为TmpBuffer是指向char的指针,它什么也不指向。。。
注释掉它,或者分配一些内存,填充它,并将其分配给TmpBuffer

08-16 11:49