WinSock 2 -使用getpeername()函数时套接字崩溃

C++ WinSock 2 - socket crashing when getpeername() function is used

本文关键字:函数 套接字 崩溃 使用 getpeername WinSock      更新时间:2023-10-16

每当我使用函数getpeername()时,我的程序崩溃并在调试时返回255。如果printf是主机名,当它开始尝试printf是对等名时,它说"对等名:",然后在它告诉我对等名之前崩溃。我知道这个节目没什么意义。我只是弄乱了一些WinSock 2函数,看看它们都做了什么。

我代码:

#include <windows.h>
#include <winsock2.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define SCK_VERSION2 0x0202
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT 27015
int main() {
    //----------------------
    // Declare and initialize variables.
    WSADATA wsaData;
    int iResult;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct sockaddr_in clientService;
    char name[500] = "";
    sockaddr sName;
    int sNameSize =  sizeof(sName);
    char *sendbuf = "Test";
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;
    //----------------------
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != NO_ERROR) {
      printf("WSAStartup failed: %dn", iResult);
      return 1;
    }
    //----------------------
    // Create a SOCKET for connecting to server
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        printf("Error at socket(): %in", WSAGetLastError() );
        WSACleanup();
        return 1;
    }
    //----------------------
    // The sockaddr_in structure specifies the address family,
    // IP address, and port of the server to be connected to.
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    clientService.sin_port = htons(444);
    //----------------------
    // Connect to server.
    iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
    if ( iResult == SOCKET_ERROR) {
        closesocket (ConnectSocket);
        printf("Unable to connect to server: %in", WSAGetLastError());
        WSACleanup();
        return 1;
    }
    //----------------------
    //Get local host name
    iResult = gethostname(name, sizeof(name));
    if (iResult == NO_ERROR) {
        printf("Host Name: %sn", name);
    }
    else if (iResult == SOCKET_ERROR) {
        printf("Could not resolve host name: %i", WSAGetLastError());
    }
    iResult = getpeername(ConnectSocket, (struct sockaddr*)&sName, &sNameSize);
    if (iResult == NO_ERROR)
        printf("Peer Name: %s", sName);
    else if (iResult == SOCKET_ERROR)
        printf("Could not get peer name: %i", WSAGetLastError());
    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
        printf("send failed: %dn", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }
    printf("Bytes Sent: %in", iResult);
    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed: %dn", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }
    // Receive until the peer closes the connection
    do {
        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if ( iResult > 0 )
            printf("Bytes received: %sn", recvbuf); //printf("Bytes received: %dn", iResult);
        else if ( iResult == 0 )
            printf("Connection closedn");
        else
            printf("recv failed: %dn", WSAGetLastError());
    } while( iResult > 0 );
    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();
    system("PAUSE");
    return 0;
}

试试这个:

<>之前sockaddr_in sockaddr;Int namelen = sizeof(sockaddr);如果(!Getpeername (socket, (struct sockaddr *)&sockaddr, &namelen)){printf("对等名称:% s n",inet_ntoa ((in_addr) (* (in_addr *) &sockaddr.sin_addr.S_un.S_addr)));}

你的电话有两个问题。

  1. 您的套接字是IPv4套接字,因此getpeername()期望sockaddr_in,就像connect()一样。但是您已经将sName变量声明为sockaddr。您需要为套接字类型使用正确的结构类型:

    sockaddr_in sName;
    
  2. 你试图打印出sName,如果它是一个以空结尾的字符串,但它不是。它是一个二元结构。您必须将其内容转换为以空结束的字符串,例如inet_ntoa():

    iResult = getpeername(ConnectSocket, (struct sockaddr*)&sName, &sNameSize);
    if (iResult == 0)
        printf("Peer Name: %s", inet_ntoa(sName.sin_addr));
    else
        printf("Could not get peer name: %i", WSAGetLastError());