forked from 3p1c/MUEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
157 lines (148 loc) · 3.71 KB
/
server.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#ifdef __WIN32
#include <windows.h>
#include <winsock2.h>
#else
#include "pthread.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "netdb.h"
#endif
#define MAX_CLIENTS 10
#define MAX_QUE 5
bool server_init(void);
bool server_create(void);
#ifdef _WIN32
void server_get_connections(SOCKET serv_socket);
#else
void server_get_connections(int serv_socket);
#endif
void server_cleanup(void);
void server_handle_clients(void* client_index);
SOCKET * client_sockets;
struct sockaddr * client_addrs;
int main(int argc, char ** argv)
{
SOCKET serv_socket;
if (server_init()) {
if (server_create()) {
printf("Server is running.\n");
while (1);
return 0;
}
server_cleanup();
}
return 1;
}
bool server_init(void)
{
#ifdef _WIN32
WSADATA wdata;
if (WSAStartup(MAKEWORD(2, 2), &wdata)) {
return false;
}
#endif
return true;
}
bool server_create(void)
{
struct sockaddr_in serv_addr;
memset((void*)&serv_addr, 0, sizeof(struct sockaddr_in));
#ifdef _WIN32
SOCKET server_socket;
if ((server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != INVALID_SOCKET) {
serv_addr.sin_port = htons(1001);
#else
int server_socket;
pthread_t threadid;
if ((server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
serv_addr.sin_port = (short)1001;
#endif
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_family = AF_INET;
if (!bind(server_socket, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr_in))) {
if (!listen(server_socket, MAX_QUE)) {
#if _WIN32
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&server_get_connections, (void*)server_socket, 0, 0);
#else
pthread_create(&threadid, NULL, &server_get_connections, (void*)server_socket);
#endif
return true;
}
}
}
return false;
}
#ifdef _WIN32
void server_get_connections(SOCKET serv_socket)
#else
void server_get_connections(int serv_socket)
#endif
{
int client_index;
#if !defined(_WIN32)
pthread_t threadid;
#endif
if (client_sockets = calloc(MAX_CLIENTS, sizeof(SOCKET))) {
#ifdef _WIN32
memset(client_sockets, INVALID_SOCKET, sizeof(SOCKET) * MAX_CLIENTS);
#else
memset(client_sockets, -1, sizeof(SOCKET) * MAX_CLIENTS);
#endif
if (client_addrs = calloc(MAX_CLIENTS, sizeof(struct sockaddr))) {
while (true) {
for (client_index = 0; client_index < MAX_CLIENTS; client_index++) {
#ifdef _WIN32
if (client_sockets[client_index] == INVALID_SOCKET) break;
#else
if (client_sockets[client_index] == -1) break;
#endif
}
if (client_index != MAX_CLIENTS) {
#ifdef _WIN32
if ((client_sockets[client_index] = accept(serv_socket, &(client_addrs[client_index]), NULL)) != INVALID_SOCKET) {
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&server_handle_clients, (void*)client_index, 0, 0);
}
#else
if ((client_sockets[client_index] = accept(serv_socket, &(client_addrs[client_index]), NULL)) != -1)
pthread_create(&threadid, NULL, &server_handle_clients, (void*)client_index);
#endif
}
#ifdef _WIN32
Sleep (100);
#else
usleep(100000);
#endif
}
free (client_addrs);
}
free (client_sockets);
}
return;
}
void server_handle_clients(void* client_index)
{
printf("Client #: %d\n", client_index);
client_sockets[(int)client_index] = 0;
return;
}
void server_cleanup(void)
{
#ifdef _WIN32
for (int i = 0; i < MAX_CLIENTS; i++)
if (client_sockets[i] != INVALID_SOCKET) closesocket(client_sockets[i]);
WSACleanup();
#else
int i;
for (i = 0; i < MAX_CLIENTS; i++)
if (client_sockets[i] != -1) close(client_sockets[i]);
#endif
if (client_addrs)
free (client_addrs);
if (client_sockets)
free (client_sockets);
return;
}