服务器不会第二次接收

时间:2018-12-03 12:37:00

标签: c linux sockets

我有一个客户端和服务器,服务器是通过以下方式设置的:

int listenS = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in s = { 0 };
s.sin_family = AF_INET;
s.sin_port = htons(PORT);
s.sin_addr.s_addr = htonl(IP_ADDR);
bind(listenS, (struct sockaddr*)&s, sizeof(s));
listen(listenS, QUEUE_LEN);

struct sockaddr_in clientIn;
int clientInSize = sizeof clientIn;
while (1)
{
    int newfd = accept(listenS, (struct sockaddr*)&clientIn, (socklen_t*)&clientInSize);
    //......

我只是删除了一些测试以使代码更易读)

客户就是:

int sock = socket(AF_INET, SOCK_STREAM, 0), nrecv;
struct sockaddr_in s = { 0 };
s.sin_family = AF_INET;
s.sin_port = htons(PORT);
s.sin_addr.s_addr = htonl(IP_ADDR);
if (connect(sock, (struct sockaddr*)&s, sizeof(s)) < 0)
{ //......

我建立了连接,并且一切正常,服务器recv第一次从客户端发送消息,但是当我尝试向服务器发送send另一条消息时,服务器不会阻止recv调用并且什么都不会得到(返回缓冲区大小,而不是0

这是客户端代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <sys/stat.h>

#define PORT 0x0da2
#define IP_ADDR 0x7f000001
#define MAX_BUFFER_SIZE 1024

int send_all(int socket, void* buffer, size_t length)
{
    char *ptr = (char*)buffer;
    while (length > 0)
    {
        int i = send(socket, ptr, length, 0);
        if (i < 1) return -1;
        ptr += i;
        length -= i;
    }
    return 0;
}

int main(int argc, char** argv)
{
    if (argc > 1)
    {
        if ((strcmp(argv[1], "list-files") != 0) &&
            (strcmp(argv[1], "upload-file") != 0) &&
            (strcmp(argv[1], "download-file") != 0) &&
            (strcmp(argv[1], "search") != 0))
            {
                perror("The arguments are incorrect.");
            }

        int sock = socket(AF_INET, SOCK_STREAM, 0), nrecv;
        struct sockaddr_in s = { 0 };
        s.sin_family = AF_INET;
        s.sin_port = htons(PORT);
        s.sin_addr.s_addr = htonl(IP_ADDR);
        if (connect(sock, (struct sockaddr*)&s, sizeof(s)) < 0)
        {
            perror("connect");
            return 1;
        }
        printf("Successfully connected.\n");

        char sendBuffer[MAX_BUFFER_SIZE];
        int lenOfArgv = strlen(argv[1]);
        int sendBufferIndex = 0;
        for (int i = 0;
            i < lenOfArgv && sendBufferIndex < MAX_BUFFER_SIZE;
            i++, sendBufferIndex++)
        {
            sendBuffer[sendBufferIndex] = argv[1][i];
        }

        if (argc == 3)
        {
            sendBuffer[sendBufferIndex++] = ' ';
            int lenOfArgv = strlen(argv[2]);
            for (int i = 0;
                i < lenOfArgv && sendBufferIndex < MAX_BUFFER_SIZE;
                i++, sendBufferIndex++)
            {
                sendBuffer[sendBufferIndex] = argv[2][i];
            }
        }

        sendBuffer[sendBufferIndex] = 0;

        // + 1 for terminating null
        if (send_all(sock, sendBuffer, strlen(sendBuffer) + 1) < 0)
        {
            perror("send buffer to server failed");
            return 1;
        }

        if(strcmp(argv[1], "download-file") == 0)
        {
            char sizeBuffer[256];
            recv(sock, sizeBuffer, 256, 0);
            int fileSize = atoi(sizeBuffer);

            if(fileSize > 0)
            {
                FILE* recievedFile = fopen(argv[2], "w");
                if(recievedFile != NULL)
                {
                    int remainData = fileSize;
                    size_t len;
                    char fileBuffer[MAX_BUFFER_SIZE];
                    while(((len = recv(sock, fileBuffer, MAX_BUFFER_SIZE, 0)) > 0 && (remainData > 0)))
                    {
                        fwrite(fileBuffer, sizeof(char), len, recievedFile);
                        remainData -= len;
                        printf("Received %d bytes, %d is left..\n", len, remainData);
                    }
                    fclose(recievedFile);
                    printf("File downloaded!\n");
                }
                else
                {
                    perror("Failed to download file\n");
                }
            }
        }
        else if(strcmp(argv[1], "upload-file") == 0)
        {
            char filePath[MAX_BUFFER_SIZE];
            sprintf(filePath, "%s", argv[2]);
            int fd = open(filePath, O_RDONLY);
            int downloadFailed = 0;
            if (fd != -1)
            {
                struct stat file_stat;
                if(fstat(fd, &file_stat) >= 0)
                {
                    char fileSize[256];
                    sprintf(fileSize, "%d", (int)file_stat.st_size);
                    int len = send(sock, fileSize, sizeof(fileSize), 0);
                    if(len >= 0)
                    {
                        int remainData = file_stat.st_size;
                        off_t offset = 0;
                        int sent_bytes = 0;
                        while(((sent_bytes = sendfile(sock, fd, &offset, MAX_BUFFER_SIZE)) > 0) && (remainData > 0))
                        {
                            remainData -= sent_bytes;
                            printf("sent %d bytes, %d is left...\n", sent_bytes, remainData);
                        }
                    }else {downloadFailed = 1;}
                }else {downloadFailed = 1;}
            }else {downloadFailed = 1;}

            if(downloadFailed == 1)
            {
                perror("Failed to download file!\n");
            }
        }
        else
        {
            char someBuffer[MAX_BUFFER_SIZE];
            // nrecv is the number of bytes that we recieved
            if ((nrecv = recv(sock, someBuffer, MAX_BUFFER_SIZE, 0)) < 0)
            {
                perror("recv");
                return 1;
            }
            printf("%s\n", someBuffer);
        }

        close(sock);
        return 0;
    }
    else
    {
        perror("The arguments are incorrect.");
    }
}

这是服务器代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <math.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/sendfile.h>

#define PORT 0x0da2 // 3490
#define IP_ADDR 0x7f000001 // 127.0.0.1
#define QUEUE_LEN 20
#define MAX_BUFFER_SIZE 1024

int send_all(int socket, void* buffer, int length)
{
    char *ptr = (char*)buffer;
    while (length > 0)
    {
        int i = send(socket, ptr, length, 0);
        if (i < 1) return -1;
        ptr += i;
        length -= i;
    }
    return 0;
}

void list_dir()
{
    DIR * directory;
    struct dirent* dir;
    directory = opendir(".");
    if (directory)
    {
        while ((dir = readdir(directory)) != NULL)
        {
            printf("%s\n", dir->d_name); // /home/text.txt, text.txt
            // get filesize (in bytes0 with dir->d_name
        }
    }
}

void list_files(char* buffer, int withBytes = 0)
{
    DIR* d;
    struct dirent* dir;
    d = opendir("data");
    int bufferIndex = 0;
    while((dir = readdir(d)) != NULL)
    {
        char tempFilename[256] = "data/";
        int tempIndex = 5;
        char* scan = dir->d_name;
        while(*scan)
        {
            tempFilename[tempIndex++] = *scan;
            buffer[bufferIndex++] = *scan++;
        }
        tempFilename[tempIndex] = 0;
        struct stat st = {0};
        stat(tempFilename, &st);
        int fileSize = st.st_size;

        if(withBytes == 1)
        {
            // Adding file size to the buffer
            bufferIndex += sprintf(&buffer[bufferIndex], " %d bytes", fileSize);
        }

        buffer[bufferIndex++] = '\n';
    }
    buffer[bufferIndex] = 0;
    closedir(d);
}

int main(void)
{
    int listenS = socket(AF_INET, SOCK_STREAM, 0);
    if (listenS < 0)
    {
        perror("socket");
        return 1;
    }
    struct sockaddr_in s = { 0 };
    s.sin_family = AF_INET;
    s.sin_port = htons(PORT);
    s.sin_addr.s_addr = htonl(IP_ADDR);
    if (bind(listenS, (struct sockaddr*)&s, sizeof(s)) < 0)
    {
        perror("bind");
        return 1;
    }
    if (listen(listenS, QUEUE_LEN) < 0)
    {
        perror("listen");
        return 1;
    }
    struct sockaddr_in clientIn;
    int clientInSize = sizeof clientIn;

    struct stat st = {0};
    if(stat("data", &st) == -1)
    {
        mkdir("data", 0700);
    }

    while (1)
    {
        int newfd = accept(listenS, (struct sockaddr*)&clientIn, (socklen_t*)&clientInSize);
        if (newfd < 0)
        {
            perror("accept");
            return 1;
        }
        int pid = fork(); // creating new thread 
        if (pid == 0)
        {
            close(listenS); // duplicate=> thats why we need to close the socket

            char someBuffer[MAX_BUFFER_SIZE];
            int nrecv;
            if ((nrecv = recv(newfd, someBuffer, MAX_BUFFER_SIZE, 0)) < 0)
            {
                perror("recv");
                return 1;
            }
            printf("Message recieved: %s\n", someBuffer);

            // Here we read the command the argument and split them
            // into seperate variables
            char command[256];
            char argument[256];
            int commandHasBeenSet = 0;
            char* token = strtok(someBuffer, " ");
            while(token != NULL)
            {
                if(commandHasBeenSet == 0)
                {
                    strcpy(command, token);
                    commandHasBeenSet = 1;
                }
                else
                {
                    strcpy(argument, token);   
                }
                token = strtok(NULL, " ");
            }

            if (strcmp(command, "list-files") == 0)
            {
                char buffer[MAX_BUFFER_SIZE];
                list_files(buffer, 1);

                if (send_all(newfd, buffer, strlen(buffer) + 1) < 0)
                {
                    perror("send buffer to client failed");
                    return 1;
                }
                printf("Sent a message to a client!\n");
            }
            else if (strcmp(command, "upload-file") == 0)
            {
                printf("Uploading file %s\n", argument);
                char sizeBuffer[256];
                recv(newfd, sizeBuffer, 256, 0);
                int fileSize = atoi(sizeBuffer); 

                if(fileSize > 0)
                {
                    char filePath[MAX_BUFFER_SIZE];
                    sprintf(filePath, "data/%s", argument);
                    printf("Downloading to %s", filePath);
                    FILE* recievedFile = fopen(filePath, "w");
                    if(recievedFile != NULL)
                    {
                        int remainData = fileSize;
                        size_t len;
                        char fileBuffer[MAX_BUFFER_SIZE];
                        while(((len = recv(newfd, fileBuffer, MAX_BUFFER_SIZE, 0)) > 0 && (remainData > 0)))
                        {
                            fwrite(fileBuffer, sizeof(char), len, recievedFile);
                            remainData -= len;
                            printf("Received %d bytes, %d is left..\n", len, remainData);
                        }
                        fclose(recievedFile);
                        printf("File downloaded!\n");
                    }
                    else
                    {
                        perror("Failed to download file\n");
                    }
                }else
                {
                    perror("Failed to get file size for download\n");
                }
            }
            else if (strcmp(command, "download-file") == 0)
            {
                char filePath[MAX_BUFFER_SIZE];
                sprintf(filePath, "data/%s", argument);
                int fd = open(filePath, O_RDONLY);
                int downloadFailed = 0;
                if (fd != -1)
                {
                    struct stat file_stat;
                    if(fstat(fd, &file_stat) >= 0)
                    {
                        char fileSize[256];
                        sprintf(fileSize, "%d", (int)file_stat.st_size);
                        int len = send(newfd, fileSize, sizeof(fileSize), 0);
                        if(len >= 0)
                        {
                            int remainData = file_stat.st_size;
                            off_t offset = 0;
                            int sent_bytes = 0;
                            while(((sent_bytes = sendfile(newfd, fd, &offset, MAX_BUFFER_SIZE)) > 0) && (remainData > 0))
                            {
                                remainData -= sent_bytes;
                                printf("Server sent %d bytes, %d is left...\n", sent_bytes, remainData);
                            }

                        }else {downloadFailed = 1;}
                    }else {downloadFailed = 1;}
                }else {downloadFailed = 1;}

                if(downloadFailed == 1)
                {
                    perror("Failed to download file!\n");
                }
            }
            else if (strcmp(command, "search") == 0)
            {
                char buffer[MAX_BUFFER_SIZE];
                char result[MAX_BUFFER_SIZE];
                int resultIndex = 0;
                list_files(buffer);

                result[0] = 0;

                char tempBuffer[MAX_BUFFER_SIZE];
                strcpy(tempBuffer, buffer);
                token = strtok(tempBuffer, "\n");
                while(token != NULL)
                {
                    char* scanToken = token;
                    char* scanArgument = argument;
                    int found = 1;
                    while(*scanToken && *scanArgument)
                    {
                        if(*scanToken++ != *scanArgument++)
                        {
                            found = 0;
                            break;
                        }
                    }

                    if(found == 1)
                    {
                        if(resultIndex > 0)
                        {
                            result[resultIndex++] = ' ';
                        }
                        strcpy(&result[resultIndex], token);
                        resultIndex += strlen(token);
                        result[resultIndex] = 0;
                    }

                    token = strtok(NULL, "\n");
                }

                if (send_all(newfd, result, strlen(result) + 1) < 0)
                {
                    perror("send buffer to client failed");
                    return 1;
                }
                printf("Sent a message to a client!\n");
            }

            close(newfd);
            exit(0);
        }
        else
            close(newfd);
    }


    close(listenS);
    return 0;
}

如果您运行服务器,然后使用以下命令运行客户端:

./client list-files
./client download-file test.txt

它将正常工作,客户端将从服务器接收消息,反之亦然。

当我尝试运行时出现问题:

./client upload-file test.txt

download-file命令基本相同,只是从客户端复制并粘贴到服务器(相同的逻辑,应该工作相同),除了不同。

具体来说,程序在服务器(recv(newfd, sizeBuffer, 256, 0);)的第175行失败,它的值为0,而不是客户端发送的值。

知道我缺少什么吗?

(我尝试在线搜索,但未找到任何内容)

2 个答案:

答案 0 :(得分:1)

TCP是流协议。没有消息边界,并且服务器的recv与客户端的send不对应。

客户端使用以下命令发送命令

    send_all(sock, sendBuffer, strlen(sendBuffer) + 1)

OTOH,服务器尝试通过以下方式接收它

    nrecv = recv(newfd, someBuffer, MAX_BUFFER_SIZE, 0))

recv不在乎流是否包含'\0'。它盲目地等待MAX_BUFFER_SIZE字节的到来。客户端发送的某些(有价值的)数据位于命令后面的someBuffer中,但服务器忽略了。

服务器必须更加认真地解析答复。为此,您可能需要更详细的协议(例如,为每个字符串加上长度)。

答案 1 :(得分:0)

您假设流套接字将保留您的消息边界。它不会。因此,您发送的内容如下:

upload-file filename\0
NNN\0\0\0...\0   [256 bytes containing filesize]
<content of filename>

因此,您可能在服务器端执行的第recv个字节(MAX_BUFFER_SIZE个字节)不仅接收命令字符串和文件名,还接收文件大小块和整个文件内容。也就是说,您已经收到所有内容,并且它位于someBuffer中。您的初始命令字符串以null终止,因此除非您选中nrecv,否则您将不知道它的其余部分。因此,您的下一个recv调用将获得文件结尾,因为客户端已完成并且已关闭其连接的结尾。

要通过TCP发送已定义的记录,您需要确切地知道每个点需要多少字节,并准确地接收(或准备解析接收到的数据)。另请参见https://stackoverflow.com/a/47440054/1076479