-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.c
143 lines (114 loc) · 3.29 KB
/
server_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
136
137
138
139
140
141
142
143
#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;
}
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);
}
int main(void){
// int sock = socket(AF_INET, SOCK_STREAM, 0);
// bind(sock, &local, sizeof(local));
// listen(sock, 128);
int sock = SOCKET_Create(AF_INET, SOCK_STREAM, 10000, "0.0.0.0", 100);
struct sockaddr_in connection_addres;
socklen_t connection_addres_len = sizeof(connection_addres);
int client= accept(sock, (struct sockaddr*) &connection_addres, &connection_addres_len);
char buffer[4096];
int bytesRead=0, res;
for(;;) {
usleep(100);
res = read(client, buffer, 4096);
printf( "b -> %d %d\n" , bytesRead, res);
if(res < 0) {
perror("read");
exit(1);
}
if(!res)
break;
bytesRead += res;
// usleep(10);
}
// sleep(1);
// close(client);
printf("%d %d\n", bytesRead, write(client, "220 Welcome\r\n", 13));
SOCKET_Close(client);
}