C   57
tcp6recv c
Guest on 4th February 2023 01:19:59 PM


  1.  
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h>
  6. #include <stdio.h>
  7. #include <signal.h>
  8. #include <sys/time.h>
  9. #include <fcntl.h>
  10. #include <sys/times.h>
  11. #include <stdlib.h>
  12. #include <memory.h>
  13.  
  14.  
  15. /* IPv6 AF_INET6 sockets:
  16.  
  17. struct sockaddr_in6 {
  18.     u_int16_t       sin6_family;   // address family, AF_INET6
  19.     u_int16_t       sin6_port;     // port number, Network Byte Order
  20.     u_int32_t       sin6_flowinfo; // IPv6 flow information
  21.     struct in6_addr sin6_addr;     // IPv6 address
  22.     u_int32_t       sin6_scope_id; // Scope ID
  23. };
  24.  
  25. struct in6_addr {
  26.     unsigned char   s6_addr[16];   // load with inet_pton()
  27. };
  28.  
  29. */
  30.  
  31. struct sockaddr_in6 saddr =
  32. {
  33.    sin6_family:  AF_INET6,
  34. };
  35.  
  36. struct sockaddr_in6 raddr =
  37. {
  38.    sin6_family:  AF_INET6,
  39. };
  40.  
  41. struct in6_addr myaddr =
  42. {
  43. //  fe80:0000:0000:0000:0000:0214:4fff:fe80
  44.    {0xfe ,0x80 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  45.     0x02 ,0x14 ,0x4f ,0xff ,0xfe ,0x80, 0xdb, 0x46},
  46. };
  47.  
  48. struct in6_addr wcaddr =  IN6ADDR_ANY_INIT;
  49.  
  50.  
  51. char buf[256];
  52.  
  53. int main(
  54. int argc,
  55. char **argv)
  56. {
  57.    int s;
  58.    int m;
  59.    int rc;
  60.    int addrlen = sizeof(raddr);
  61.  
  62.    saddr.sin6_port = htons(3334);
  63.    saddr.sin6_flowinfo = htonl(1);
  64. // bcopy(&wcaddr, &saddr.sin6_addr, 16);
  65.  
  66.  
  67. // inet_pton(AF_INET6, "fe80::214:4fff:fe80:db46" , &saddr.sin6_addr);
  68.    inet_pton(AF_INET6, "fec0::20c:29ff:fecf:5d69" , &saddr.sin6_addr);
  69. // inet_pton(AF_INET6, "::1" , &saddr.sin6_addr);
  70. // inet_pton(AF_INET6, "::0" , &saddr.sin6_addr);
  71.  
  72.    s = socket(PF_INET6, SOCK_STREAM, 0);
  73.    if (s < 0)
  74.    {
  75.       perror("socket");
  76.       exit(1);
  77.    }
  78.  
  79.    rc = bind(s, (struct sockaddr *)&saddr,  sizeof(saddr));
  80.    if (rc < 0)
  81.    {
  82.       perror("bind");
  83.       exit(1);
  84.    }
  85.  
  86.    rc = listen(s, 4);
  87.    if (rc < 0)
  88.    {
  89.       perror("listen");
  90.       exit(1);
  91.    }
  92.  
  93.    m  = accept(s, (struct sockaddr *)&raddr,  &addrlen);
  94.    if (m < 0)
  95.    {
  96.       perror("accept");
  97.       exit(1);
  98.    }
  99.  
  100.  
  101.    rc = read(m, buf, sizeof(buf));
  102.    while (rc > 0)
  103.    {
  104.       write(1, buf, rc);
  105.       rc = read(m, buf, sizeof(buf));
  106.    }
  107.  
  108.    if (rc < 0)
  109.    {
  110.       perror("read");
  111.       exit(1);
  112.    }
  113.  
  114. }

Raw Paste

Login or Register to edit or fork this paste. It's free.