Я работаю над простым клиент-серверным приложением. Однако после запуска клиента я получаю сообщение об ошибке 10038 с recv () на стороне сервера. Дескриптор номера сокета сохраняет одинаковое значение как на клиенте, так и на сервере, поэтому я думаю, что ошибки сокета нет. Любая помощь будет оценена.
клиент:
#ifndef UNICODE
#define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
// Need to link with Ws2_32.lib.
#pragma comment(lib, "ws2_32.lib")
int wmain()
{
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
printf("WSAStartup() failed with error: %d\n", iResult);
return 1;
}
// Create a socket for connecting to server.
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket() failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
printf("Socket descriptor: %d\n",ConnectSocket);
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
sockaddr_in Service;
memset(&Service, 0, sizeof(Service));
Service.sin_family = AF_INET;
Service.sin_addr.s_addr = inet_addr("127.0.0.1");
Service.sin_port = htons(27015);
// Connect to server.
iResult = connect(ConnectSocket, (SOCKADDR *) &Service, sizeof (Service));
if (iResult == SOCKET_ERROR) {
printf("connect() failed with error: %ld\n", WSAGetLastError());
iResult = closesocket(ConnectSocket);
if (iResult == SOCKET_ERROR)
printf("closesocket() failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
printf("Connected to server.\n");
// Message that has to be sent.
char receiveBuffer[1000];
char message[1000];
printf("\nEnter message: ");
gets_s(message);
printf("Message you wrote is: %s\n", message);
// Send a message.
if (send(ConnectSocket, message, sizeof(message), 0) == SOCKET_ERROR)
{
printf("send() failed with error code: %d\n", WSAGetLastError());
}
printf("Message successfully sent to server.");
// Receive a message.
if (recv(ConnectSocket, receiveBuffer, 1000, 0) == SOCKET_ERROR)
{
printf("recv() failed with error code: %d\n", WSAGetLastError());
while(1);
}
printf("\nServer says:");
printf(receiveBuffer,sizeof(receiveBuffer));
while(1);
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
сервер:
#ifndef UNICODE
#define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
// Need to link with Ws2_32.lib
#pragma comment(lib, "ws2_32.lib")
int wmain()
{
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
printf("WSAStartup() failed with error: %d\n", iResult);
return 1;
}
// Create a socket for connecting to client.
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket() failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
printf("Socket descriptor: %d\n", ConnectSocket);
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
sockaddr_in Service;
memset(&Service, 0, sizeof(Service));
Service.sin_family = AF_INET;
Service.sin_addr.s_addr = inet_addr("127.0.0.1");
Service.sin_port = htons(27015);
//Bind.
if (bind(ConnectSocket, (struct sockaddr *)&Service, sizeof(Service)) == SOCKET_ERROR)
{
printf("Bind failed with error code: %d\n" , WSAGetLastError());
}
printf("Bind done.\n");
// Listen on the socket for a client.
if (listen(ConnectSocket, 1) == SOCKET_ERROR)
{
printf ("listen() failed with error: %ld\n", WSAGetLastError() );
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("listen() run successfully.\n");
// Accept a connection from a client.
SOCKET acceptSocket;
acceptSocket = accept(ConnectSocket, NULL, NULL);
if (acceptSocket == INVALID_SOCKET) {
printf("accept() failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
else{
printf("accept() run successfully.\n");
}
// No longer need server socket.
closesocket(ConnectSocket);
char receiveBuffer[1000];
int recv_len;
printf("\nWaiting for data...\n");
fflush(stdout);
// Receive a message.
if (recv_len = recv(ConnectSocket, receiveBuffer, 1000, 0) == SOCKET_ERROR)
{
printf("Socket descriptor, after recv(): %d\n", ConnectSocket);
printf("recv() failed with error code: %d\n", WSAGetLastError());
while(1);
}
// Send a message.
if (send(ConnectSocket, receiveBuffer, recv_len, 0) == SOCKET_ERROR)
{
printf("sendto() failed with error code: %d\n", WSAGetLastError());
while(1);
}
else
printf("\nMessage sent back to client.");
while(1);
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
Я новичок в программировании Winsock, и любая помощь будет оценена.
Ошибка 10038 WSAENOTSOCK, Это означает, что у вас нет действительного сокета. На стороне сервера вы используете сокет сервера (ConnectSocket
) после того, как вы его закрыли. Для получения и отправки необходимо использовать подключенный разъем (acceptSocket
) вместо Кроме того, вам нужно закрыть acceptSocket
когда вы закончите с этим, не закрывайте ConnectSocket
второй раз.
Других решений пока нет …