-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.c
121 lines (100 loc) · 2.62 KB
/
client.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* @author Ammar Faizi <[email protected]>
* @license MIT
*
* Simple socket server.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "data_structure.h"
int client_socket(char *server_ip, uint16_t server_port);
int main(int argc, char *argv[])
{
if (argc != 3) {
printf("Invalid argument!\n");
printf("Usage: %s <bind_ip> <bind_port>\n", argv[0]);
return 1;
}
return client_socket(argv[1], atoi(argv[2]));
}
int client_socket(char *server_ip, uint16_t server_port)
{
ssize_t rwbytes;
int net_fd;
char client_ip[32], data_arena[65538]; /* Why should we use heap if we can use stack? */
packet *pkt = (packet *)&data_arena;
struct sockaddr_in server_addr;
socklen_t rlen = sizeof(struct sockaddr_in);
/**
* Prepare server address data.
*/
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
server_addr.sin_addr.s_addr = inet_addr(server_ip);
/**
* Create TCP socket.
*/
if ((net_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation failed");
return 1; /* Don't need to close socket fd since it fails to create. */
}
/**
* Conncet to TeaVPN server.
*/
printf("Connecting to 127.0.0.1:8000...\n");
if (connect(net_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in)) < 0) {
perror("Error on connect");
goto close_socket;
}
printf("Connection established!\n");
printf("Enter your data: ");
fgets(pkt->data, 1024, stdin);
pkt->len = strlen(pkt->data) - 1;
pkt->data[pkt->len] = '\0';
{
size_t wtotal = 0;
/* Send data length */
rwbytes = send(net_fd, &(pkt->len), sizeof(uint16_t), 0);
if (rwbytes < 0) {
perror("Write error");
goto close_socket;
}
wtotal += rwbytes;
/* Send data */
rwbytes = send(net_fd, pkt->data, pkt->len, 0);
if (rwbytes < 0) {
perror("Write error");
goto close_socket;
}
wtotal += rwbytes;
printf("%ld bytes sent!\n", wtotal);
}
printf("Waiting for the response...\n");
/**
* Read data length.
*/
rwbytes = recv(net_fd, &(pkt->len), sizeof(uint16_t), 0);
if (rwbytes < 0) {
perror("Read error");
goto close_socket;
}
/**
* Read data.
*/
rwbytes = recv(net_fd, &(pkt->data), pkt->len, 0);
if (rwbytes < 0) {
perror("Read error");
goto close_socket;
}
/* Add null terminator */
pkt->data[pkt->len] = '\0';
printf("Received data \"%s\" (%d bytes).\n", pkt->data, pkt->len);
close_socket:
close(net_fd);
return 0;
}