-
Notifications
You must be signed in to change notification settings - Fork 1
/
udp_packet_sender.c
49 lines (46 loc) · 1.28 KB
/
udp_packet_sender.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <ifaddrs.h>
#include <stdint.h>
int main(int argc, char **argv)
{
int sendfd,recvfd;
struct sockaddr_in servaddr,local,cliaddr;
socklen_t cliaddrlen = sizeof(cliaddr);
char sendline[65535],recvline[65535];
if (argc != 2)
{
printf("usage: udp_packet_sender <IPaddress>\n");
return -1;
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5202);
inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
local.sin_family = AF_INET;
local.sin_port = htons(2152);
local.sin_addr.s_addr = htonl(INADDR_ANY);
sendfd = socket(AF_INET, SOCK_DGRAM, 0);
while (fgets(sendline, 65535, stdin) != NULL) {
sendto(sendfd, sendline, strlen(sendline), 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
printf("packet send\n");
recvfd = socket(AF_INET, SOCK_DGRAM, 0);
bind(recvfd,(struct sockaddr*)&local,sizeof(local));
recvfrom(recvfd,recvline,65535,0,(struct sockaddr*)&cliaddr, &cliaddrlen);
}
exit(0);
}