-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cc
56 lines (41 loc) · 1.28 KB
/
server.cc
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
/*
TCP Server from scratch
*/
#include <iostream>
#include <netinet/in.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstring>
#include "server_client_functions.h"
using namespace std;
// the driver function
int main() {
struct sockaddr_in server_info, client_info;
int socketfd, clientfd;
socketfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&server_info, 0, sizeof(server_info));
server_info.sin_family = AF_INET;
server_info.sin_addr.s_addr = htonl(INADDR_ANY);
server_info.sin_port = htons(PORT);
if (::bind(socketfd, (struct sockaddr *)&server_info, sizeof(server_info)) == -1) {
cerr << "Unable to bind to port." << endl;
exit(EXIT_FAILURE);
}
if (listen(socketfd, BACKLOG) == -1) {
cerr << "Unable to listen for connections." << endl;
exit(EXIT_FAILURE);
}
cout << "Server is listening on port " << PORT << endl;
socklen_t client_len = sizeof(client_info);
clientfd = accept(socketfd, (struct sockaddr *)&client_info, &client_len);
if (clientfd == -1) {
cerr << "Error accepting connection." << endl;
exit(EXIT_FAILURE);
}
receive_image(clientfd, "./image.png");
close(socketfd);
close(clientfd);
return 0;
}