include sys types include sys socket include netinet in include stdio

 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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int sock;
struct sockaddr_in addr;
char buf[1024];
int bytes_read;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0)
{
perror("socket");
// exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(7777);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
// exit(2);
}
while(1)
{
bytes_read = recvfrom(sock, buf, 1024, 0, NULL, NULL);
buf[bytes_read] = '\0';
printf(buf);
printf("ujnjdj");
}
close(sock);
return 0;
}