-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.c
130 lines (109 loc) · 3.45 KB
/
socket.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h> /* inet_ntoa */
#include <netdb.h> /* gethostname */
#include <sys/socket.h>
#include "socket.h"
/*
* Initialize a server address associated with the given port.
*/
struct sockaddr_in *init_server_addr(int port) {
struct sockaddr_in *addr = malloc(sizeof(struct sockaddr_in));
// Allow sockets across machines.
addr->sin_family = PF_INET;
// The port the process will listen on.
addr->sin_port = htons(port);
// Clear this field; sin_zero is used for padding for the struct.
memset(&(addr->sin_zero), 0, 8);
// Listen on all network interfaces.
addr->sin_addr.s_addr = INADDR_ANY;
return addr;
}
/*
* Create and setup a socket for a server to listen on.
*/
int setup_server_socket(struct sockaddr_in *self, int num_queue) {
int soc = socket(PF_INET, SOCK_STREAM, 0);
if (soc < 0) {
perror("socket");
exit(1);
}
// Make sure we can reuse the port immediately after the
// server terminates. Avoids the "address in use" error
int on = 1;
int status = setsockopt(soc, SOL_SOCKET, SO_REUSEADDR,
(const char *) &on, sizeof(on));
if (status < 0) {
perror("setsockopt");
exit(1);
}
// Associate the process with the address and a port
if (bind(soc, (struct sockaddr *)self, sizeof(*self)) < 0) {
// bind failed; could be because port is in use.
perror("bind");
exit(1);
}
// Set up a queue in the kernel to hold pending connections.
if (listen(soc, num_queue) < 0) {
// listen failed
perror("listen");
exit(1);
}
return soc;
}
/*
* Wait for and accept a new connection.
* Return -1 if the accept call failed.
*/
int accept_connection(int listenfd) {
struct sockaddr_in peer;
unsigned int peer_len = sizeof(peer);
peer.sin_family = PF_INET;
fprintf(stderr, "Waiting for a new connection...\n");
int client_socket = accept(listenfd, (struct sockaddr *)&peer, &peer_len);
if (client_socket < 0) {
perror("accept");
return -1;
} else {
fprintf(stderr,
"New connection accepted from %s:%d\n",
inet_ntoa(peer.sin_addr),
ntohs(peer.sin_port));
return client_socket;
}
}
/******************************************************************************
* Client-specific functions
*****************************************************************************/
/*
* Create a socket and connect to the server indicated by the port and hostname
*/
int connect_to_server(int port, const char *hostname) {
int soc = socket(PF_INET, SOCK_STREAM, 0);
if (soc < 0) {
perror("socket");
exit(1);
}
struct sockaddr_in addr;
// Allow sockets across machines.
addr.sin_family = PF_INET;
// The port the server will be listening on.
addr.sin_port = htons(port);
// Clear this field; sin_zero is used for padding for the struct.
memset(&(addr.sin_zero), 0, 8);
// Lookup host IP address.
struct hostent *hp = gethostbyname(hostname);
if (hp == NULL) {
fprintf(stderr, "unknown host %s\n", hostname);
exit(1);
}
addr.sin_addr = *((struct in_addr *) hp->h_addr);
// Request connection to server.
if (connect(soc, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("connect");
exit(1);
}
return soc;
}