-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketlib.c
103 lines (88 loc) · 3.32 KB
/
socketlib.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
/*
* socketlib.c - library for basic TCP socket management
* Copyright (C) 2023 Antonio Addeo <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
/* To create the server we instantiate a socket relying on:
* 1. socket() to create a socket that allows communication between processes on different hosts connected by IPV4
* 2. setsockopt() to enable the reuse of address and port
* 3. bind() to bind the socket to (local) address and port
* 4. listen() to actually make the socket capable of listening for connections */
int createServer(int port) {
int serverFD;
int opt = 1;
if ((serverFD = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket creation error");
exit(EXIT_FAILURE);
}
if (setsockopt(serverFD, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
perror("setsockopt error");
exit(EXIT_FAILURE);
}
struct sockaddr_in address;
socklen_t addrlen = sizeof(address);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
if (bind(serverFD, (struct sockaddr*)&address, addrlen) == -1) {
perror("bind error");
exit(EXIT_FAILURE);
}
if (listen(serverFD, 3) == -1) {
perror("listen error");
exit(EXIT_FAILURE);
}
return serverFD;
}
/* Create a socket from a client connection request and return the relative file descriptor.
* Retry in case of error. */
int acceptConnection(int serverFD) {
struct sockaddr_in address;
socklen_t addrlen = sizeof(address);
int clientFD;
while ((clientFD = accept(serverFD, (struct sockaddr*) &address, &addrlen)) == -1);
return clientFD;
}
/* To instantiate the client socket we rely on socket() to create a socket that allows communication
* between processes on different hosts connected by IPV4. */
int createClient() {
int clientFD;
if ((clientFD = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket creation error");
exit(EXIT_FAILURE);
}
return clientFD;
}
/* To enable the connection of a client to the server we use:
* 1. htons() to convert values between host and network byte order to avoid conflicts about endianess format
* 2. inet_pton() to convert IPv4 and IPv6 addresses from text to binary form
* 3. connect() to initiate the connection to the server. */
void connectToServer(int clientFD, char *ip, int port) {
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(port);
if (inet_pton(AF_INET, ip, &serverAddress.sin_addr) <= 0) {
perror("Address is invalid or not supported");
exit(EXIT_FAILURE);
}
if (connect(clientFD, (struct sockaddr*) &serverAddress, sizeof(serverAddress)) < 0) {
perror("Connection failed");
exit(EXIT_FAILURE);
}
}