serv.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "include/tcp.h"
  2. #include "include/udp.h"
  3. //#include "rtc.h"
  4. #include <unistd.h>
  5. #include <pthread.h>
  6. #include <string.h>
  7. #if 1
  8. #define SELF_PORT_UDP 5002
  9. #define SELF_PORT_TCP 5003
  10. #define SELF_IP "192.168.188.136" //自己IP
  11. #define OTHER_PORT_UDP 5004
  12. #define OTHER_PORT_TCP 5005
  13. #define OTHER_IP "192.168.188.136" //他人IP
  14. #define BROADCAST_PORT_UDP 5200
  15. #define BROADCAST_RECV_IP INADDR_ANY //广播IP
  16. #else
  17. #define OTHER_PORT_UDP 5002
  18. #define OTHER_PORT_TCP 5003
  19. #define OTHER_IP "192.168.188.136" //自己IP
  20. #define SELF_PORT_UDP 5004
  21. #define SELF_PORT_TCP 5005
  22. #define SELF_IP "192.168.188.136" //他人IP
  23. #define BROADCAST_PORT_UDP 5200
  24. #define BROADCAST_SEND_IP INADDR_BROADCAST //广播IP
  25. #endif
  26. void *udp_recv(void *arg)
  27. {
  28. struct sockaddr_in recv_cin;
  29. struct msg recv_str[BUFSIZ];
  30. udp_broad_recv_init(BROADCAST_RECV_IP, BROADCAST_PORT_UDP);
  31. while (1)
  32. {
  33. udp_broad_recv_from(&recv_cin, recv_str);
  34. printf("2.%s\n", (char *)recv_str);
  35. }
  36. pthread_exit(NULL);
  37. }
  38. void *tcp_send(void *arg)
  39. {
  40. static char *send_str = "hello world";
  41. // open_rtc();
  42. while (1)
  43. {
  44. sleep(1);
  45. // send_str =read_rtc();
  46. // tcp_send_to(OTHER_IP, OTHER_PORT_TCP, (char *)send_str, strlen(send_str)+1);
  47. }
  48. pthread_exit(NULL);
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. pthread_t tid1;
  53. pthread_t tid2;
  54. pthread_create(&tid1, NULL, udp_recv, NULL);
  55. pthread_create(&tid2, NULL, tcp_send, NULL);
  56. pthread_join(tid1, NULL);
  57. pthread_join(tid2, NULL);
  58. return 0;
  59. }