-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.c
More file actions
41 lines (33 loc) · 943 Bytes
/
client.c
File metadata and controls
41 lines (33 loc) · 943 Bytes
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
#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>
int main(int argc, char **argv){
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
exit(0);
}
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
int sockfd;
struct sockaddr_in addr;
char buffer[1024];
socklen_t addr_size;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
bzero(buffer, 1024);
strcpy(buffer, "Hello, World!");
sendto(sockfd, buffer, 1024, 0, (struct sockaddr*)&addr, sizeof(addr));
printf("[+]Data send: %s\n", buffer);
bzero(buffer, 1024);
addr_size = sizeof(addr);
recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)&addr, &addr_size);
printf("[+]Data recv: %s\n", buffer);
return 0;
}