Changeset 48 in libcf for trunk/src/cf_socket.c


Ignore:
Timestamp:
03/29/13 16:04:12 (11 years ago)
Author:
cheese
Message:

#1 add socket option (reuseaddr and linger) and fix test code bug

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/cf_socket.c

    r47 r48  
    7272
    7373static int
     74CF_Socket_Local_SetLinger (const int sock)
     75{
     76    struct linger linger;
     77
     78    linger.l_onoff = 1;
     79    linger.l_linger = 0;
     80
     81    return CF_Socket_SetOption (sock,
     82                                SO_LINGER,
     83                                &linger,
     84                                sizeof (linger));
     85}
     86
     87static int
     88CF_Socket_Local_SetReUseAddr (const int sock)
     89{
     90    int reuseaddr = 1;
     91
     92    return CF_Socket_SetOption (sock,
     93                                SO_REUSEADDR,
     94                                &reuseaddr,
     95                                sizeof (reuseaddr));
     96}
     97
     98static int
    7499CF_Socket_Local_CheckTimeout (const int sock,
    75100                              const int timeout)
     
    281306    struct sockaddr_in  address;
    282307    struct hostent      * hostEnt;
    283     struct linger       linger;
    284308
    285309    int                 retval = 0;
     
    300324    address.sin_port        = htons (port);
    301325    address.sin_addr.s_addr = inet_addr (ip);
    302 
    303     linger.l_onoff = 1;
    304     linger.l_linger = 0;
    305326
    306327    TRY
     
    323344
    324345        /* 4. set options */
    325         result = CF_Socket_SetOption (sock, SO_LINGER, &linger, sizeof (linger));
    326         if (result < 0)
     346        if (CF_Socket_Local_SetLinger (sock)    < 0 ||
     347            CF_Socket_Local_SetReUseAddr (sock) < 0 )
    327348        {
    328349            result = CF_ERROR_SOCKET_SET_OPTION;
     
    425446    TRY
    426447    {
     448        if (CF_Socket_Local_SetLinger (sock)    < 0 ||
     449            CF_Socket_Local_SetReUseAddr (sock) < 0 )
     450        {
     451            result = CF_ERROR_SOCKET_SET_OPTION;
     452            TRY_BREAK;
     453        }
     454
    427455        result = bind (sock, (struct sockaddr *) &address, sizeof (struct sockaddr));
    428456        if (result < 0)
Note: See TracChangeset for help on using the changeset viewer.