-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclietn_test.c
140 lines (108 loc) · 3.07 KB
/
clietn_test.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <unistd.h>
// sockets
#include <netdb.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
// #include "./includes/socket_functions.h"
int SOCKET_Create(int _domain, int _type, int _port, char* _addr, int _n_listen){
int err = 0;
// create socket
int sock = socket(_domain, _type, 0);
if (sock < 0){
perror("Socket creation");
return sock;
}
// re use socket
int opt_val = 1;
err = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof(int));
if ( err < 0 ){
perror("Sock reuse option failed");
return err;
}
// sock addr
struct sockaddr_in sock_addr_info;
struct in_addr addr;
inet_aton(_addr, &addr);
sock_addr_info.sin_family = _domain;
sock_addr_info.sin_addr.s_addr = addr.s_addr;
sock_addr_info.sin_port = htons( (uint16_t)_port );
// bind
err = bind(sock, (struct sockaddr*) &sock_addr_info, sizeof(sock_addr_info));
if ( err < 0 ){
perror("Bind failed");
return err;
}
// listen
err = listen(sock, _n_listen);
if ( err < 0 ){
perror("Listen failed");
return err;
}
// return socket fd
return sock;
}
// connect to a socket and return thee file descriptor
int SOCKET_Connect(int _domain, int _type, int _port, char* _addr){
int err = 0;
// create socket
int sock = socket(_domain, _type, 0);
if (sock < 0){
perror("Socket creation");
return sock;
}
// sock addr
struct sockaddr_in sock_addr_info;
struct in_addr addr;
inet_aton(_addr, &addr);
sock_addr_info.sin_family = _domain;
sock_addr_info.sin_addr.s_addr = addr.s_addr;
sock_addr_info.sin_port = htons( (uint16_t)_port );
// connect to server
err = connect(sock, (struct sockaddr*) &sock_addr_info, sizeof(sock_addr_info));
if ( err < 0 ){
perror("Connection failed");
return err;
}
return sock;
}
// close socket safely
int SOCKET_Close(int socket){
shutdown(socket, SHUT_RDWR);
char buffer[1];
int bytes_read = 0;
// while( (bytes_read = read(socket, buffer, 1)) == 0 ){
// printf("bytes read -> %d",bytes_read);
// }
// printf("bytes read ---> %d",bytes_read);
close(socket);
}
#define size 10000
int main(int argd, char** argv){
int sock = SOCKET_Connect(AF_INET, SOCK_STREAM, 10000, "0.0.0.0");
// int sock = socket(AF_INET, SOCK_STREAM, 0);
// connect(sock, &remote, sizeof(remote));
// sleep(5);
int mb = atoi(argv[1]);
char buffer[size];
memset(buffer, 13, size);
for (int i = 0; i < mb; i++){
write(sock, buffer, size); // returns 1000000
printf("sent %d mb\n", i); fflush(stdout);
}
// sleep(1);
sleep(2);
// shutdown(sock, SHUT_RDWR);
// printf("SHUTTTDOWNN\n");
// sleep(2);
// printf("CLODEDDDDDDDD\n");
SOCKET_Close(sock);
}