This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
zhangyang-libzt/tests/api_test/udpserver6.c

44 lines
978 B
C
Raw Normal View History

2016-10-28 16:45:38 -07:00
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
2016-11-17 16:55:11 -08:00
#include <string.h>
2016-10-28 16:45:38 -07:00
#define MAXBUF 65536
2016-12-05 10:28:02 -08:00
int main(int argc, char *argv[])
2016-10-28 16:45:38 -07:00
{
int sock;
2016-12-05 10:28:02 -08:00
int n;
2016-10-28 16:45:38 -07:00
struct sockaddr_in6 sin6;
2016-11-17 16:55:11 -08:00
socklen_t sin6len;
2016-10-28 16:45:38 -07:00
char buffer[MAXBUF];
sock = socket(PF_INET6, SOCK_DGRAM,0);
sin6len = sizeof(struct sockaddr_in6);
memset(&sin6, 0, sin6len);
2016-12-05 10:28:02 -08:00
sin6.sin6_port = htons(atoi(argv[1]));
2016-10-28 16:45:38 -07:00
sin6.sin6_family = AF_INET6;
sin6.sin6_addr = in6addr_any;
2016-12-05 10:28:02 -08:00
n = bind(sock, (struct sockaddr *)&sin6, sin6len);
if(-1 == n)
2016-10-28 16:45:38 -07:00
perror("bind"), exit(1);
2016-12-05 10:28:02 -08:00
//n = getsockname(sock, (struct sockaddr *)&sin6, &sin6len);
//printf("%d\n", ntohs(sin6.sin6_port));
2016-10-28 16:45:38 -07:00
2016-12-05 10:28:02 -08:00
while (1) {
sleep(1);
n = recvfrom(sock, buffer, MAXBUF, 0, (struct sockaddr *)&sin6, &sin6len);
printf("n = %d, buffer : %s\n", n, buffer);
}
2016-10-28 16:45:38 -07:00
shutdown(sock, 2);
close(sock);
return 0;
}