Unix客户端和服务器程序 - 非常奇怪的客户端和服务器套接字错误

时间:2011-09-10 00:29:30

标签: unix client

我目前正在Unix环境中制作客户端和服务器程序。我已经做到了,所以客户端能够将文件的内容上传到客户端。我现在正在向服务器示例添加选项和错误处理程序,客户端必须输入文件名。要做到这一点,我想发送一条消息说好,如果检查了所有选项,但如果我这样做,它似乎导致我的文件读取和发送疯狂,我不明白为什么。

我上传了执行此操作的功能

客户代码

int putFile (char path[256], int fd)
{
    char mystring[1000];
    char buffer[100];
    int i , n;
    FILE * pFile;


    n = read(fd,buffer,100);
    printf("%s", buffer);

    if (strcmp(buffer, "OK") == 0)
    {
        pFile = fopen(path, "r");
        if(pFile != NULL)
        {
            while(fgets(mystring, sizeof(mystring), pFile) != NULL)
            {
                //fputs(mystring, fd);
                write(fd,mystring,strlen(mystring));
            }
        }
        else
        {
            printf("Invalid File or Address \n");
        }

        fclose(pFile);
    }
    else
    {
        printf("%s \n", buffer);
    }
}

用于读取套接字的服务器代码

int putRequest(int fd, char buf[], char str[])
{
    char data[256];

    int number;
    char * ptr;
    char results[100];
    int total = 0;
    char *arguments[1024]; 
    char temp[10];
    int i;

    ptr = strtok(buf," ");
    while (ptr != NULL)
    {
        char * temp; 
        temp = (char *)malloc(sizeof(ptr));
        temp = ptr;
        arguments[total] = temp;
        total++;
        ptr = strtok (NULL, " ");
    }

    if(total == 1)
    {
        strcat(str, "Invaild Arguments \n");
        return 1;
    }

    write(fd, "OK", 256);
    FILE * pFile;
    pFile = fopen ("myfile.txt","w");
    if (pFile!=NULL)
    {

        while(read(fd, data, 256) != NULL)
        {

            fputs(data, pFile);
        }

        fclose (pFile);
    }
    else
    {
        strcat(str, "Invaild File");
        return 0;
    }
    strcat(str, "Done");
    return 1;

}

如果您需要查看更多代码,请提前致谢并发布内容。我刚刚放置了导致问题的代码。

0 个答案:

没有答案
相关问题