1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+
4
+ #include <arpa/inet.h>
5
+ #include <sys/socket.h>
6
+ #include <sys/types.h>
7
+ #include <netdb.h>
8
+ #include <unistd.h>
9
+ #include <fcntl.h>
10
+
11
+ void main (){
12
+ int sock_fd = socket (AF_INET , SOCK_DGRAM , IPPROTO_UDP );
13
+ int true_flag = 1 ;
14
+ setsockopt (sock_fd , SOL_SOCKET , SO_REUSEADDR , & true_flag , sizeof (int ));
15
+
16
+ struct sockaddr_in bind_addr ;
17
+ bind_addr .sin_family = AF_INET ;
18
+ bind_addr .sin_port = htons (8080 );
19
+ bind_addr .sin_addr .s_addr = htonl (INADDR_ANY );
20
+ bind (sock_fd , (struct sockaddr * )& bind_addr , sizeof (bind_addr ));
21
+
22
+ struct sockaddr_in client_addr ;
23
+ int client_addr_size ;
24
+
25
+ char * client_ip = (char * )malloc (sizeof (char )* 100 );
26
+ int client_port ;
27
+
28
+ int msg_size = -1 ;
29
+ char * msg_buff = (char * )malloc (sizeof (char )* 100 );
30
+ do {
31
+ msg_size = recvfrom (sock_fd , msg_buff , 100 , MSG_WAITALL , (struct sockaddr * )& client_addr , & client_addr_size );
32
+ client_port = ntohs (client_addr .sin_port );
33
+ inet_ntop (AF_INET , (void * )& client_addr .sin_addr , client_ip , 100 );
34
+ printf ("\nFrom %s:%d" , client_ip , client_port );
35
+ printf ("\n%s" , msg_buff );
36
+ fflush (stdout );
37
+ msg_size = sendto (sock_fd , "CHECK" , 6 , MSG_CONFIRM , (struct sockaddr * )& client_addr , client_addr_size );
38
+ printf ("DONE" );
39
+ fflush (stdout );
40
+ }while (msg_size != 0 );
41
+ }
42
+
43
+ /*
44
+
45
+ sendto(sockfd, (const char *)hello, strlen(hello),
46
+ MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
47
+ len);
48
+ printf("Hello message sent.\n");
49
+
50
+ #define PORT 8080
51
+ #define MAXLINE 1024
52
+
53
+ // Driver code
54
+ int main() {
55
+ int sockfd;
56
+ char buffer[MAXLINE];
57
+ char *hello = "Hello from server";
58
+ struct sockaddr_in servaddr, cliaddr;
59
+
60
+ // Creating socket file descriptor
61
+ if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
62
+ perror("socket creation failed");
63
+ exit(EXIT_FAILURE);
64
+ }
65
+
66
+ memset(&servaddr, 0, sizeof(servaddr));
67
+ memset(&cliaddr, 0, sizeof(cliaddr));
68
+
69
+ // Filling server information
70
+ servaddr.sin_family = AF_INET; // IPv4
71
+ servaddr.sin_addr.s_addr = INADDR_ANY;
72
+ servaddr.sin_port = htons(PORT);
73
+
74
+ // Bind the socket with the server address
75
+ if ( bind(sockfd, (const struct sockaddr *)&servaddr,
76
+ sizeof(servaddr)) < 0 )
77
+ {
78
+ perror("bind failed");
79
+ exit(EXIT_FAILURE);
80
+ }
81
+
82
+ int len, n;
83
+
84
+ len = sizeof(cliaddr); //len is value/resuslt
85
+
86
+ n = recvfrom(sockfd, (char *)buffer, MAXLINE,
87
+ MSG_WAITALL, ( struct sockaddr *) &cliaddr,
88
+ &len);
89
+ buffer[n] = '\0';
90
+ printf("Client : %s\n", buffer);
91
+ sendto(sockfd, (const char *)hello, strlen(hello),
92
+ MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
93
+ len);
94
+ printf("Hello message sent.\n");
95
+
96
+ return 0;
97
+ }
98
+
99
+ */
0 commit comments