udp-connect-server.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. g++ udp-connect-server.cpp ../../objs/st/libst.a -g -O0 -o udp-connect-server &&
  3. ./udp-connect-server 127.0.0.1 8000 2
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include "../../objs/st/st.h"
  9. #include <sys/socket.h>
  10. #include <arpa/inet.h>
  11. #include <vector>
  12. using namespace std;
  13. vector<int> lfds;
  14. void* pfn(void* arg) {
  15. sockaddr_in addr = *(sockaddr_in*)arg;
  16. char* ip = inet_ntoa(addr.sin_addr);
  17. int port = ntohs(addr.sin_port);
  18. int fd = ::socket(AF_INET, SOCK_DGRAM, 0);
  19. assert(fd > 0);
  20. lfds.push_back(fd);
  21. // Find another thread to recv packet.
  22. int migrate_fd = 0;
  23. while (!migrate_fd) {
  24. for (int i = 0; i < (int)lfds.size(); i++) {
  25. if (lfds.at(i) != fd) {
  26. migrate_fd = lfds.at(i);
  27. break;
  28. }
  29. }
  30. st_usleep(10 * 1000);
  31. }
  32. if (true) {
  33. int v = 1;
  34. int r0 = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(int));
  35. assert(r0 != -1);
  36. }
  37. if (true) {
  38. int v = 1;
  39. int r0 = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(int));
  40. assert(r0 != -1);
  41. }
  42. if (true) {
  43. int r0 = bind(fd, (sockaddr*)&addr, sizeof(sockaddr_in));
  44. assert(!r0);
  45. }
  46. st_netfd_t stfd = st_netfd_open_socket(fd);
  47. printf("listen at %s:%d, fd=%d, migrate_fd=%d ok\n", ip, port, fd, migrate_fd);
  48. while (true) {
  49. char buf[1600] = {0};
  50. sockaddr_in from;
  51. int nb_from = sizeof(from);
  52. int r0 = st_recvfrom(stfd, buf, sizeof(buf), (sockaddr*)&from, &nb_from, ST_UTIME_NO_TIMEOUT);
  53. printf("fd #%d, peer %s:%d, got %dB, %s\n", fd, inet_ntoa(from.sin_addr), ntohs(from.sin_port), r0, buf);
  54. if (r0 <= 0) {
  55. break;
  56. }
  57. // When we got the packet, connect to another thread.
  58. // If it is of type SOCK_DGRAM, this call specifies the peer with which the socket
  59. // is to be associated; this address is that to which datagrams are to be sent, and
  60. // the only address from which datagrams are to be received.
  61. errno = 0;
  62. r0 = connect(migrate_fd, (const sockaddr*)&from, (socklen_t)nb_from);
  63. printf("Transfer %s:%d from #%d to #%d, r0=%d, errno=%d\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port),
  64. fd, migrate_fd, r0, errno);
  65. st_usleep(10 * 1000);
  66. }
  67. st_netfd_close(stfd);
  68. return NULL;
  69. }
  70. int main(int argc, char** argv) {
  71. if (argc < 4) {
  72. printf("Usage: %s ip port workers\n", argv[0]);
  73. exit(-1);
  74. }
  75. st_init();
  76. const char* ip = argv[1];
  77. int port = ::atoi(argv[2]);
  78. int workers = ::atoi(argv[3]);
  79. printf("Start %d workers, at %s:%d\n", workers, ip, port);
  80. sockaddr_in addr;
  81. addr.sin_family = AF_INET;
  82. addr.sin_addr.s_addr = inet_addr(ip);
  83. addr.sin_port = htons(port);
  84. for (int i = 0; i < workers; i++) {
  85. st_thread_t thread = st_thread_create(pfn, &addr, 1, 0);
  86. assert(thread);
  87. }
  88. st_thread_exit(NULL);
  89. return 0;
  90. }