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 *UdpClient::DataThread(void * pParam)
{
    if(null == pParam) return null;

    UdpClient *pThis = (UdpClient*)pParam;

    pThis->mIsDataThreadRunnung = true;
    while(1)
    {
        if(!pThis->mIsConnecting)
            break;
        if(!pThis->mDataList.empty())
        {
            pthread_mutex_lock(&pThis->mMutex);
            string msg = pThis->mDataList.front();
            pThis->mDataList.pop_front();
            pthread_mutex_unlock(&pThis->mMutex);
            if(!pThis->mMsgHdlr) pThis->mMsgHdlr((void *)msg.c_str());
            pThis->OnRecvMessage(msg);
        }
        usleep(100);
    }

    pThis->mIsDataThreadRunnung = false;
    return null;
}
示例#3
0
int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	UdpClient w;
	w.show();
	return a.exec();
}
void MulticastClientUDP()
{
    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.InitializeMulticastSocket(MULTICAST_GROUP, PORT, 1)) 
    {
        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.
            */
        }
    }
}
示例#5
0
int main(int argc, char** argv) {
	const std::string& endpoint = (const std::string&)argv[1];

	pid_t pid = fork();
    if (pid == 0) {
		pid_t pid2 = fork();
	    if (pid2 == 0) {
	    	UdpClient *client = new UdpClient(1);
			client->Execute(endpoint);
	    } else {
	    	TcpClient *client = new TcpClient(2);
	        client->Execute(endpoint);
	    }
    } else {
		pid_t pid3 = fork();
	    if (pid3 == 0) {
        	UdpClient *client = new UdpClient(3);
    		client->Execute(endpoint);
        } else {
        	TcpClient *client = new TcpClient(4);
            client->Execute(endpoint);
        }
    }
	return 0;
}
示例#6
0
文件: main.cpp 项目: GieYan/QT
int main( int argc, char **argv )
{
    QFont font("ZYSong18030",12);
    QApplication::setFont(font);
    	
    QApplication a( argc, argv );
    QTranslator translator(0);
    translator.load("udpclient_zh",".");
    a.installTranslator(&translator);    
    
    UdpClient *udpclient = new UdpClient();
    udpclient->show();
    return a.exec();
}
示例#7
0
void Client::updateClientsView()
{
    if (clientsList != 0)
        freeClientsList();

    clientsList = new char*[udpClients.size() + 1];
    clientsListSize = 0;
    sort(udpClients.begin(), udpClients.end());
    for (UdpClients::const_iterator i = udpClients.begin(); i != udpClients.end(); ++i)
    {
        UdpClient* udpClient = *i;
        std::string str = udpClient->toString();
        const char* clientName = str.c_str();
        clientsList[clientsListSize] = new char[strlen(clientName)+1];
        strcpy(clientsList[clientsListSize++], clientName);
    }
    clientsList[clientsListSize] = 0;
}
示例#8
0
void *UdpClient::RecvThread(void * pParam)
{
    if(null == pParam) return 0;

    UdpClient *pThis = (UdpClient*)pParam;
    char *tmpBuf = new char(pThis->mMaxMsgLen);

    pThis->mIsRecvThreadRunning = true;
    while(1)
    {
        int res = recv(pThis->mSocketFd, tmpBuf, pThis->mMaxMsgLen, 0);
        if(0 < res)
        {
            if(DEBUG) cout << "Receive data : " << tmpBuf << endl;
            pthread_mutex_lock(&pThis->mMutex);
            string tmp = tmpBuf;
            pThis->mDataList.push_back(tmp);
            pthread_mutex_unlock(&pThis->mMutex);
        }
        else if(0 == res)
        {
            cerr << "lost connect" << endl;
            pthread_mutex_lock(&pThis->mMutex);
            pThis->OnDisconnect();
            pThis->mIsConnecting = false;
            pthread_mutex_unlock(&pThis->mMutex);
            break;
        }
        else
        {
            cerr << "receive failed." << endl;
        }

        delete tmpBuf;
    }
    pThis->mIsRecvThreadRunning = false;
    return null;
}
/**
 * @brief Write data to the cloud
 * Note: you have to register components prior.
 * E.g. using "iotkit-admin register <comp_name> <catalogid>" or via iotkit-agent
 */
void writeDataToCloud(string &component, string &data) {
    UdpClient client;
    stringstream ss;

    /* Create a udp connection for iotkit-agent to communicate over */
    if (client.isConnected() == false) {
        if (client.connectUdp(NODE, SERVICE) < 0) {
            cerr << "connection failed" << endl;
            exit(EXIT_FAILURE);
        }
    }

    /*
     * Create the string to send to the cloud
     * eg. {"n":"gpsv1","v":"45.550953, -122.968719"}
     */
    ss << "{\"n\":\"" << component << "\",\"v\":\"" << data << "\"}" << endl;
    cout << ss.str();

    if (client.writeData(ss) < 0)
        cerr << "Cannot write to cloud\n" << endl;

    ss.str("");
}
int main()
{
    // check that we are running on Galileo or Edison
    mraa::Platform platform = mraa::getPlatformType();
    if ((platform != mraa::INTEL_GALILEO_GEN1) &&
            (platform != mraa::INTEL_GALILEO_GEN2) &&
            (platform != mraa::INTEL_EDISON_FAB_C)) {
        std::cerr << "Unsupported platform, exiting" << std::endl;
        return mraa::ERROR_INVALID_PLATFORM;
    }

    // UdpClient class is wrapper for sending UDP data to iotkit-agent
    UdpClient client;
    if (client.connectUdp(NODE, SERVICE) < 0) {
        std::cerr << "Connection to iotkit-agent failed, exiting" << std::endl;
        return mraa::ERROR_UNSPECIFIED;
    }

    // create an analog input object from MRAA using pin A0
    mraa::Aio* a_pin = new mraa::Aio(0);
    if (a_pin == NULL) {
        std::cerr << "Can't create mraa::Aio object, exiting" << std::endl;
        return mraa::ERROR_UNSPECIFIED;
    }

    // loop forever sending the input value every second
    for (;;) {
        uint16_t data = a_pin->read();
        std::stringstream ss;
        ss << "{\"n\":\"" << COMP_NAME << "\",\"v\":" << data << "}" << std::endl;
        client.writeData(ss);
        sleep(1);
    }

    return mraa::SUCCESS;
}
示例#11
0
void Pgw::handle_uplink_udata(Packet pkt, UdpClient &sink_client) {
	pkt.truncate();
	sink_client.set_server(g_sink_ip_addr, g_sink_port);
	sink_client.snd(pkt);
	TRACE(cout << "pgw_handleuplinkudata:" << " uplink udata forwarded to sink: " << pkt.len << endl;)
}