Home > Software engineering >  Windows on the right to send asynchronous socket
Windows on the right to send asynchronous socket

Time:10-10

The send, my understanding is that the data sent to the buffer, the buffer after sent by TCP to ensure data integrity in the past,
Whether standard practice must be like this
 
Int the SendRequest (int socket_fd, const char * send_buffer, long size)
{
Int ret=1;
Int Total=0;
Int lenSend=0;

Struct timeval TV;
TV. Tv_sec=3;
TV. Tv_usec=500;
Fd_set wset;
While (1)
{
FD_ZERO (& amp; Wset);
FD_SET (socket_fd, & amp; Wset);
If (select (socket_fd + 1, NULL, & amp; Wset, NULL, & amp; TV) & gt; 0)//3.5 seconds can send, the socket can be written
{
LenSend=send (socket_fd send_buffer + Total, size - Total, 0).
If (lenSend==1)
{
Ret=SEND_FAIL;
break;
}
Total +=lenSend;
If (Total==size)
{
Ret=OK;
break;
}
}
Socket or else//3.5 seconds can not write, think not
{
Ret=SEND_FAIL;
break;
}
}
return ret;
}

CodePudding user response:

Or for an asynchronous socket standard specification send \ recv operations

CodePudding user response:

http://bbs.csdn.net/topics/390374955

CodePudding user response:

Under normal circumstances, as long as the send did not return an error they will take care of it. If the system data in a buffer cannot be sent to each other and eventually confirmed, will get an error message when using the socket after. Then can reconnect, state to initialize, it doesn't matter that data before sending out.
If must confirm received you send the data to the next step of the operation, can put the buffer on the socket set is empty, so send function will wait for the other party will return after receipt of the data validation package back.

CodePudding user response:

https://git.oschina.net/1050676515/Artemis.git
There is the use of the IOCP, WSASend or WSARecv, continuously updated

CodePudding user response:

The MSDN example
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \
* ioctl. C - TCP server
*
* This is a part of the Microsoft Source Code Samples.
* Copyright 1996-1997 Microsoft Corporation.
* All rights reserved.
* This source code is only intended as a supplement to
* Microsoft Development Tools and/or WinHelp documentation.
* See these sources for detailed information regarding the
* Microsoft samples designed.
A \ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */



# define WIN32_LEAN_AND_MEAN
#include
#include
#include
#include

# define DEFAULT_PORT 5001

Int ReadAndEcho (SOCKET, char *, int);
Int WriteMessage (SOCKET, char *, int);

Void the Usage (char * progname) {
Fprintf (stderr, "Usage \ n % s - e [the endpoint] - [interface] I \ n",
Progname);
Fprintf (stderr, "Where: \ n");
Fprintf (stderr, "\ tendpoint is the port to listen on \ n");
Fprintf (stderr, "\ tinterface is the ipaddr (in dotted decimal notation)");
Fprintf (stderr, "to bind to \ n");
Fprintf (stderr, "Defaults are 5001 and INADDR_ANY \ n");
WSACleanup ();
The exit (1);
}
Int main (int arg c, char * * argv) {

Char Buffer [128].
Char * interface=NULL;
Unsigned short port=DEFAULT_PORT;
Int fromlen;
Int I, ioctl_opt=1;
Struct sockaddr_in local, from;
WSADATA WSADATA;
The SOCKET listen_socket msgsock;
Fd_set readfds, writefds exceptfds;

/* the Parse the arguments */
If (arg c & gt; 1) {
for(i=1; i If ((argv [I] [0]=='-') | | (argv [I] [0]=='/')) {
The switch (tolower (argv [I] [1])) {
In case the 'I' :
Interface=argv [I] + +;
break;
Case 'e' :
Port=atoi (argv [+ +] I);
break;
Default:
The Usage (argv [0]);
break;
}
}
The else
The Usage (argv [0]);
}
}

If (WSAStartup (0 x202, & amp; WsaData)=={SOCKET_ERROR)
Fprintf (stderr, "WSAStartup failed with the error % d \ n", WSAGetLastError ());
WSACleanup ();
return -1;
}

If (port==0) {
The Usage (argv [0]);
}

//
//The fd sets should be zeroed out before using them to prevent errors.
FD_ZERO (& amp; Readfds);
FD_ZERO (& amp; Writefds);
FD_ZERO (& amp; Exceptfds);
Memset (Buffer, 0, sizeof (Buffer));

Local. Sin_family=AF_INET;

//
//to bind to specific interface if desired.

The local sin_addr. S_addr=(! Interface)? INADDR_ANY: inet_addr (interface);

/*
* the Port MUST be a Network Byte Order
*/
The local sin_port=htons (port);

Listen_socket=socket (AF_INET SOCK_STREAM, 0).//TCP socket
If (listen_socket==INVALID_SOCKET) {
Fprintf (stderr, "socket () failed with the error % d \ n", WSAGetLastError ());
WSACleanup ();
return -1;
}
//
//the bind () associates a local address and port combination with the
//socket just created.

If (bind (listen_socket, (struct sockaddr *) & amp; Local, sizeof (local))
=={SOCKET_ERROR)
Fprintf (stderr, "the bind () failed with the error % d \ n", WSAGetLastError ());
WSACleanup ();
return -1;
}

//
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related