gpt4 book ai didi

客户端-服务器套接字 C 代码 : Server doesn't execute code after "read" until another line is sent

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:58 25 4
gpt4 key购买 nike

我正在编写一些代码来创建客户端-服务器连接。客户端向服务器发送字符串,服务器应该根据发送的内容解析和打印字符串。

客户端代码:

// Client side C/C++ program to demonstrate Socket programming
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080

int main(int argc, char const *argv[]) {
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
size_t len = 0;
char *buffer = NULL;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}

memset(&serv_addr, '0', sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}

if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}

while(1) {
getline(&buffer, &len, stdin);
printf("Line sent: %s", buffer);
send(sock, buffer, strlen(buffer), 0);
}
}

服务器代码:

/*Required Headers*/

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include<string.h>

int main()
{

char str[100];
int listen_fd, comm_fd;

struct sockaddr_in servaddr;

listen_fd = socket(AF_INET, SOCK_STREAM, 0);

bzero( &servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htons(INADDR_ANY);
servaddr.sin_port = htons(8080);

bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));

listen(listen_fd, 10);

comm_fd = accept(listen_fd, (struct sockaddr*) NULL, NULL);

while(1)
{
bzero( str, 100);

read(comm_fd,str,100);

printf("Received: %s",str);
if(strncmp(str, "NEW", 3) == 0)
{
printf("Client - NEW");
}
else if(strncmp(str, "FILE", 4) == 0)
{
printf("Client - FILE");
}
else if(strncmp(str, "NAME", 4) == 0)
{
strtok(str, ": ");
printf("NAME: %s", str);
}
else if(strncmp(str, "DESCRIPTION", 11) == 0)
{
strtok(str, ": ");
printf("DESCRIP: %s", str);
}
else if(strncmp(str, "PRINTER", 7) == 0)
{
strtok(str, ": ");
printf("PRINTER: %s", str);
}

}
}

我期望发生的事情是,当我从客户端向服务器发送“NEW”时,服务器会打印“Client - NEW”。然后当我发送“FILE”时,服务器打印“CLIENT - FILE”。发生的情况是一次发送有延迟。因此,当我发送 NEW 时,服务器打印“Received: NEW”,但它不会进入 if 检查以打印“CLIENT - NEW”。然后当我发送“FILE”时,服务器打印“Client - NEWRecieved: FILE”。

我在这里做错了什么?

最佳答案

So when I send NEW, the server prints "Received: NEW"

是的,就在这里:

printf("Received: %s",str);

但具体来说,您发送“NEW\n”,服务器打印“Received: NEW\n”。这个换行符是相关的,因为它触发行缓冲终端上的输出

but it doesn't go into the if check to print "CLIENT - NEW".

是的,它执行条件:

printf("Client - NEW");

但输出会被缓冲,直到打印下一个换行符。

Then when I send "FILE", the server prints "Client - NEWRecieved: FILE".

现在,在您发送“FILE”后,它会被执行:

printf("Received: %s",str);

str 的内容为“FILE\n”。这会刷新当前包含“Client - NEWRecieved: FILE\n”的输出缓冲区。此外

printf("Client - FILE");

被执行(但不输出,仅缓冲)并且过程迭代。

所以解决方法是,在每个 printf 格式字符串的末尾添加一个换行符“\n”(或者在简单情况下使用 puts)。

printf("Received: %s\n",str); // Although "str" will usually contain a newline, but better play it safe

puts("Client - NEW");

此外,如果你有一个基于行的协议(protocol),你应该为你的套接字切换到缓冲输入:

comm_fd = accept(listen_fd, (struct sockaddr*) NULL, NULL);
FILE *sock = fdopen(comm_fd, "r+");

然后逐行读取输入

while (1) {
fgets(str, sizeof(str), sock);

关于客户端-服务器套接字 C 代码 : Server doesn't execute code after "read" until another line is sent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55446092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com