0

我的服务器没有收到来自客户端的任何数据,但没有错误,所以我真的不知道出了什么问题。在客户端的 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;
}

我希望有人能帮帮忙。

4

1 回答 1

0

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

setbuf(stdout, NULL);

stdout或者只是在你之后冲洗printf()

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);
于 2013-01-30T09:57:05.317 回答