-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.old.c
116 lines (93 loc) · 3.32 KB
/
server.old.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
/*
This code primarily comes from
http://www.prasannatech.net/2008/07/socket-programming-tutorial.html
and
http://www.binarii.com/files/papers/c_sockets.txt
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#define BUFF_SIZE 1000
char* temperature = "Hello, world";
int start_server(int PORT_NUMBER)
{
// structs to represent the server and client
struct sockaddr_in server_addr,client_addr;
int sock; // socket descriptor
// 1. socket: creates a socket descriptor that you later use to make other system calls
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
int temp;
if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&temp,sizeof(int)) == -1) {
perror("Setsockopt");
exit(1);
}
// configure the server
server_addr.sin_port = htons(PORT_NUMBER); // specify port number
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
// 2. bind: use the socket and associate it with the port number
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("Unable to bind");
exit(1);
}
// 3. listen: indicates that we want to listn to the port to which we bound; second arg is number of allowed connections
if (listen(sock, 5) == -1) {
perror("Listen");
exit(1);
}
// once you get here, the server is set up and about to start listening
printf("\nServer configured to listen on port %d\n", PORT_NUMBER);
fflush(stdout);
// 4. accept: wait here until we get a connection on that port
int sin_size = sizeof(struct sockaddr_in);
int fd = accept(sock, (struct sockaddr *)&client_addr,(socklen_t *)&sin_size);
printf("Server got a connection from (%s, %d)\n", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
// buffer to read data into
char request[1024];
// 5. recv: read incoming message into buffer
int bytes_received = recv(fd,request,1024,0);
// null-terminate the string
request[bytes_received] = '\0';
printf("Here comes the message:\n");
printf("%s\n", request);
// this is the message that we'll send back
/* it actually looks like this:
{
"name": "cit595"
}
*/
// char *reply = "{\n\"name\": \"cit595\"\n}\n";
char reply[BUFF_SIZE] = "{\n\"name\": \"";
strcat(reply, temperature);
strcat(reply, "\"\n}\n");
// 6. send: send the message over the socket
// note that the second argument is a char*, and the third is the number of chars
send(fd, reply, strlen(reply), 0);
//printf("Server sent message: %s\n", reply);
// 7. close: close the socket connection
close(fd);
close(sock);
printf("Server closed connection\n");
return 0;
}
int main(int argc, char *argv[])
{
// check the number of arguments
if (argc != 2)
{
printf("\nUsage: server [port_number]\n");
exit(0);
}
int PORT_NUMBER = atoi(argv[1]);
start_server(PORT_NUMBER);
}