C套接字,文件后发送消息,TCP

时间:2014-12-09 19:58:27

标签: c linux sockets send

我想通过套接字发送文件,之后我想发送三条消息。下面的代码有效,但我必须发送两次第一句话。我的问题是为什么?没有一个句子1,接收者显示句子2,句子3和句子3。这段代码有什么问题? 我使用的是tcp协议。

Sender.c

char file_size[256];
struct stat file_stat;
int sent_bytes = 0;
int fd;
int offset;
int remain_data;

fd = open(FILE_TO_SEND, O_RDONLY);
if (fd == -1)
{
        fprintf(stderr, "Error opening file --> %s", strerror(errno));

        exit(EXIT_FAILURE);
}

/* Get file stats */
if (fstat(fd, &file_stat) < 0)
{
        fprintf(stderr, "Error fstat --> %s", strerror(errno));

        exit(EXIT_FAILURE);
}

fprintf(stdout, "File Size: \n%d bytes\n", file_stat.st_size);

sprintf(file_size, "%d", file_stat.st_size);

write(sck, file_size, 256);

char buffer[1024] = "";


while (1) {
    int bytes_read = read(fd, buffer, sizeof(buffer));
    if (bytes_read == 0) 
        break;

    void *p = buffer;
    while (bytes_read > 0) {
        int bytes_written = write(sck, p, bytes_read);
        if (bytes_written <= 0) {
            // handle errors
        }
        bytes_read -= bytes_written;
        p += bytes_written;

    }
}


char sentence[1024];


write(sck, sentence1, 1024);

write(sck, sentence1, 1024);

write(sck, sentence2, 1024);

write(sck, sentence3, 1024);

Receiver.c

char buffer[1024] = "";
int sck = *((int*) arg);

int file_size;

read(sck, buffer, 256);
file_size = atoi(buffer);

ssize_t len;
FILE *received_file;
int remain_data = 0;


received_file = fopen("plik.pdf", "w");
if (received_file == NULL)
{
        fprintf(stderr, "Failed to open file foo --> %s\n", strerror(errno));

        exit(EXIT_FAILURE);
}

remain_data = file_size;

while (((len = recv(sck, buffer, 1024, 0)) > 0) && (remain_data > 0))
{
        fwrite(buffer, sizeof(char), len, received_file);
        remain_data -= len;
}
fclose(received_file);



read (sck, buffer, 1024);
printf("%s 1\n", buffer);    

read (sck, buffer, 1024);
printf("%s 2\n", buffer);

read (sck, buffer, 1024);
printf("%s 3\n", buffer);

1 个答案:

答案 0 :(得分:2)

与TCP协议无关。你的逻辑是有缺陷的。您正在接收数据,然后检查remain_data。所以你的sentence1被接收并在while循环中被丢弃。

将你的recv()移动到while的主体中以解决此问题或更改while中的顺序。

while ((remain_data > 0) && ((len = recv(sck, buffer, 1024, 0)) > 0))

在修改后的while中,只有当remaining_data&gt;才调用recv()。如果remaining_data == 0(延迟评估),则不调用recv()。因此,您的while循环在收到文件后立即结束并准备接收您的句子。在你的代码中,while循环读取第一个句子,然后检查了remaining_data&gt; 0有效地放弃了句子1