gpt4 book ai didi

C socket编程,服务器发送缓冲区似乎没有被清除

转载 作者:行者123 更新时间:2023-12-04 10:38:55 24 4
gpt4 key购买 nike

我是套接字编程的新手,遇到了麻烦。我想实现一个根据特定客户端请求进行应答的服务器。就我而言,GET、HEAD 或其他错误。考虑以下代码

如果我在调用 send() 之前打印答案(请参阅下面的服务器端代码),则消息是正确的。但是让我们从我发送的客户端说起

  1. 获取
  2. 负责人
  3. 获取
  4. 测试
  5. 获取

,客户端打印出来的答案是

You want GET
You want HEAD
You want GETD
HTTP/1.1 400 Bad Request
You want GET Bad Request

所以看起来从服务器发送的消息有点“被覆盖”,但这如何避免呢?如果这是问题所在,是否可以“清除服务器缓冲区”?

这是完整的服务端代码

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define BUFSIZE 1024
#define MAXPENDING 100

int main(){

char get[] = "GET";
char head[] = "HEAD";
int serverSocket = socket(PF_INET, SOCK_STREAM, 0);

struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&serverAddress.sin_zero, '\0', 8);

bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress));

listen(serverSocket, MAXPENDING);

for(;;){
struct sockaddr_in clientAddress;
int clientAddressLength = sizeof(clientAddress);
int clientSocket;

clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddress, &clientAddressLength);

char buf[BUFSIZE];
int bytesRec;

bytesRec = recv(clientSocket, buf, BUFSIZE, 0);
while(bytesRec > 0){

char *result;
result = strtok(buf, " ");

printf("result %s\n", result);

if(strcmp(&buf, &get) == 0){
char answer[] = "You want GET";
send(clientSocket, answer, strlen(answer), 0);
}else{
if(strcmp(&buf, &head) == 0){
char answer[] = "You want HEAD";
send(clientSocket, answer, strlen(answer), 0);
}else{
char answer[] = "HTTP/1.1 400 Bad Request";
send(clientSocket, answer, strlen(answer), 0);
}
}

bytesRec = recv(clientSocket, buf, BUFSIZE, 0);
}

return 0;
}

和客户端

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int main(){

int clientSocket = socket(PF_INET, SOCK_STREAM, 0);

struct sockaddr_in clientAddress;
clientAddress.sin_family = AF_INET;
clientAddress.sin_port = 0;
clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&clientAddress.sin_zero, '\0', 8/*sizeof(clientAddress.sin_zero)*/);

bind(clientSocket, (struct sockaddr*)&clientAddress, sizeof(clientAddress));

struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(&serverAddress.sin_zero, '\0', 8);

if(connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1){
printf("error in connecting!\n");
return 0;
}

char* serverReply[1024];

for(;;){
char request[100];
printf("Enter your request: ");
scanf("%s", &request);
send(clientSocket, request, strlen(msg), 0);
if(recv(clientSocket, serverReply, strlen(serverReply), 0) < 0){
printf("failure in receiving from server!\n");
}else{
printf("%s\n", serverReply);
}
}

close(clientSocket);

return 0;
}

最佳答案

send(clientSocket, request, strlen(msg), 0);

这只发送字符串字符,不包括终止 NULL。接收缓冲区不会终止服务器端接收到的字符串。

这就是您仍然看到上一条消息中的“D”字符的原因。

You want GETD <-- 'D' is from the previous 'HEAD'

要么在服务器上终止接收到的字符串,

buf[bytesRec] = '\0';

编辑:正如@Zan 指出的那样,不要这样做:

或从客户端发送 NULL 终止符。

send(clientSocket, request, strlen(msg) + 1, 0);

关于C socket编程,服务器发送缓冲区似乎没有被清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15747345/

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