Malloc的客户端/服务器问题?

时间:2017-03-08 07:36:42

标签: c sockets malloc

我正在处理的这项任务的一部分涉及一个程序,它通过套接字从客户端执行命令。现在我唯一需要编辑的文件是我的mathServer.c。我目前在所提供的说明中坚持使用一个的几个部分:

完成方法 - doServer()。 doServer()应该有一个循环,等待客户端连接到listenFd。当客户这样做时,它应该:

  1. malloc()2个整数的足够内存

  2. 将accept()中的文件描述符放在其中一个空格

  3. 将threadCount的值放在另一个空格中,然后递增 THREADCOUNT

  4. 创建一个分离的线程来处理这个新客户端。我调用了我的函数handleClient(),但你可以打电话给你。传递malloc() - ed数组的地址。

  5. 然后循环应该返回另一个accept()。

    这是我的doServer:

    void doServer (int listenFd)
    {
      //  I.  Application validity check:
    
      //  II.  Server clients:
      pthread_t     threadId;
      pthread_attr_t    threadAttr;
    
      int   threadCount = 0; 
    
      // YOUR CODE HERE
      int *a;
    
      while(1) {
    
        //1. If Malloc was NEVER (outside or inside loop) in this program then 
        // it outputs Thread 0 recieved
        a = malloc(sizeof(int) * 2);
    
        accept(getServerFileDescriptor(), NULL, NULL);
    
        // 2.
        a[0] = getServerFileDescriptor();
    
        // 3.
        a[1] = threadCount++;
    
        // ALL 4
        pthread_attr_init(&threadAttr);
        pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED);
        pthread_create(&threadId, &threadAttr, handleClient, &a);
    
        pthread_join(threadId, NULL); 
        pthread_attr_destroy(&threadAttr);
    
       }
    
    }
    

    这是我的handleClient方法:

    void* handleClient(void* vPtr) {
    
      // Use another pointer to cast back to int*
      // Save the file descriptor and thread number in local vars
      // free() the memory
    
      // I wrote these 2 lines.
      int *castMe = (int *)vPtr;
      free(vPtr);
    
      //  II.B.  Read command:
      char  buffer[BUFFER_LEN];
      char  command;
      int fileNum;
    
      int fd = castMe[0];
      int threadNum = castMe[1];
    
      char  text[BUFFER_LEN];
      int   shouldContinue  = 1;
    
      while  (shouldContinue)
      {
        text[0] = '\0';
    
        read(fd,buffer,BUFFER_LEN);
        printf("Thread %d received: %s\n",threadNum,buffer);
        sscanf(buffer,"%c %d \"%[^\"]\"",&command,&fileNum,text);
    
        //printf("Thread %d quitting.\n",threadNum);
        return(NULL);
    
        // YOUR CODE HERE
    
      }
    
    }
    

    我发现每当我删除 a = malloc(sizeof(int)* 2)以及与malloc相关的所有内容时,它都会输出收到的线程0 。但是,当我保留malloc时,输出只是空白而不会返回任何内容。

    起初我以为是因为我没有释放内存,但内存正在从handleClient中解放出来吗?

    **请注意这不是整个计划。你在这里看到的任何方法都是教授的工作。这两种方法都是我自己的(你的代码在这里)。假设教授代码有效:) **

    非常感谢任何帮助!

4 个答案:

答案 0 :(得分:1)

你的代码

// I wrote these 2 lines.
int *castMe = (int *)vPtr;
free(vPtr);

free castMe指向的内存,当您使用它时,您将取消引用无效内存

int fd = castMe[0];       //<----- BOOM
int threadNum = castMe[1];//<----- BOOM

undefined behavior

此外,当你写 时,我删除了一个= malloc(sizeof(int)* 2) 我猜你要保留a声明

int *a;

那是undefined behavior,因为a没有指向有效的内存地址。

答案 1 :(得分:0)

这是错误:

int *castMe = (int *)vPtr;
free(vPtr);
...
int fd = castMe[0];

您释放vPtr指向的内存,然后尝试通过另一个指针访问该内存。

答案 2 :(得分:0)

此:

pthread_create(&threadId, &threadAttr, handleClient, &a);
                                                     ^
                                                     |
                                                    WAT

是一个问题,因为在这里你让pthreads库将指向<{1}} 中第一个元素的指针的地址传递给线程,你只想传递一个指向a中的第一个元素。

所以,你应该通过a,这更容易,更清晰地写成&a[0]

此调用与您在线程函数中使用参数的方式不匹配。您可以打印这两个以查看差异:

a

然后在函数add:

printf("&a=%p\n", (void *) &a);

价值观会有所不同。

此外,您的内存处理已关闭,而您仍需要printf("castMe=%p\n", (void *) castMe); 数据。在C语言中,您不需要对数据指针和free()进行显式强制转换,您可以这样做:

void *

答案 3 :(得分:0)

这是答案吗?我也正在研究这个问题!