When I encountered a socket 10038 invalid socket error, I checked it online for a long time and still failed to solve it. Please give me some advice ....

Source: Internet
Author: User
Tags socket error htons

Server:

 

 

 

# Define recv_posted 0
# Define send_posted 1

Char buffer [1024];

Typedef struct _ per_handle_data
{
Socket socket; // This structure is used to send a socket to the port
} Per_handle_data, * lper_handle_data;

Typedef struct _ per_io_data
{
Overlapped;
Wsabuf wsabuffer;
DWORD recvbyte, sendbyte;
Int operationtype;

} Per_io_data, * lper_io_data;

 

//************************************** **************************************** ***
// Serverworkthread ............
//************************************** **************************************** ***
DWORD winapi serverworkerthread (lpvoid completionportid)
{
Handle completionport = (handle) completionportid;
DWORD bytestransferred;
Per_io_data periodata;
Per_handle_data perhandledata;
DWORD flags;
Wsaevent eventarray [wsa_maximum_wait_events];
DWORD totalevent = 0;
DWORD indexevent;
While (true)
{
If (getqueuedcompletionstatus (completionport, & bytestransferred,
(Lpdword) & perhandledata, (lpoverlapped *) & periodata, infinite) = NULL)
{
Printf ("Wait error... errorcode: % d", wsagetlasterror ());
// Wait failed! Error: wsagetlasterror ()
Continue;
}
// Did not use wsaevent ..................
/* If (bytestransferred = 0 & (periodata-> operationtype = recv_posted
| Periodata-> operationtype = send_posted ))
{
Cout <"socket is disabled! "<Endl;
Closesocket (perhandledata. Socket );
Globalfree (perhandledata );
Continue;
}
*/

Eventarray [totalevent] = wsacreateevent ();
Zeromemory (& periodata. overlapped, sizeof (overlapped ));
Periodata. wsabuffer. Buf = buffer;
Periodata. wsabuffer. Len = 2048;
Periodata. overlapped. hevent = eventarray [totalevent];
Periodata. operationtype = recv_posted;

Indexevent = wsawaitformultipleevents (totalevent, eventarray, false, wsa_infinite, false );
Wsaresetevent (eventarray [indexevent-wsa_wait_event_0]);

//-----------------------------------------
// Determine the status of the overlapped event
Wsagetoverlappedresult (perhandledata. socket, & periodata. overlapped, & bytestransferred, false, & flags );
If (bytestransferred = 0 ){
Printf ("Closing socket % d/N", perhandledata. Socket );
Closesocket (perhandledata. Socket );
Wsacloseevent (eventarray [indexevent-wsa_wait_event_0]);
Return 0;
}

If (periodata. operationtype = recv_posted)
{
Printf ("recved things is: % s/n", periodata. wsabuffer. BUF );
// Received data
}
// Continue receiving data...
Printf ("Go on recving.../N ");
If (wsarecv (perhandledata. socket, & (periodata. wsabuffer), 1, & (periodata. recvbyte), & flags, & (periodata. overlapped), null) = socket_error)
{
// Failed to send data...
Printf ("send error .. errorcode is: % d/N", wsagetlasterror ());
Continue;
}
}
}
//************************************** **************************************** ***
Void main ()
{
Handle completionport;
Wsadata;
System_info systeminfo;
Sockaddr_in serveraddr;
Sockaddr_in clientaddr;
Int clientaddrlen;
Socket listensocket;
Socket newsocket;
Per_handle_data perhandledata; // * perhandledata = NULL;
Per_io_data periodata; // * periodata;
DWORD flags = NULL;

If (wsastartup (makeword (2, 2), & wsadata )! = NULL) // Winsock2
{
Printf ("registration failed! Error: % d/N ", wsagetlasterror ());
Exitprocess (1 );
}

Listensocket = wsasocket (af_inet, sock_stream, 0, null, 0, wsa_flag_overlapped );
If (listensocket = invalid_socket)
{
Printf ("failed to create socket! /N ");
Wsacleanup ();
Exitprocess (1 );
}

//************************************** **************************************** ************
Completionport = createiocompletionport (invalid_handle_value, null, 0, 0 );
If (completionport = invalid_handle_value)
{
Printf ("An error occurred while creating the port object! /N ");
Wsacleanup ();
Exitprocess (1 );
}

Getsysteminfo (& systeminfo); // obtain system information
Printf ("the CPU's num is: % d/N", systeminfo. dwnumberofprocessors );
For (DWORD I = 0; I <systeminfo. dwnumberofprocessors; I ++) // The number of worker threads to be created is two times the number of processors plus two
{
Handle threadhandle;

Threadhandle = createthread (null, 0, serverworkerthread, completionport, 0, null );
Closehandle (threadhandle );
}

// Bind with server ========================================== ======================
Serveraddr. sin_family = af_inet;
Serveraddr. sin_addr.s_addr = htonl (inaddr_any );
Serveraddr. sin_port = htons (8000 );

If (BIND (listensocket, (psockaddr) & serveraddr, sizeof (serveraddr) = socket_error)
{
Printf ("socket binding error: % d/N", wsagetlasterror ());
Wsacleanup ();
Exitprocess (1 );
}

If (Listen (listensocket, 5) = socket_error)
{
// Socket listening failed!
Printf ("Listen error...: % d", wsagetlasterror ());
Wsacleanup ();
Exitprocess (1 );
}

Clientaddrlen = sizeof (clientaddr );
While (true)
{
Printf ("listening begin.../N ");
Newsocket = wsaaccept (listensocket, (sockaddr *) & clientaddr, & clientaddrlen, null, 0); // client_info
If (newsocket = invalid_socket)
{
Printf ("Accept error.../N ");
Continue;
}
// Perhandledata = (lper_handle_data) globalalloc (gptr, sizeof (per_handle_data); // use globalalloc to allocate a block
// Fixed memory, and initially 0
Printf ("received Port Number % d, link with address % s! /N ", ntohs (clientaddr. sin_port ),
Inet_ntoa (clientaddr. sin_addr ));

Perhandledata. Socket = newsocket;
// Perhandledata-> socket = newsocket;
// Memcpy (& perhandledata-> clientaddr, & clientaddr, clientaddrlen );
// Perhandledata is used to save the user's socket Information

Createiocompletionport (handle) newsocket, completionport, (DWORD) & perhandledata, 0 );
// Associate the port with the socket
// Periodata = (lper_io_data) globalalloc (gptr, sizeof (per_io_data ));

Periodata. wsabuffer. Buf = buffer;
Periodata. wsabuffer. Len = sizeof (buffer );
Periodata. operationtype = recv_posted;
Zeromemory (& periodata. overlapped, sizeof (periodata. overlapped ));

If (wsarecv (newsocket, & periodata. wsabuffer, 1, & periodata. recvbyte, & flags, & (periodata. overlapped), null) = socket_error)
{
Printf ("failed to receive data! Error: % d ", wsagetlasterror ());
Continue;
}
}
Printf ("server is closed.../N ");
Closesocket (newsocket );
Wsacleanup ();
}

.....

 

 

 

 

Client:

 

 

# Define no_flags_set 0
# Pragma comment (Lib, "ws2_32.lib ")
# Define Port (u_short) 8000
# Define dest_ip_addr "127.0.0.1" // server address

Int main (void)
{
Wsadata data;
Sockaddr_in destsockaddr;
Socket destsocket;
Unsigned long destaddr;
Int status;
Int numsnt;
Char * tosendtxt = "I am coming ...... haha ";

Status = wsastartup (makeword (1, 1), & data );
If (status! = 0)
Printf ("error: wsastartup unsuccessful/N ");
/* Convert IP address into in_addr form */
Destaddr = inet_addr (dest_ip_addr );
/* Copy destaddr into sockaddr_in structure */
Memcpy (& destsockaddr. sin_addr,
& Destaddr, sizeof (destaddr ));
/* Specify the port portion of the address */
Destsockaddr. sin_port = htons (port );
/* Specify the address family as Internet */
Destsockaddr. sin_family = af_inet;

Destsocket = socket (af_inet, sock_stream, 0 );
If (destsocket = invalid_socket)
{
Printf ("error: Socket unsuccessful/N ");
Status = wsacleanup ();
If (status = socket_error)
Printf ("error: wsacleanup unsuccessful/N ");
Return (1 );
}
Printf ("trying to connect to IP Address: % s", dest_ip_addr );

/* Connect to the server */
Status = connect (destsocket, (lpsockaddr) & destsockaddr, sizeof (destsockaddr ));
If (status = socket_error)
{
Printf ("error: connect unsuccessful/N ");
Status = closesocket (destsocket );
If (status = socket_error)
Printf ("error: closesocket unsuccessful ");
Status = wsacleanup ();
If (status = socket_error)
Printf ("error: wsacleanup unsuccessful ");
Return (1 );
}

Printf ("connected.../N ");
While (1)
{
Printf ("sending.../N ");
Numsnt = Send (destsocket, tosendtxt, strlen (tosendtxt) + 1, no_flags_set );
If (numsnt! = (DWORD) strlen (tosendtxt) + 1)
{
Printf ("Connection terminated/N ");
Status = closesocket (destsocket );
If (status = socket_error)
Printf ("error: closesocket unsuccessful/N ");
Status = wsacleanup ();
If (status = socket_error)
Printf ("error: wsacleanup unsuccessful/N ");
Return (1 );
}

/* Wait before sending the message again */
Sleep (4800 );
}/* While */
}

 

 

When I encountered a socket 10038 invalid socket error, I checked it online for a long time and still failed to solve it. Please give me some advice ....
At the while,
Why is the first time the data is received normal, and the second time it is socket_error?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.