Exemplo n.º 1
0
int
main (int argc, char *argv[])
{

  if (argc != 2)
    { // Test for correct number of parameters
      cerr << "Usage: " << argv[0] << " <Server Port>" << endl;
      exit (1);
    }

  unsigned short echoServPort = atoi (argv[1]); // First arg:  local port

  try
    {
      UDPSocket sock (echoServPort);

      char echoBuffer[ECHOMAX];  // Buffer for echo string
      int recvMsgSize;           // Size of received message
      string sourceAddress;      // Address of datagram source
      unsigned short sourcePort; // Port of datagram source
      for (;;)
        { // Run forever
          // Block until receive message from a client
          recvMsgSize
              = sock.recvFrom (echoBuffer, ECHOMAX, sourceAddress, sourcePort);

          cout << "Received packet from " << sourceAddress << ":" << sourcePort
               << endl;

          sock.sendTo (echoBuffer, recvMsgSize, sourceAddress, sourcePort);
        }
    }
  catch (SocketException &e)
    {
      cerr << e.what () << endl;
      exit (1);
    }
  // NOT REACHED

  return 0;
}
Exemplo n.º 2
0
//Na 30 seconden timed the socket out en zal er een socketTimeoutExeption worden aangeroepen.
//Als je dit wilt aanpassen moet je dat veranderen in de prackticalsocket.cpp in de functie recvFRom
int main()
{
    UDPSocket sock;
    //Verander IP
    SocketAddress localadr("172.16.120.134", 8888, SocketAddress::UDP_SOCKET);
    sock.bind(localadr);
    SocketAddress remoteadr;
    cout << "UDP Server is gestart:" << endl;

    int32_t temp[5]= {1,2,3,4,5};
    //Dit bericht wordt verstuurd
    mavlink_message_t msg = encodeLidarMessage(temp, COMMAND_DESTINATION::COMMAND_DESTINATION_ENUM_END, LIDAR_COMMAND_FUNCTIONS::LIDAR_INIT);

    while(true){
            try{
				sock.recvFrom(&msg, sizeof(mavlink_message_t), remoteadr);

				cout << "recieved: " << decodeLidarMessage(msg).Payload[0] << endl;

				//Opnieuw een bericht samen stellen
				for(int i=0; i< 6; i++){
					temp[i] = decodeLidarMessage(msg).Payload[i] + 1;
				}

				msg = encodeLidarMessage( temp, COMMAND_DESTINATION::COMMAND_DESTINATION_ENUM_END, LIDAR_COMMAND_FUNCTIONS::LIDAR_INIT);

				sock.sendTo(&msg, sizeof(mavlink_message_t), remoteadr);

				cout << "sending: " << decodeLidarMessage(msg).Payload[0] << endl;
				cout << "from: " << remoteadr.getAddress() << ":" << remoteadr.getPort() << endl;

				sleep(1);
            }catch(SocketTimedOutException e){
            	continue;
            }
    }
}
Exemplo n.º 3
0
int main(int argc, char **argv)
{
	UDPSocket sock;
	SocketAddress localadr("145.89.98.137", 2222, SocketAddress::UDP_SOCKET);
	sock.bind(localadr);
	SocketAddress remoteadr;
	sock.setTimeOut(2);

	int count = 0;
	while(true){
		try{
			cout << "waiting" << endl;

			sock.recvFrom(&count, sizeof(int), remoteadr);

			cout << "recieved: " << count << endl;
			cout << "from: " << remoteadr.getAddress() << ":" << remoteadr.getPort() << endl;

			count++;
			sock.sendTo(&count, sizeof(int), remoteadr );

		}catch(SocketTimedOutException e){continue;}
	}
}