-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpserver.c
194 lines (171 loc) · 4.62 KB
/
tcpserver.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifdef _WIN32
#define _WIN32_WINNT _WIN32_WINNT_WIN7
#include <winsock2.h> //for all socket programming
#include <ws2tcpip.h> //for getaddrinfo, inet_pton, inet_ntop
#include <stdio.h> //for fprintf, perror
#include <unistd.h> //for close
#include <stdlib.h> //for exit
#include <string.h> //for memset
void OSInit( void )
{
WSADATA wsaData;
int WSAError = WSAStartup( MAKEWORD( 2, 0 ), &wsaData );
if( WSAError != 0 )
{
fprintf( stderr, "WSAStartup errno = %d\n", WSAError );
exit( -1 );
}
}
void OSCleanup( void )
{
WSACleanup();
}
#define perror(string) fprintf( stderr, string ": WSA errno = %d\n", WSAGetLastError() )
#else
#include <sys/socket.h> //for sockaddr, socket, socket
#include <sys/types.h> //for size_t
#include <netdb.h> //for getaddrinfo
#include <netinet/in.h> //for sockaddr_in
#include <arpa/inet.h> //for htons, htonl, inet_pton, inet_ntop
#include <errno.h> //for errno
#include <stdio.h> //for fprintf, perror
#include <unistd.h> //for close
#include <stdlib.h> //for exit
#include <string.h> //for memset
void OSInit( void ) {}
void OSCleanup( void ) {}
#endif
int initialization();
int connection( int internet_socket );
void execution( int internet_socket );
void cleanup( int internet_socket, int client_internet_socket );
int main( int argc, char * argv[] )
{
//////////////////
//Initialization//
//////////////////
OSInit();
int internet_socket = initialization();
//////////////
//Connection//
//////////////
int client_internet_socket = connection( internet_socket );
/////////////
//Execution//
/////////////
execution( client_internet_socket );
////////////
//Clean up//
////////////
cleanup( internet_socket, client_internet_socket );
OSCleanup();
return 0;
}
int initialization()
{
//Step 1.1
struct addrinfo internet_address_setup;
struct addrinfo * internet_address_result;
memset( &internet_address_setup, 0, sizeof internet_address_setup );
internet_address_setup.ai_family = AF_UNSPEC;
internet_address_setup.ai_socktype = SOCK_STREAM;
internet_address_setup.ai_flags = AI_PASSIVE;
int getaddrinfo_return = getaddrinfo( NULL, "24042", &internet_address_setup, &internet_address_result );
if( getaddrinfo_return != 0 )
{
fprintf( stderr, "getaddrinfo: %s\n", gai_strerror( getaddrinfo_return ) );
exit( 1 );
}
int internet_socket = -1;
struct addrinfo * internet_address_result_iterator = internet_address_result;
while( internet_address_result_iterator != NULL )
{
//Step 1.2
internet_socket = socket( internet_address_result_iterator->ai_family, internet_address_result_iterator->ai_socktype, internet_address_result_iterator->ai_protocol );
if( internet_socket == -1 )
{
perror( "socket" );
}
else
{
//Step 1.3
int bind_return = bind( internet_socket, internet_address_result_iterator->ai_addr, internet_address_result_iterator->ai_addrlen );
if( bind_return == -1 )
{
perror( "bind" );
close( internet_socket );
}
else
{
//Step 1.4
int listen_return = listen( internet_socket, 1 );
if( listen_return == -1 )
{
close( internet_socket );
perror( "listen" );
}
else
{
break;
}
}
}
internet_address_result_iterator = internet_address_result_iterator->ai_next;
}
freeaddrinfo( internet_address_result );
if( internet_socket == -1 )
{
fprintf( stderr, "socket: no valid socket address found\n" );
exit( 2 );
}
return internet_socket;
}
int connection( int internet_socket )
{
//Step 2.1
struct sockaddr_storage client_internet_address;
socklen_t client_internet_address_length = sizeof client_internet_address;
int client_socket = accept( internet_socket, (struct sockaddr *) &client_internet_address, &client_internet_address_length );
if( client_socket == -1 )
{
perror( "accept" );
close( internet_socket );
exit( 3 );
}
return client_socket;
}
void execution( int internet_socket )
{
//Step 3.1
int number_of_bytes_received = 0;
char buffer[1000];
number_of_bytes_received = recv( internet_socket, buffer, ( sizeof buffer ) - 1, 0 );
if( number_of_bytes_received == -1 )
{
perror( "recv" );
}
else
{
buffer[number_of_bytes_received] = '\0';
printf( "Received : %s\n", buffer );
}
//Step 3.2
int number_of_bytes_send = 0;
number_of_bytes_send = send( internet_socket, "Hello TCP world!", 16, 0 );
if( number_of_bytes_send == -1 )
{
perror( "send" );
}
}
void cleanup( int internet_socket, int client_internet_socket )
{
//Step 4.2
int shutdown_return = shutdown( client_internet_socket, SD_RECEIVE );
if( shutdown_return == -1 )
{
perror( "shutdown" );
}
//Step 4.1
close( client_internet_socket );
close( internet_socket );
}