Blame view

src/decoder/sip/udp_server.cpp0 1.59 KB
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  #include <stdio.h>
  #include <stdlib.h>
  #include <sys/socket.h>
  #include <sys/types.h>
  #include <netdb.h>
  #include <string.h>
  #include <errno.h>
  #include <netinet/in.h>
  
  
  #define BUF_SIZE 1024
  
  
  int main(int argc, char *argv[])
  {
      struct addrinfo hint, *result;
      struct sockaddr_in client_addr;
      int res, sfd, client_addr_len;
      char buf[BUF_SIZE];
      
      struct sockaddr_in addr;
      memset(&addr, 0, sizeof(struct sockaddr_in));
  
      unsigned short port = 30026;
  
      addr.sin_family = AF_INET;
      addr.sin_port = htons(port);
      addr.sin_addr.s_addr = INADDR_ANY;
  
      sfd = socket(AF_INET, SOCK_DGRAM, 0);
      if (sfd == -1) 
      {
          perror("socket error!\n");
          exit(1);
      }
      
      res = bind(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
      if (res == -1) 
      {
          perror("bind error!\n");
          exit(1);
      }
      
      while (1) 
      {
          printf("waiting for client ...\n");
          
          res = recv(sfd, buf, BUF_SIZE, 0);
          if (res == -1) 
          {
              perror("recvfrom error\n");
              exit(1);
          }
          printf("revceived data : %s\n", buf);
             
          snprintf(buf, BUF_SIZE, "hello client! Total reveive : %d.", res);
  // //        res = sendto(sfd, buf, strlen(buf), 0, (struct sockaddr*)&client_addr, client_addr_len);
  //         res = send(sfd, buf, strlen(buf), 0);
  //         if (res != strlen(buf) ) 
  //         {
  //             perror("sendto error\n");
  //             exit(1);
  //         }
          
  //         printf("send %d bytes to peer", res);
  
  //         printf("send data < %s > to client\n\n", buf);
      }
  }