DWORD val = 1; int error = setsockopt( socket, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, (char *)&val, sizeof(DWORD)); SO_UPDATE_CONNECT_CONTEXT This option is used with the ConnectEx, WSAConnectByList, and WSAConnectByName functions. This option updates the properties of the socket after the connection is established. This option should be set if the getpeername, getsockname, getsockopt, setsockopt, or shutdown functions are to be used on the connected socket.
如果是非阻塞方式,需要真正连接成功后调用
int error = setsockopt( socket, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, (char *)&val, sizeof(DWORD)); SO_UPDATE_CONNECT_CONTEXT
This option is used with the ConnectEx, WSAConnectByList, and WSAConnectByName functions. This option updates the properties of the socket after the connection is established. This option should be set if the getpeername, getsockname, getsockopt, setsockopt, or shutdown functions are to be used on the connected socket.