Coder Social home page Coder Social logo

nexus's People

Contributors

nitaym avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

dakash eyalgab

nexus's Issues

TCP connection establishment error

Hello,

  1. The last push, that added "ifdef"s for win32 support,
    broke compilation for Linux in "Comm/Physical/CClientSocket.cpp".
    In function "TCommErr CClientSocket::Connect()", the "SOCKET_CLEANUP()" should be "SOCKET_CLEANUP" in Linux.
  2. When TCP connection fails to initialize, the Connect function still returns "E_NEXUS_OK", because in case of an error "m_hSocket" is set to 0 and not to a negative number (-1 for ex.)
    // Connect to server. l_iResult = connect(m_hSocket, l_pAddrInfo->ai_addr, (int)l_pAddrInfo->ai_addrlen); if (!SOCKET_TEST_RESULT(l_iResult)) { #ifdef _WIN32 closesocket(m_hSocket); m_hSocket = INVALID_SOCKET; #else close(m_hSocket); m_hSocket = 0; #endif }

suggested fix for the points above:

`int l_iResult;

#ifdef _WIN32
WSADATA l_oWSAData;

// Initialize Winsock
l_iResult = WSAStartup(MAKEWORD(2,2), &l_oWSAData);
if (l_iResult != 0)
{
	dprintf("CSocket::CSocket> WSAStartup failed: %d\n", l_iResult);
	return E_NEXUS_FAIL;
}

#endif

addrinfo *l_pAddrInfo = NULL;
addrinfo l_oHints;

memset(&l_oHints, 0, sizeof(l_oHints) );
l_oHints.ai_family = AF_UNSPEC;
l_oHints.ai_socktype = SOCK_STREAM;
l_oHints.ai_protocol = IPPROTO_TCP;


stringstream ss;
ss << m_wPort;
l_iResult = getaddrinfo(m_sIP.c_str(), ss.str().c_str(), &l_oHints, &l_pAddrInfo);
if (l_iResult != 0)
{
    #ifdef _WIN32
    SOCKET_CLEANUP()
    #else
        SOCKET_CLEANUP
    #endif
	dprintf("CSocket::CSocket> getaddrinfo failed: %d\n", l_iResult);
	return E_NEXUS_FAIL;
}

#ifdef _WIN32
m_hSocket = socket(l_pAddrInfo->ai_family, l_pAddrInfo->ai_socktype, l_pAddrInfo->ai_protocol);
#else
m_hSocket = socket(AF_INET, SOCK_STREAM, 0);
#endif
if (!SOCKET_TEST(m_hSocket))
{
dprintf("CSocket::CSocket> Error at socket(): %ld\n", SOCKET_GETLASTERROR);
#ifdef _WIN32
SOCKET_CLEANUP()
#else
SOCKET_CLEANUP
#endif
freeaddrinfo(l_pAddrInfo);
return E_NEXUS_FAIL;
}

// Connect to server.
l_iResult = connect(m_hSocket, l_pAddrInfo->ai_addr, (int)l_pAddrInfo->ai_addrlen);
if (!SOCKET_TEST_RESULT(l_iResult))
{

#ifdef _WIN32
closesocket(m_hSocket);
m_hSocket = INVALID_SOCKET;
#else
close(m_hSocket);
m_hSocket = -1;
#endif
}

// Should really try the next address returned by getaddrinfo
// if the connect call failed
// But for this simple code we just free the resources
// returned by getaddrinfo and print an error message

freeaddrinfo(l_pAddrInfo);

if (!SOCKET_TEST(m_hSocket))
{
	dprintf("CClientSocket::Connect> Unable to connect to server!\n");
    #ifdef _WIN32
        SOCKET_CLEANUP()
    #else
        SOCKET_CLEANUP
    #endif
	return E_NEXUS_FAIL;
}

m_bIsConnected = true;
return E_NEXUS_OK;`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.