Socket send and receive timeout

Source: Internet
Author: User
Tags htons

int Nnettimeout= +;//1 seconds
//Delivery time limit
setsockopt (Socket,sol_s0cket,so_sndtimeo, (Char *)&Nnettimeout,sizeof(int));
//Receiving time limit
setsockopt (Socket,sol_s0cket,so_rcvtimeo, (Char *)&Nnettimeout,sizeof(int));

1.closesocket (typically does not close immediately and undergoes the time_wait process) to continue to reuse the socket:
BOOL breuseaddr=true;
SetSockOpt (S,sol_socket, SO_REUSEADDR, (const char*) &breuseaddr,sizeof (BOOL));
2. If you want a soket that is already in the connected state to be forced to close after calling Closesocket, do not experience
The process of time_wait:
BOOL Bdontlinger = FALSE;
SetSockOpt (S,sol_socket,so_dontlinger, (const char*) &bdontlinger,sizeof (BOOL));

Waiting for time_wait to end may be a worries fire, especially if you are developing a socket service, you need to stop the server to do some changes and then re-start. Fortunately, there are ways to avoid time_wait. You can use the SO_REUSEADDR socket option for sockets so that the ports can be reused on a horse.

[Code]
int sock, ret, on;struct sockaddr_in servaddr;



Sock = socket (af_inet, Sock_stream, 0):




on = 1;

ret = setsockopt (sock, Sol_socket, so_reuseaddr, &on, sizeof (on));



memset (&servaddr, 0, sizeof (SERVADDR));

servaddr.sin_family = af_inet;

SERVADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);


Servaddr.sin_port = htons (8080);




ret = bind (sock, (struct sockaddr *) &servaddr, sizeof (SERVADDR));

[/code]
3. In the Send (), recv () process sometimes due to network conditions and other reasons, the collection can not be expected to proceed, and set the time and delivery period:
int NNETTIMEOUT=1000;//1 sec
Delivery time limit
SetSockOpt (Socket,sol_s0cket,so_sndtimeo, (char *) &nnettimeout,sizeof (int));
Receiving time limit
SetSockOpt (Socket,sol_s0cket,so_rcvtimeo, (char *) &nnettimeout,sizeof (int));
4. In Send (), the actual bytes sent (synchronous) or bytes sent to the socket buffer are returned.
(asynchronous); The system default state send and Receive is 8688 bytes (approximately 8.5K), and data is sent in the actual process.
and receive a large amount of data, you can set the socket buffer, and avoid the Send (), recv () Continuous loop transceiver:
Receive buffers
int nrecvbuf=32*1024;//set to 32K
SetSockOpt (S,sol_socket,so_rcvbuf, (const char*) &nrecvbuf,sizeof (int));
Send buffer
int nsendbuf=32*1024;//set to 32K
SetSockOpt (S,sol_socket,so_sndbuf, (const char*) &nsendbuf,sizeof (int));
5. If you do not want to experience a copy of the system buffer to the socket buffer when sending the data
Performance of the program:
int nzero=0;
SetSockOpt (Socket,sol_s0cket,so_sndbuf, (char *) &nzero,sizeof (Nzero));
6. Complete the above function (by default, copy the contents of the socket buffer to the system buffer) in recv ():
int nzero=0;
SetSockOpt (Socket,sol_s0cket,so_rcvbuf, (char *) &nzero,sizeof (int));
7. In general, when sending a UDP datagram, you want the data sent by the socket to have broadcast characteristics:
BOOL bbroadcast=true;
SetSockOpt (S,sol_socket,so_broadcast, (const char*) &bbroadcast,sizeof (BOOL));
8. In the Client connection server process, if the socket in non-blocking mode is in the process of connect (), you can
To set the Connect () delay until Accpet () is called (this function is set only in the non-blocking process with significant
function, which has little effect in blocking functions)
BOOL bconditionalaccept=true;
SetSockOpt (s,sol_socket,so_conditional_accept, (const char*) &bconditionalaccept,sizeof (BOOL));
9. If in the process of sending the data (send () is not completed, and the data is not sent), Closesocket () was called, before we
The general measures taken are "calmly shut down" shutdown (s,sd_both), but the data is definitely lost and how to set the program to meet specific
Application requirements (that is, to let the data not sent out after the socket is closed)?
struct Linger {
U_short L_onoff;
U_short L_linger;
};
Linger M_slinger;
m_slinger.l_onoff=1;//(in the closesocket () call, but there is no data sent when the time allowed to stay)
If m_slinger.l_onoff=0, then function and 2.) function the same;
m_slinger.l_linger=5;//(allow 5 seconds to stay)
SetSockOpt (S,sol_socket,so_linger, (const char*) &m_slinger,sizeof (LINGER));

Setting the connection timeout in the socket: Enables applications that want to avoid being locked out during a socket invocation, taking an orderly approach while managing multiple sockets. Before using this function, you need to set the socket to non-locking mode, so that when connect, it will skip immediately, but also usually produces a wsaewouldblock error, this error does not matter. Performing a Select again is a true timeout.
Wsadata WSD;
SOCKET cclient;
int ret;
struct SOCKADDR_IN server;
Hostent *host=null;
if (WSAStartup (Makeword (2,0), &WSD)) {return 0;}
Cclient=socket (AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (cclient==invalid_socket) {return 0;}
Set RECV and Send time Out
int timeout=6000; Set Send timeout 6 seconds
if (:: SetSockOpt (Cclient,sol_socket,so_sndtimeo, (char *) &timeout,sizeof (TimeOut)) ==socket_error) {
return 0;}
timeout=6000;//set receive timeout 6 seconds
if (:: SetSockOpt (Cclient,sol_socket,so_rcvtimeo, (char *) &timeout,sizeof (TimeOut)) ==socket_error) {
return 0;
}
To set up a non-blocking mode connection
unsigned long ul = 1;
ret = ioctlsocket (Cclient, Fionbio, (unsigned long*) &ul);
if (Ret==socket_error) return 0;
Connection
server.sin_family = af_inet;
Server.sin_port = htons (25);
server.sin_addr. S_addr = inet_addr ((LPCSTR) psmtp);
if (server.sin_addr.s_addr = = inaddr_none) {return 0;}
Connect (cclient, (const struct SOCKADDR *) &server,sizeof (server));
Select model, that is, set timeout
struct Timeval timeout;
Fd_set R;
Fd_zero (&R);
Fd_set (Cclient, &r);
Timeout.tv_sec = 15; Connection Timeout 15 seconds
timeout.tv_usec = 0;
ret = Select (0, 0, &r, 0, &timeout);
if (ret <= 0)
{
:: Closesocket (Cclient);
return 0;
}
General non-locking mode socket is difficult to control, can be considered according to the actual situation to set back to block mode
unsigned long ul1= 0;
ret = ioctlsocket (Cclient, Fionbio, (unsigned long*) &ul1);
if (ret==socket_error) {
:: Closesocket (Cclient);
return 0;
As mentioned above, how the Recv function returns 0 is a communication failure. You can use the Shutdown function to confirm in this case.
......
else if (Rcved_len = = 0)
{
int ilasterr = 0;
Ishutdown = Shutdown (*PFD, 0);
/*begin OS dependent*/
#ifdef _WIN32
Ilasterr = WSAGetLastError ();
if (Ishutdown = = Socket_error
&& (Ilasterr = = Wsaenetdown | | Ilasterr = = Wsaenotconn))
{
errno = Ilasterr + Errno_max;
#else
if (Ishutdown = =-1
&& errno = = enotconn)
{
#endif
/*end OS dependent*/
/* Communication failed */
Perrmsg->ucerrmsg = pmonthre_err_151_comm_failed;
}
Else
{
Perrmsg->ucerrmsg = pmonthre_err_900_systemcall_failed;
}

strcpy (Perrmsg->szfuncname, "recv");
Perrmsg->ierrno = errno;
Perrmsg->idetailcode = _id_pmonthremsgrecv | 0x00000006

Goto L_error;
}
...... }

Socket send and receive timeout

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.