本文整理汇总了C++中Address::GetPort方法的典型用法代码示例。如果您正苦于以下问题:C++ Address::GetPort方法的具体用法?C++ Address::GetPort怎么用?C++ Address::GetPort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Address
的用法示例。
在下文中一共展示了Address::GetPort方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Send
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;
}
示例2:
bool Address::operator < (const Address & other) const
{
if (m_address == other.GetAddress()) {
return m_port < other.GetPort();
}
return m_address < other.GetAddress();
}
示例3: Send
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;
}
示例4: Send
int UdpSocket::Send(const std::string& pMessage, const Address& pAddress)
{
mPacket->data = (Uint8*)pMessage.c_str();
mPacket->len = (int)pMessage.size();
mPacket->address.host = pAddress.GetHost();
mPacket->address.port = pAddress.GetPort();
int code = SDLNet_UDP_Send(mSocket, -1, mPacket);
if(code == 0)
SDL_Log("[UdpSocket::Send] Failed to send packets: %s", SDLNet_GetError());
return code;
}
示例5: Send
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;
}
示例6: test_address
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 );
}
}
示例7: main
int main( int argc, char * argv[] )
{
// initialize socket layer
if ( !InitializeSockets() )
{
printf( "failed to initialize sockets\n" );
return 1;
}
// create socket
int port = 30000;
if ( argc == 2 )
port = atoi( argv[1] );
printf( "creating socket on port %d\n", port );
Socket socket;
if ( !socket.Open( port ) )
{
printf( "failed to create socket!\n" );
return 1;
}
// read in addresses.txt to get the set of addresses we will send packets to
vector<Address> addresses;
string line;
ifstream file( "addresses.txt" );
if ( !file.is_open() )
{
printf( "failed to open 'addresses.txt'\n" );
return 1;
}
while ( !file.eof() )
{
getline( file, line );
int a,b,c,d,port;
if ( sscanf( line.c_str(), "%d.%d.%d.%d:%d", &a, &b, &c, &d, &port ) == 5 )
addresses.push_back( Address( a,b,c,d,port ) );
}
file.close();
// send and receive packets until the user ctrl-breaks...
while ( true )
{
const char data[] = "hello world!";
for ( int i = 0; i < (int) addresses.size(); ++i )
socket.Send( addresses[i], data, sizeof( data ) );
while ( true )
{
Address sender;
unsigned char buffer[256];
int bytes_read = socket.Receive( sender, buffer, sizeof( buffer ) );
if ( !bytes_read )
break;
printf( "received packet from %d.%d.%d.%d:%d (%d bytes)\n", sender.GetA(), sender.GetB(), sender.GetC(), sender.GetD(), sender.GetPort(), bytes_read );
}
wait_seconds( 1.0f );
}
// shutdown socket layer
ShutdownSockets();
return 0;
}