gpt4 book ai didi

c - 服务器不接收来自客户端的数据

转载 作者:行者123 更新时间:2023-11-30 17:55:21 27 4
gpt4 key购买 nike

我的服务器没有从客户端接收到任何数据,但没有错误,所以我真的不知道出了什么问题。在客户端的 while 循环中,我尝试发送消息。服务器停留在recvfrom函数中并且似乎没有获取数据。 cmd 中的 netstat 显示服务器监听端口 50000。我将带有控制台参数的 IPv6 地址::1 提供给客户端。

这是服务器的代码:

#include <WinSock2.h>
#include <ws2ipdef.h>
#include <stdio.h>

#define PORT 50000

#define BUFFERSIZE 64

void error(char *err)
{
fprintf(stderr,"%s: %d\n", err, WSAGetLastError());
getchar();
exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
struct sockaddr_in6 server, client;
SOCKET sock;
char buffer[BUFFERSIZE];
LPTSTR recvBuff[1024];
DWORD recvBuffLen = 1024UL;
int len = sizeof(client);

WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(1,1);
WSAStartup(wVersionRequested, &wsaData);

sock = socket(AF_INET6, SOCK_DGRAM, 0);
if (sock < 0)
error("Fehler beim Anlegen des Sockets");

memset( &server, 0, sizeof (server));
memset( &client, 0, sizeof (client));

server.sin6_family = AF_INET6;
server.sin6_port = htons(PORT);
server.sin6_addr = in6addr_any;

if (bind(sock, (struct sockaddr *) &server, sizeof(server)) == -1)
error("Fehler beim binden des Sockets");

while (1)
{
if (recvfrom(sock, buffer, BUFFERSIZE, 0, (struct sockaddr *)&client, &len) == -1)
error("Fehler beim erhalten von Daten");
printf("...");
printf("Pakete erhalten von: %s:%d\nDaten: %s\n\n", WSAAddressToString((struct sockaddr *)&client.sin6_addr, len, NULL, *recvBuff, &recvBuffLen), PORT, buffer);
}

closesocket(sock);
return 0;
}

这是我的客户的代码:

#include <WinSock2.h>
#include <ws2ipdef.h>
#include <stdio.h>

#define PORT 50000

#define BUFFERSIZE 64

void error(char *err)
{
fprintf(stderr,"%s: %d\n", err, WSAGetLastError());
getchar();
exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
struct sockaddr_in6 server;
SOCKET sock;
char buffer[BUFFERSIZE];
int len = sizeof(server);

WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(1,1);
WSAStartup(wVersionRequested, &wsaData);

if (argc != 2) {
printf("Usage: %s <Server-IP>\n", argv[0]);
getchar();
exit(0);
}

sock = socket(AF_INET6, SOCK_DGRAM, 0);
if (sock == -1)
error("Socket Fehler");

memset( &server, 0, sizeof (server));

server.sin6_family = AF_INET6;
server.sin6_port = htons(PORT);
if ((WSAStringToAddressA(argv[1], AF_INET6, NULL, (SOCKADDR *)&server, &len)) < 0)
error("String konnte nicht aufgelößt werden");

while (1)
{
printf("\nDaten eingeben: (Enter für Text bestätigen, 'q' eingeben um zu beenden)");
scanf("%[^\n]", buffer);
getchar();
if (strcmp(buffer, "q") == 0)
exit(0);

if (sendto(sock, buffer, BUFFERSIZE, 0, (struct sockaddr *)&server, len) == -1)
error("Fehler bei Übertragung");
}

closesocket(sock);
return 0;
}

希望有人能帮忙。

最佳答案

只是猜测:问题可能是 stdout 缓冲 - 尝试禁用它

setbuf(stdout, NULL);

或者只是在 printf() 之后刷新 stdout:

printf("...");
printf("Pakete erhalten von: %s:%d\nDaten: %s\n\n", WSAAddressToString((struct sockaddr *)&client.sin6_addr, len, NULL, *recvBuff, &recvBuffLen), PORT, buffer);
fflush(stdout);

关于c - 服务器不接收来自客户端的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14375499/

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