-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathudpServer.c
33 lines (27 loc) · 800 Bytes
/
udpServer.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void main(int argc, char **argv){
if(argc != 2){
printf("Usage: %s <port>\n", argv[0]);
exit(0);
}
int port = atoi(argv[1]);
int sockfd;
struct sockaddr_in si_me, si_other;
char buffer[1024];
socklen_t addr_size;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&si_me, '\0', sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(port);
si_me.sin_addr.s_addr = inet_addr("127.0.0.1");
bind(sockfd, (struct sockaddr*)&si_me, sizeof(si_me));
addr_size = sizeof(si_other);
recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)& si_other, &addr_size);
printf("[+]Data Received: %s", buffer);
}