int UDPSocket::Listen(unsigned short _port) { CoreAssert(this != NULL); struct sockaddr_in sin; if (m_impl->m_sock != INVALID_SOCKET) return CC_ERR_NOT_SOCKET; memset(&sin, 0, sizeof(sin)); sin.sin_family = PF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(_port); m_impl->m_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); if (m_impl->m_sock == INVALID_SOCKET) return GetError(); SetSocketAttributes(m_impl->m_sock); #if 0 unsigned long arg = 1; #if defined (TARGET_OS_WINDOWS) ioctlsocket(m_impl->m_sock, FIONBIO, &arg); #else ioctl(m_impl->m_sock, FIONBIO, &arg); #endif #endif if (bind(m_impl->m_sock, (sockaddr *)&sin, sizeof(sin)) != 0) { int err = GetError(); /* Close the connection, it failed. */ #ifdef TARGET_OS_WINDOWS closesocket(m_impl->m_sock); #else close(m_impl->m_sock); #endif return err; } return CC_ERR_NONE; }
int TCPSocket::Accept(TCPSocket **_socket) { CoreAssert(this != NULL); /* We're forced to accept any incoming connection, unfortunately. */ socket_t sock = accept(m_impl->m_sock, 0, 0); /* Did we truly accept a connection? */ if (sock != INVALID_SOCKET) { /* Set up the typical transmission attributes. */ SetSocketAttributes(sock); #if 0 unsigned long arg = 1; /* Non-blocking I/O, if possible. Ignore any errors. */ #if defined (TARGET_OS_WINDOWS) ioctlsocket(m_impl->m_sock, FIONBIO, &arg); #else ioctl(m_impl->m_sock, FIONBIO, &arg); #endif #endif /* Create a new wrapper for our socket. */ TCPSocket *csock = new TCPSocket(); csock->m_impl->m_sock = sock; m_state = SOCKET_STATE_CONNECTED; /* We're done. */ *_socket = csock; return CC_ERR_NONE; } /* Nothing accepted. */ *_socket = NULL; return CC_ERR_NO_SOCK; }
int UDPSocket::Bind(const char *_address, unsigned short _port) { CoreAssert(this != NULL); struct sockaddr_in sin; struct hostent *host; if (m_impl->m_sock != INVALID_SOCKET) return CC_ERR_NOT_SOCKET; m_impl->m_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); if (m_impl->m_sock == INVALID_SOCKET) return GetError(); SetSocketAttributes(m_impl->m_sock); host = gethostbyname((char *)_address); if (!host) return GetError(); memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = (( struct in_addr * )(host->h_addr))->s_addr; sin.sin_port = htons(_port); if (connect(m_impl->m_sock, (( struct sockaddr * )&sin), sizeof(sin)) != 0) { int err = GetError(); /* Close the connection, it failed. */ #ifdef TARGET_OS_WINDOWS closesocket(m_impl->m_sock); #else close(m_impl->m_sock); #endif return err; } return CC_ERR_NONE; }
int TCPSocket::Listen(unsigned short _port) { CoreAssert(this != NULL); struct sockaddr_in sin; /* Verify our socket isn't in use. */ if (m_impl->m_sock != INVALID_SOCKET) return CC_ERR_INVALID_CALL; /* Set up our sockaddr_in */ memset(&sin, 0, sizeof(sin)); sin.sin_family = PF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(_port); /* Open a new TCP/IP socket. */ m_impl->m_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); /* Verify the socket. */ if (m_impl->m_sock == INVALID_SOCKET) return GetError(); /* Set up the typical transmission attributes. */ SetSocketAttributes(m_impl->m_sock); #if 0 unsigned long arg = 1; /* Non-blocking I/O, if possible. Ignore any errors. */ #if defined (TARGET_OS_WINDOWS) ioctlsocket(m_impl->m_sock, FIONBIO, &arg); #else ioctl(m_impl->m_sock, FIONBIO, &arg); #endif #endif /* Bind our socket to our given port number. */ if (bind(m_impl->m_sock, (sockaddr *)&sin, sizeof(sin)) != 0) { /* Bind failure, for some reason. */ int err = GetError(); #ifdef TARGET_OS_WINDOWS closesocket(m_impl->m_sock); #elif defined(TARGET_OS_HAIKU) ::shutdown(m_impl->m_sock, SHUT_RDWR); #else close(m_impl->m_sock); #endif return err; } /* Listen on the given port, with a maximum of 10 half-open connections. */ if (listen(m_impl->m_sock, 10) == SOCKET_ERROR) { /* Listen failure, for some reason. */ #ifdef TARGET_OS_WINDOWS closesocket(m_impl->m_sock); #elif defined(TARGET_OS_HAIKU) ::shutdown(m_impl->m_sock, SHUT_RDWR); #else close(m_impl->m_sock); #endif return GetError(); } m_state = SOCKET_STATE_LISTENING; return CC_ERR_NONE; }