コード例 #1
0
int main(int argc, char **argv) 
{
    Joystick joystick;
    UdpClient client;
    
    joystick.Initialize(Joystick::AnyJoystick);
    char x = 0, y = 0;
    if(client.InitializeSocket(gHostDest, gPort))
    {
        char buffer[512];
        while(true)
        {   
            if(joystick.IsConnected())
            {
                sprintf(buffer, "Joystick - X:%d, Y:%d", (int)joystick.GetAxisPercentage(Joystick::X), (int)joystick.GetAxisPercentage(Joystick::Y));
            }
            else
            {
                sprintf(buffer, "Joystick - X:%d, Y:%d", (int)x++, (int)y--);
            }
            client.Send(buffer, ((unsigned int)strlen(buffer)));
            if(GetChar() == 27)
            {
                break;
            }
            SleepMs(1);
        }
    }
    return 0;
}
コード例 #2
0
void ClientUDP()
{

    UdpClient client;
    IP4Address::List hosts;
    client.GetHostAddresses(hosts);
    cout << "IP Address for Host Machine\n";
    for(int i = 0; i < (int)hosts.size(); i++ ) 
    {
        cout << hosts[i].mString << endl;
    }

    Packet data;
    data.SetByteOrder(CX_PACKET_BIG_ENDIAN);
    if(!client.InitializeSocket(HOSTNAME, PORT)) 
    {
        cout << "Unable to connect to host " << HOSTNAME << endl;
        return;
    }
    cout << "Initialized UDP Client Socket on port " << PORT << endl;
    char key = 0;
    while(key != 27) 
    {
        if((key = GetChar()) > 0) 
        {
            data.Write(key);
            cout << key;
            client.Send(data);  //  Send data (could send character array also if desired
            data.Clear();       //  Clear packet and encode data.
            /*  Alternative way to send:
                client.Send(&key, 1);    //  Send buffer/array.
            */
        }
    }
}