繁体   English   中英

C中涉及套接字和客户端-服务器通信的程序出现问题

[英]Issue with program involving sockets and client-server communication in C

我正在使用C语言开发一个程序,该程序涉及客户端与服务器之间的连接以及两方之间的通信。

该程序包括客户端向服务器发送一封信,然后服务器获取该信件。 然后,服务器在当前文件目录(在Linux中)中搜索以该字母开头的文件,并向客户端发送该文件的字节数和该文件的文本。

整个程序很长,对于作业,讲师已经做了很多代码,例如设置套接字和为操作的客户端创建整个程序。

对于服务器端,我必须编写以下代码:

  • 从传递的内存中获取文件描述符并进行转换

收到客户的来信

-尝试打开当前目录

-遍历目录以查找以字母开头的文件

-尝试打开文件并将文件的大小和文件的字节数发送到网络字节序的客户端

-完成后关闭文件和目录

-错误检查:如果无法打开目录,无法打开文件或找不到匹配的文件,则存在错误检查语句

以下是我的注释代码

void*           handleClient    (void*  vPtr
                                )
{
  //  I.  Application validity check:

       int      fd              = *((int *) vPtr); 
     //casting vPtr to an int//

        free(vPtr);


//  II.  Handle the client:

        char buffer[BUFFER_LEN+1];
        read(fd, buffer, BUFFER_LEN+1);
        //read the letter into a buffer//

        const char* dirNamePtr = ".";
        DIR* dirPtr = opendir(dirNamePtr);
//   Open the current directory


        if (dirPtr == NULL)
        {
            int toSend = htonl(CANT_READ_DIR_CODE);
                write(fd,&toSend,sizeof(toSend));
                printf("Cannot read directory\n");
                return(NULL);
        }
// If current directory cannot be opened, it sends a error message in network // endian to the client

        struct dirent* entryPtr;
        char path[BUFFER_LEN];
        struct stat statBuffer;
//implements struct dirent to get info on the directory

//iterates through the directory
        while ((entryPtr=readdir(dirPtr)) != NULL)
        {


           stat(entryPtr->d_name, &statBuffer);
//puts in metaddata of the current directory into statbuffer


          if (!S_ISREG(statBuffer.st_mode))
                continue;
//if the entry is not a file, continue

// if the first letter of the file is not the character received from the //client, send an error mesage

          if(entryPtr->d_name[0]!=buffer[0])  {
                   int toSend2 = htonl(NO_MATCH_CODE);
                   write(fd,&toSend2,sizeof(toSend2));
                   printf("No matching file\n");
                   return(NULL);
               }


                int ab;
                int numRead;
                int numBytes;
                char buffer[BUFFER_LEN];
//open the file and send bytes of file and file size to client

            if (entryPtr->d_name[0]==buffer[0] &(S_ISREG(statBuffer.st_mode)))
                {

                        ab=open(entryPtr->d_name,O_RDONLY,0660);

                        if(ab<0) {
                                int toSend3 = htonl(CANT_READ_FILE_CODE);
                                write(fd,&toSend3, sizeof(toSend3));
                                printf("Cannot read <filename>\n");
                                return(NULL);
                                }

                        numBytes=htonl(statBuffer.st_size);
                        write(fd, &numBytes, sizeof(numBytes));

                        printf("Sending %s, %d bytes\n",entryPtr >d_name,statBuffer.st_size);


                while((numBytes=read(ab,buffer,BUFFER_LEN))>0)
                        {
                                printf("We read %d bytes\n", numBytes);
                                write(fd, buffer, numBytes);
                        }
//close the fiel

                       close(ab);

                }
                break;

//leave the loop





}

 //  III.  Finished:
//

closedir(dirPtr);

return(NULL);
}

我的代码可以编译,但是在我尝试运行该文件时不会将其发送给客户端。 我尝试了几种不同的字母,但对任何一个字母都无效。 我不太清楚问题是什么,这使得我很难纠正错误。

我不是在寻求答案或其他任何东西,而只是帮助查看我在哪里错了。 感谢您的帮助。

您何时发送与何时发送无文件状态的逻辑似乎是错误的。 认为应该是这样的(警告,我没有测试它,甚至没有在基本语法检查之外对其进行编译,但是您应该明白这一点):

void* handleClient(void*  vPtr)
{
    //  I.  Application validity check:
    int fd = *((int *) vPtr);
    free(vPtr);

    //  II.  Handle the client:
    char buffer[BUFFER_LEN+1];
    read(fd, buffer, BUFFER_LEN+1);
    //read the letter into a buffer//

    const char* dirNamePtr = ".";
    DIR* dirPtr = opendir(dirNamePtr);
    //   Open the current directory

    if (dirPtr == NULL)
    {
        int toSend = htonl(CANT_READ_DIR_CODE);
        write(fd,&toSend,sizeof(toSend));
        printf("Cannot read directory\n");
        return(NULL);
    }

    struct dirent* entryPtr;
    char path[BUFFER_LEN];
    struct stat statBuffer;
    //implements struct dirent to get info on the directory

    //iterates through the directory
    while ((entryPtr=readdir(dirPtr)) != NULL)
    {
        stat(entryPtr->d_name, &statBuffer);
        //puts in metaddata of the current directory into statbuffer

        // if this isn't a regular file OR the first char doesn't match...
        if (!S_ISREG(statBuffer.st_mode) || entryPtr->d_name[0]!=buffer[0])
            continue;

        int ab;
        int numRead;
        int numBytes;
        char buffer[BUFFER_LEN];
        //open the file and send bytes of file and file size to client

        ab = open(entryPtr->d_name,O_RDONLY,0660);

        if(ab<0) {
            int toSend3 = htonl(CANT_READ_FILE_CODE);
            write(fd,&toSend3, sizeof(toSend3));
            printf("Cannot read <filename>\n");
            closedir(dirPtr);
            return(NULL);
        }

        numBytes=htonl(statBuffer.st_size);
        write(fd, &numBytes, sizeof(numBytes));

        printf("Sending %s, %d bytes\n",entryPtr >d_name,statBuffer.st_size);
        while((numBytes=read(ab,buffer,BUFFER_LEN))>0)
        {
            printf("We read %d bytes\n", numBytes);
            write(fd, buffer, numBytes);
        }

        //close the file and leave
        close(ab);
        break;
    }

    // if this is NULL it means we dind't send anything. we break the loop
    //  when a file to send it discovered.
    if (entryPtr == NULL)
    {
        printf("No matching file\n");
        int toSend2 = htonl(NO_MATCH_CODE);
        write(fd, &toSend2, sizeof(toSend2));
    }

    //  III.  Finished:
    closedir(dirPtr);

    return(NULL);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM