tlm.cpp 738 B

12345678910111213141516171819202122232425262728293031323334
  1. #include "tlm.h"
  2. int tlm_start(){
  3. return udp_start(CLIENT_PORT);
  4. }
  5. int external_start(){
  6. return udp_start(EXT_CMD_PORT);
  7. }
  8. int udp_start(int port){
  9. int sock;
  10. struct sockaddr_in addr;
  11. sock = socket(AF_INET, SOCK_DGRAM, 0);
  12. assert(sock != -1);
  13. addr.sin_family = AF_INET;
  14. addr.sin_port = htons(port);
  15. addr.sin_addr.s_addr = htonl(CLIENT_IP);
  16. assert(fcntl(sock, F_SETFL, O_NONBLOCK, 1) != -1);
  17. int sock_buf_size = 256;
  18. assert(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &sock_buf_size, sizeof(int)) != -1);
  19. assert(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) != -1);
  20. return sock;
  21. }
  22. int recv_external_cmd(int sock, char* payload){
  23. return recv(sock, payload, sizeof(char), 0);
  24. }