使用 PuTTY 连接到 winsock 服务器时出现错误“收到的数据包上的填充长度无效”
Posted
技术标签:
【中文标题】使用 PuTTY 连接到 winsock 服务器时出现错误“收到的数据包上的填充长度无效”【英文标题】:Error "Invalid padding length on received packet" when connecting to winsock server using PuTTY 【发布时间】:2021-11-18 13:01:02 【问题描述】:我大多是网络新手,所以我想我会从一些简单的东西开始,所以我试图制作一个简单的 C++ 回显服务器。我正在使用 PuTTY 进行测试。当我通过 PuTTY 连接到服务器时,出现 PuTTY 错误
收到的数据包填充长度无效
当我检查服务器控制台时,它说 PuTTY 客户端已连接但立即断开连接。 这是我的代码。
#include <stdlib.h>
#include <WS2tcpip.h> //includes the winsock file as well
#include <string>
#pragma comment (lib,"ws2_32.lib")
#define PORT 17027
int main()
//Initialize winsock
WSADATA wsData;
WORD ver = MAKEWORD(2, 2);
int wsOK = WSAStartup(ver, &wsData);
if (wsOK != 0)
std::cout << "Can't initialize wonsock! Quitting" << std::endl;
return 0;
//Create a socket
SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (listenSocket == INVALID_SOCKET)
std::cout << "Can't create socket! Quitting" << std::endl;
return 0;
//Bind the socket to an ip address and port
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(PORT);
hint.sin_addr.S_un.S_addr = INADDR_ANY;
bind(listenSocket, (sockaddr *)&hint, sizeof(hint));
//Tell Winsock the socket is for listening
listen(listenSocket, SOMAXCONN);
//Wait for connection
sockaddr_in client;
int clientSize = sizeof(client);
SOCKET clientSocket = accept(listenSocket, (sockaddr*)&client, &clientSize);
if (clientSocket == INVALID_SOCKET)
std::cout << "Cant accept client! Quitting" << std::endl;
return 0;
char host[NI_MAXHOST]; // Client's remote name;
char service[NI_MAXHOST]; // Client's (i.e. port) the client is connected on
ZeroMemory(host, NI_MAXHOST);
ZeroMemory(service, NI_MAXHOST);
if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXHOST, 0) == 0) // try to get name of client
std::cout << host << " connected on port " << service << std::endl;
else
inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST); //get address of client
std::cout << host << " connected on port " << ntohs(client.sin_port) << std::endl;
//Close listening socket
closesocket(listenSocket);
//While loop: accept and echo message back to client
char buf[4096];
while (true)
ZeroMemory(buf, 4096);
//Wait for client to send data
int bytesReceived = recv(clientSocket, buf, 4096, 0);
if (bytesReceived == SOCKET_ERROR)
std::cerr << "Error in recv(). Quitting" << std::endl;
break;
if (bytesReceived == 0)
std::cerr << "Client disconnected" << std::endl;
break;
//Echo message back to client
send(clientSocket, buf, bytesReceived + 1, 0);
//Close the socket
closesocket(clientSocket);
//Cleanup winsock
WSACleanup();
return 0;
【问题讨论】:
【参考方案1】:您似乎正在尝试使用 SSH 连接。您的代码不是 SSH 服务器。
要使用 PuTTY 连接到原始套接字服务器,您需要选择“原始”连接类型。
【讨论】:
以上是关于使用 PuTTY 连接到 winsock 服务器时出现错误“收到的数据包上的填充长度无效”的主要内容,如果未能解决你的问题,请参考以下文章