-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketlib.txt
More file actions
293 lines (205 loc) · 11.1 KB
/
Copy pathsocketlib.txt
File metadata and controls
293 lines (205 loc) · 11.1 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*****************************************************************
Copyright 2003 PIER LUCA MONTESSORO
University of Udine
ITALY
montessoro@uniud.it
www.montessoro.it
This file is part of a freeware open source software package.
It can be freely used (as it is or modified) as long as this
copyright note is not removed.
******************************************************************/
This is a brief documentation of the Pier Luca Montessoro's socketlib
functions that make easy writing simple client-server appplications.
User messages to be sent and received must be ASCII strings. However,
the functions can be easily extended to binary data exchange.
TCPSOCKETLIB
- int create_tcp_client_connection (char *ip_address, int port);
Creates a TCP client. Server IP address (string format, e.g. "10.0.0.1")
and TCP port (integer) are passed as arguments.
Returns the socket identifier, -1 on failure.
- int create_tcp_server (char *ip_address, int port);
Creates a TCP server. Its own IP address (string format, e.g. "10.0.0.1")
and TCP port (integer) are passed as arguments.
This function calls server_handle (see below), a user-defined function,
that will execute the server actions.
Returns the socket identifier, -1 on failure.
- int close_tcp_connection (int sk);
Closes a TCP connection. Should be called by both sides of the connections
(client and server). The socket identifier is passed as argument.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
void tcp_set_non_blocking_mode (int sk);
void tcp_set_blocking_mode (int sk);
Sets the mode to blocking or non-blocking. Currently implemented for receive
only: dimension of received data is greater than zero after successful receive.
WARNING: makes blocking/non-blocking every protocol on that socket!
- int tcp_send (int sk, char *buffer);
Sends the string in buffer ('\0' terminated) through the TCP connection.
The socket identifier is passed as argument.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
- int tcp_binary_send (int sk, char *buffer, int msg_len);
Sends msg_len data contained in buffer through the TCP connection.
The socket identifier is passed as argument.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
- int tcp_receive (int sk, char *buffer);
Receives a string from the TCP connection and puts it into buffer
('\0' terminated). The socket identifier is passed as argument.
Returns the number of bytes received, -1 (or exits via the error_handler
function) on failure.
- int tcp_binary_receive (int sk, char *buffer);
Receives data from the TCP connection and puts it into buffer.
The socket identifier is passed as argument.
Returns the number of bytes received, -1 (or exits via the error_handler
function) on failure.
- void tcp_putchar (int sk, int ch);
- int tcp_getchar (int sk);
Same as getc and putc, but from/to the TCP connection instead of
stdin/stdout.
int server_handler (int sk, char *ip_addr, int port);
Put here the server actions. Client IP address and TCP port are passed
in ip_addr and port arguments. For parallel server, here will be executed
the fork function to create child processes.
server_handler is called by create_tcp_server. If server_handler returns 0,
the server process is terminated, otherwise the server process executes
the accept() primitive again.
UDPSOCKETLIB
- int create_udp_client (void);
Creates a UDP client.
Returns the socket identifier, -1 on failure.
- int create_udp_server (char *ip_address, int port);
Creates a UDP server. Its own IP address (string format, e.g. "10.0.0.1")
and UDP port (integer) are passed as arguments.
Returns the socket identifier, -1 on failure.
- int close_udp_socket (int sk);
Closes a UDP socket. The socket identifier is passed as argument.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
void udp_set_non_blocking_mode (int sk);
void udp_set_blocking_mode (int sk);
Sets the mode to blocking or non-blocking. Currently implemented for receive
only: dimension of received data is greater than zero after successful receive.
WARNING: makes blocking/non-blocking every protocol on that socket!
- int udp_send (int sk, char *buffer, char *ip_address, int port);
Sends a UDP message containing the string in buffer ('\0' terminated).
The socket identifier, the server IP address (string format, e.g. "10.0.0.1")
and the UDP port (integer) are passed as arguments.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
- int udp_binary_send (int sk, char *buffer, int msg_len, int ip_address, int port);
Sends a UDP message containing msg_len data in buffer.
The socket identifier, the server IP address (string format, e.g. "10.0.0.1")
and the UDP port (integer) are passed as arguments.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
- int udp_receive (int sk, char *buffer);
Receives a UDP message ('\0' terminated) and puts it in buffer.
The socket identifier is passed as argument.
Returns the number of bytes received, -1 (or exits via the error_handler
function) on failure.
The sender IP address and UDP port are saved to be used by udp_reply
(see below).
- int udp_binary_receive (int sk, char *buffer);
Receives UDP data and puts them in buffer.
The socket identifier is passed as argument.
Returns the number of bytes received, -1 (or exits via the error_handler
function) on failure.
The sender IP address and UDP port are saved to be used by udp_reply
(see below).
- int udp_receive_and_get_sender_info (int sk, char *buffer,
char *ip_address, int *pport);
Same as udp_receive, but returns in ip_address and pport (by reference)
the sender IP address and UDP port.
The socket identifier is passed as argument.
Returns the number of bytes received, -1 (or exits via the error_handler
function) on failure.
- int udp_binary_receive_and_get_sender_info (int sk, char *buffer,
char *ip_address, int *pport);
Same as udp_binary_receive, but returns in ip_address and pport (by reference)
the sender IP address and UDP port.
The socket identifier is passed as argument.
Returns the number of bytes received, -1 (or exits via the error_handler
function) on failure.
- int udp_reply (int sk, char *buffer);
Sends a UDP message containing the string in buffer ('\0' terminated)
to the sender of the last received UDP message.
The socket identifier is passed as argument.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
- int udp_binary_reply (int sk, char *buffer, int msg_len);
Sends a UDP message containing msg_len data in buffer
to the sender of the last received UDP message.
The socket identifier is passed as argument.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
RAWSOCKETLIB
Raw sockets allow sending IP datagram without any transport layer header.
Client and server creates socket symmetrically (both execute bind).
Therefore, client and server must run on different computers having
different IP addresses.
- int create_raw_socket (char *ip_address, int protocol);
Creates a raw socket (client or server). Its own IP address (string format,
e.g. "10.0.0.1") and protocol number (integer) are passed as arguments.
Returns the socket identifier, -1 on failure.
- int close_raw_socket (int sk);
Closes a raw socket. The socket identifier is passed as argument.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
- int raw_send (int sk, char *buffer, char *ip_address);
Sends an IP datagram containing the string in buffer ('\0' terminated).
The socket identifier and the destination IP address (string format, e.g.
"10.0.0.1") are passed as arguments.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
- int raw_receive_and_get_sender_info
(int sk, char *buffer, char *ip_address);
Receives an IP datagram and puts it into buffer (it is NOT a message
string: it is the entire datagram, including the IP header).
Returns in ip_address the sender IP address.
The socket identifier is passed as argument.
Returns the number of bytes received, -1 (or exits via the error_handler
function) on failure.
User data in buffer starts at buffer [(buffer[0] & 0xF) * 4].
The sender IP address is saved to be used by raw_reply (see below).
- int raw_reply (int sk, char *buffer);
Sends an IP datagram containing the string in buffer ('\0' terminated)
to the sender of the last IP datagram received by
raw_receive_and_get_sender_info.
The socket identifier is passed as argument.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
MUDPSOCKETLIB (Multicast UDP)
- int create_mudp_client (char *ip_address, int port);
Creates a Multicast UDP client. The multicast IP address and the UDP port
are passed as arguments.
Returns the socket identifier, -1 on failure.
- int create_mudp_server (char *ip_address, int port);
Creates a Multicast UDP server. The multicast IP address (string format,
e.g. "239.0.0.1") and the UDP port (integer) are passed as arguments.
Returns the socket identifier, -1 on failure.
- int close_mudp_client (int sk);
Closes the socket associated to the multicast UDP client. The socket
identifier is passed as argument.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
- int close_mudp_server (int sk);
Closes the socket associated to the multicast UDP server. The socket
identifier is passed as argument.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
- int mudp_send (int sk, char *buffer);
Sends a Multicast UDP message containing the string in buffer ('\0' terminated).
The socket identifier, and the string are passed as arguments.
Returns 1 on success, 0 (or exits via the error_handler function) on failure.
- int mudp_receive (int sk, char *buffer);
Receives a Multicast UDP message ('\0' terminated) and puts it in buffer.
The socket identifier and the string are passed as arguments.
Returns the number of bytes received, -1 (or exits via the error_handler
function) on failure.
The sender IP address and UDP port are saved to be used by udp_reply
(see below).
- int mudp_receive_and_get_sender_info (int sk, char *buffer,
char *ip_address, int *pport);
Same as udp_receive, but returns in ip_address and pport (by reference)
the sender IP address and UDP port.
The socket identifier is passed as argument.
Returns the number of bytes received, -1 (or exits via the error_handler
function) on failure.
- int mudp_binary_send (int sk, char *buffer, int data_size);
- int mudp_binary_receive (int sk, char *buffer);
- int mudp_binary_receive_and_get_sender_info
(int sk, char *buffer, char *ip_address, int *pport);
Same as non-binary functions, but the data type is unsigned char and the data size
is passed as argument.
WARNING: Few variables are global and statically allocated. In a multi-thread
environment, this limits threads to one multicast server and one
multicast client in a single process. For the same reason, the UDP and
MUDP reply functions may not work properly in a multi-thread environment.