Changeset 57 in cheroxy


Ignore:
Timestamp:
12/17/12 18:01:59 (7 years ago)
Author:
cheese
Message:

#1 add reuseaddr option to server socket and linger option to client socket

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/CRXSocket.h

    r38 r57  
    3434#define ERROR_TCPSOCKET_FAILED_TO_RECEIVE       ERROR_TCPSOCKET - 10
    3535#define ERROR_TCPSOCKET_NOT_READY               ERROR_TCPSOCKET - 11
    36 #define ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT    ERROR_TCPSOCKET - 12
     36#define ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT   ERROR_TCPSOCKET - 12
     37#define ERROR_TCPSOCKET_FAILED_TO_SET_TIMEOUT   ERROR_TCPSOCKET - 13
    3738
    3839class CRXSocket : public CRXException
  • trunk/src/CRXSocket.cpp

    r38 r57  
    103103#ifdef _WIN32
    104104                          (char *) &aTimeMilliSec,
    105                           sizeof (aTimeMilliSec));
    106105#else
    107106                          &aTimeVal,
     107#endif
    108108                          (socklen_t) sizeof (aTimeVal));
    109 #endif
    110     if (aResult < 0)
    111     {
    112         aResult = ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT;
     109    if (aResult < 0)
     110    {
     111        aResult = ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT;
    113112        CRX_ERROR_SET (aResult, "Failed to set socket option.");
    114113    }
     
    212211    int             aResult = -1;
    213212    struct hostent  * aHostEnt;
     213    struct linger   aLinger;
    214214
    215215    /*----------------------------------------------------------------*/
     
    239239    }
    240240
     241    aLinger.l_onoff = 1;
     242    aLinger.l_linger = 0;
     243    aResult = setsockopt (mSocket,
     244                          SOL_SOCKET,
     245                          SO_LINGER,
     246#ifdef _WIN32
     247                          (char *) &aLinger,
     248#else
     249                          &aLinger,
     250#endif
     251                          (socklen_t) sizeof (aLinger));
     252    if (aResult < 0)
     253    {
     254        aResult = ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT;
     255        CRX_ERROR_SET (aResult, "Failed to set socket option (linger).");
     256    }
     257
    241258    aResult = SetTimeout (aTimeout);
    242259    if (aResult < 0)
    243260    {
    244         aResult = ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT;
     261        aResult = ERROR_TCPSOCKET_FAILED_TO_SET_TIMEOUT;
    245262        CRX_ERROR_SET (aResult, "Failed to set timeout (%d).", GetSystemErrorCode ());
    246263        return aResult;
     
    265282    int aResult = -1;
    266283
     284    int aReuseAddress = 1;
     285
    267286    /*----------------------------------------------------------------*/
    268287    aResult = CreateSocket ();
     
    272291        CRX_ERROR_SET (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
    273292        return aResult;
     293    }
     294
     295    aResult = setsockopt (mSocket,
     296                          SOL_SOCKET,
     297                          SO_REUSEADDR,
     298#ifdef _WIN32
     299                          (char *) &aReuseAddress,
     300#else
     301                          &aReuseAddress,
     302#endif
     303                          (socklen_t) sizeof (aReuseAddress));
     304    if (aResult < 0)
     305    {
     306        aResult = ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT;
     307        CRX_ERROR_SET (aResult, "Failed to set socket option (reuseaddr).");
    274308    }
    275309
Note: See TracChangeset for help on using the changeset viewer.