Home > Software engineering >  The recv () WSAGetLasterror 10038 error for help
The recv () WSAGetLasterror 10038 error for help

Time:09-18

I learning network programming, to achieve a retroreflection server functions, code is as follows:
 
The server-side code
#include
#include
# pragma comment (lib, "WS2_32. Lib")
# define PORT 8888
# define ADDR "127.0.0.1
"
Int main ()
{
Char buf [100].
Int iResult=0;
The SOCKET sconnection=INVALID_SOCKET;
Struct sockaddr_in serAddr;
//socket initialization
WSADATA mydata.
If (WSAStartup (MAKEWORD (2, 2), & amp; Mydata)!=0)
{
Printf (" socket initialization failed: % d \ n ", WSAGetLastError ());
exit(0);
}
Printf (" initialization success... \n");
//create a socket
If ((sconnection=socket (AF_INET SOCK_STREAM, 0))==INVALID_SOCKET)
{
Printf (" failed to create a socket: % d \ n ", WSAGetLastError ());
WSACleanup ();
return 0;
}
Printf (" TCP socket to create success... \n");

//set the address of the server structure
SerAddr. Sin_family=AF_INET;
SerAddr. Sin_port=htons (PORT);
SerAddr. Sin_addr. S_un. S_addr=inet_addr (ADDR);

//to connect to the server
Printf (" is the connection to % s: % d \ n ", inet_ntoa (serAddr. Sin_addr), htons (serAddr. Sin_port));
If (connect (sconnection, (struct sockaddr *) & amp; SerAddr, sizeof (serAddr))==SOCKET_ERROR)
{
Printf (" connection: % d \ n ", WSAGetLastError ());
Closesocket (sconnection);
WSACleanup ();
return 0;
}
Printf (" connection success... \n");
Printf (" ready to send data... \n");


//send the data in the buffer
While (1)
(
Memset (buf, 0, sizeof (buf));//initialize the buffer
Printf (" want to send data for: \ n ");
Gets (buf);
If (iResult=send (sconnection, buf, sizeof (buf), 0)==SOCKET_ERROR)
{
Printf (" sending data failure: % d \ n ", WSAGetLastError ());
Closesocket (sconnection);
WSACleanup ();
return 0;
}
Printf (" data sent successfully to % s \ n ", inet_ntoa (serAddr. Sin_addr));
Memset (buf, 0, sizeof (buf));//initialize the buffer

//receive the data returned from the server
If (iResult=recv (sconnection, buf, strlen (buf), 0)==SOCKET_ERROR)
{
Printf (" receiving data failure: % d \ n ", WSAGetLastError ());
}
Printf (" the data has been successfully received from % s \ n ", inet_ntoa (serAddr. Sin_addr));
The printf (buf);
);
Printf (" ready to close the connection... \n");
Closesocket (sconnection);
WSACleanup ();
return 0;
}


 
The client code:
#include
#include
# pragma comment (lib, "WS2_32. Lib")
# define PORT 8888
# define ADDR "127.0.0.1
"
Int main ()
{
Char buf [100].
Int iResult=0;
The SOCKET sconnection=INVALID_SOCKET;
Struct sockaddr_in serAddr;
//socket initialization
WSADATA mydata.
If (WSAStartup (MAKEWORD (2, 2), & amp; Mydata)!=0)
{
Printf (" socket initialization failed: % d \ n ", WSAGetLastError ());
exit(0);
}
Printf (" initialization success... \n");
//create a socket
If ((sconnection=socket (AF_INET SOCK_STREAM, 0))==INVALID_SOCKET)
{
Printf (" failed to create a socket: % d \ n ", WSAGetLastError ());
WSACleanup ();
return 0;
}
Printf (" TCP socket to create success... \n");

//set the address of the server structure
SerAddr. Sin_family=AF_INET;
SerAddr. Sin_port=htons (PORT);
SerAddr. Sin_addr. S_un. S_addr=inet_addr (ADDR);

//to connect to the server
Printf (" is the connection to % s: % d \ n ", inet_ntoa (serAddr. Sin_addr), htons (serAddr. Sin_port));
If (connect (sconnection, (SOCKADDR *) & amp; SerAddr, sizeof (serAddr))==SOCKET_ERROR)
{
Printf (" connection: % d \ n ", WSAGetLastError ());
Closesocket (sconnection);
WSACleanup ();
return 0;
}
Printf (" connection success... \n");
Printf (" ready to send data... \n");


//send the data in the buffer
For (int I=0;; I++)
{
Memset (buf, 0, sizeof (buf));//initialize the buffer
Printf (" want to send data for: \ n ");
Gets (buf);
If (iResult=send (sconnection, buf, sizeof (buf), 0)==SOCKET_ERROR)
{
Printf (" sending data failure: % d \ n ", WSAGetLastError ());
Closesocket (sconnection);
WSACleanup ();
return 0;
}
Printf (" data sent successfully to % s \ n ", inet_ntoa (serAddr. Sin_addr));
//memset (buf, 0, sizeof (buf));//initialize the buffer

//receive the data returned from the server
If (iResult=recv (sconnection, buf, strlen (buf), 0)==SOCKET_ERROR)
{
Printf (" receiving data failure: % d \ n ", WSAGetLastError ());
}
Printf (" the data has been successfully received from % s \ n ", inet_ntoa (serAddr. Sin_addr));
The printf (buf);
}
Printf (" ready to close the connection... \n");
Closesocket (sconnection);
WSACleanup ();
return 0;
}


Debugging results:
Once the commissioning client, the server side haven't receive data shows that the error, to find a lot of information on the Internet, a socket problem, but a bit see not too understand, hope, give some Suggestions, what's the eldest brother, otherwise I won't solve this problem well,

CodePudding user response:

10038 WSAENOTSOCK
The server-side code how didn't see listen the accept?


CodePudding user response:

Oh oh, copy didn't completely, change immediately

CodePudding user response:

The server-side code:
#include
#include
# pragma comment (lib, "WS2_32. Lib")
# define PORT 8888
# define ADDR "127.0.0.1
"Int main ()
{
Char buf [100].
Int iResult=0;
The SOCKET listensocket=INVALID_SOCKET;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related