dev/c/echo_server-udp/echo-server.c

44 lines
848 B
C

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
int main()
{
int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in sa;
int bound;
ssize_t recsize;
socklen_t *address_len = NULL;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons(7654);
bound = bind(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr));
if (bound < 0)
fprintf(stderr, "bind(): ошибка %s\n", strerror(errno));
char Hz[1024];
while (1) {
printf("recv test....\n");
recsize = recvfrom(sock, (void *)Hz, 100, 0, (struct sockaddr *)&sa, address_len);
if (recsize < 0)
perror("Ошибка:");
printf("recsize: %d\n ", (int)recsize);
//sleep(1);
printf("datagram: %s\n", Hz);
}
}