WinSock 2 -使用getpeername()函数时套接字崩溃
C++ WinSock 2 - socket crashing when getpeername() function is used
每当我使用函数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)));}你的电话有两个问题。
-
您的套接字是IPv4套接字,因此
getpeername()
期望sockaddr_in
,就像connect()
一样。但是您已经将sName
变量声明为sockaddr
。您需要为套接字类型使用正确的结构类型:sockaddr_in sName;
-
你试图打印出
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());
相关文章:
- 接受线程 C++ 套接字中的函数循环
- 如何在类中封装C/C++套接字发送和接收函数?
- 为什么套接字中的 recv() 函数不返回任何内容?
- C++中TCP套接字的recv函数
- 为什么 sys 套接字 recv 函数不填充数据但返回字节长度?
- 套接字 read() 函数在处理隧道时有时从不返回 0
- 如何将函数<void()>写入管道/套接字对?
- 此函数是否对套接字有问题?
- 其中是升压套接字的函数读取
- 为什么 fcntl() 标志值以八进制格式定义,以及此函数如何用于阻塞/非阻塞套接字?
- 套接字:我的select()函数逻辑有未定义的行为
- 套接字错误 gcc 无法在 WinSock2.h 中获取函数
- 是否可以将套接字设置为仅针对recv()函数的非阻塞套接字
- 套接字读取()到一个在C++中返回指针的函数中
- 是否可以通过套接字发送指向函数的指针
- 从安卓客户端发送数据时,C++ 套接字接收函数中第一个字符中的 NUL 值
- 套接字不能用作函数
- 在 c++ 中通过套接字发送图片(发送函数),但未收到完整(Windows)
- 编译使用 libcxx 的套接字函数 bind() 的代码失败
- 在回调函数中使用zmq套接字