-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetwork.hpp
More file actions
65 lines (58 loc) · 1.4 KB
/
Copy pathNetwork.hpp
File metadata and controls
65 lines (58 loc) · 1.4 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
#pragma once
#include <fcntl.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <termios.h>
#include <pthread.h>
#include <iostream>
#define CONCLOSE_MESSAGE "CLOSED\n"
class Server
{
int listenfd;
int connfd;
fd_set rset;
struct sockaddr_in cliaddr, servaddr;
public:
Server(uint16_t port)
{
listenfd = socket(AF_INET, SOCK_STREAM, 0);
connfd = 0;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
}
int GetConID() { return connfd; };
void Close()
{
write(connfd, CONCLOSE_MESSAGE, 9);
close(connfd);
std::cout << "Connection closed" << std::endl;
}
void soc_bind()
{
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
}
void listen(const int Connection_Number)
{
::listen(listenfd, Connection_Number);
FD_ZERO(&rset);
FD_SET(listenfd, &rset);
}
void accept_connection(socklen_t length)
{
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &length);
}
char * read_message()
{
char * buffer = new char[256];
read(connfd, buffer, sizeof(buffer));
return &buffer[0];
}
};