bool SocketImpl::Send(const Address& i_destination, const void* ip_data, size_t i_size) { assert(ip_data); assert(i_size > 0); if (m_socket == 0) return false; assert(i_destination.GetAddress() != 0); assert(i_destination.GetPort() != 0); sockaddr_in address; address.sin_family = AF_INET; address.sin_addr.s_addr = htonl(i_destination.GetAddress()); address.sin_port = htons((unsigned short)i_destination.GetPort()); int sent_bytes = sendto(m_socket, (const char*)ip_data, i_size, 0, (sockaddr*)&address, sizeof(sockaddr_in)); if (-1 == sent_bytes) { int errsv = errno; printf("Errno: %d\n", errsv); } bool all_sent = sent_bytes == i_size; if (all_sent == false) printf("<Network> Sent/size: %x/%x\n", sent_bytes, i_size); return all_sent; }
bool Address::operator < (const Address & other) const { if (m_address == other.GetAddress()) { return m_port < other.GetPort(); } return m_address < other.GetAddress(); }
bool Socket::Send(const Address & destination, const void * data, int size) { sockaddr_in address; address.sin_family = AF_INET; // IPv4 Socket address.sin_addr.s_addr = htonl(destination.GetAddress()); // local address address.sin_port = htons(destination.GetPort()); int sent_bytes = sendto(handle, (const char*)data, size, 0, (sockaddr*)&address, sizeof(sockaddr_in)); printf("Sending to address %d and port = %d\n", destination.GetAddress(), destination.GetPort()); printf("sent bytes: %d\n", sent_bytes); // std::cout << sent_bytes; if (sent_bytes != size) { printf("failed to send packet\n"); return false; } return true; }
bool Socket::Send(const Address & destination, const void * data, int size) { assert( data ); assert( size > 0 ); if (socket == 0) return false; sockaddr_in address; address.sin_family = AF_INET; address.sin_addr.s_addr = htonl(destination.GetAddress()); address.sin_port = htons((unsigned short) destination.GetPort()); int sent_bytes = sendto(socket, (const char*) data, size, 0, (sockaddr*) &address, sizeof(sockaddr_in)); return sent_bytes == size; }
void test_address() { printf( "-----------------------------------------------------\n" ); printf( "test address\n" ); printf( "-----------------------------------------------------\n" ); printf( "defaults\n" ); { Address address; check( address.GetA() == 0 ); check( address.GetB() == 0 ); check( address.GetC() == 0 ); check( address.GetD() == 0 ); check( address.GetPort() == 0 ); check( address.GetAddress() == 0 ); } printf( "a,b,c,d,port\n" ); { const unsigned char a = 100; const unsigned char b = 110; const unsigned char c = 50; const unsigned char d = 12; const unsigned short port = 10000; Address address( a, b, c, d, port ); check( a == address.GetA() ); check( b == address.GetB() ); check( c == address.GetC() ); check( d == address.GetD() ); check( port == address.GetPort() ); } printf( "equality/inequality\n"); { Address x(100,110,0,1,50000); Address y(101,210,6,5,50002); check( x != y ); check( y == y ); check( x == x ); } }
Bool TcpSocketWin32::Connect(const Address & p_Address, const Uint16 p_Port, const Time & p_Timeout, const Uint16 p_EndpointPort) { // Create the socket if ((m_Handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) { bitLogNetErr( "Can not create the socket. Error: " << static_cast<Int32>(GetLastError()) ); return false; } // Bind the socket to a port sockaddr_in service; if (p_EndpointPort != 0) { service.sin_family = AF_INET; service.sin_addr.s_addr = htonl(INADDR_ANY); service.sin_port = htons(static_cast<u_short>(p_EndpointPort)); const int optVal = 1; const int optLen = sizeof(optVal); int rtn = setsockopt(m_Handle, SOL_SOCKET, SO_REUSEADDR, (const char*)&optVal, optLen); if( rtn != 0 ) { bitLogNetErr( "Can not set reusable socket. Error: " << static_cast<Int32>(GetLastError()) ); return false; } // Bind if (bind(m_Handle, reinterpret_cast<const sockaddr *>(&service), sizeof(service)) == SOCKET_ERROR) { bitLogNetErr( "Can not bind the socket. Error: " << static_cast<Int32>(GetLastError()) ); return false; } } // Create an object that's holding the host data service.sin_family = AF_INET; service.sin_addr.s_addr = htonl( static_cast<u_long>( p_Address.GetAddress( ) ) ); service.sin_port = htons( static_cast<u_short>( p_Port ) ); // We are using timeout // Get the blocking status and disable it. Bool blocking = GetBlocking( ); SetBlocking( false ); // Connect if( connect( m_Handle, ( const sockaddr * )&service, sizeof (sockaddr_in ) ) != 0 ) { // Ignore the WSAEWOULDBLOCK error DWORD lastError = GetLastError( ); if( lastError != WSAEWOULDBLOCK ) { Disconnect( ); return false; } } // We failed to connect, but we are waiting for the connection to establish struct timeval tv; if( p_Timeout.AsMicroseconds( ) / 1000000ULL > g_MaxTimeout ) { tv.tv_sec = static_cast<long>( g_MaxTimeout ); tv.tv_usec = static_cast<long>( 0 ); } else { tv.tv_sec = static_cast<long>( p_Timeout.AsMicroseconds( ) / 1000000ULL ); tv.tv_usec = static_cast<long>( p_Timeout.AsMicroseconds( ) % 1000000ULL ); } // Create a FD_SET, and add the m_Handle to the set fd_set fdset; FD_ZERO(&fdset); FD_SET(m_Handle, &fdset); // Select from the set if (select(static_cast<int>(m_Handle)+1, NULL, &fdset, NULL, &tv) > 0) { // Check if the address is valid. Address address = GetPeerAddress( ); if( address == p_Address ) { // The address is not 0, we successfully connected. SetBlocking( blocking ); return true; } } DWORD lastError = GetLastError(); // Failed to connect. Close the socket. Disconnect( ); // Failed. return false; }