-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
109 lines (83 loc) · 2.59 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
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <pthread.h>
#include "util.h"
//#include "C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\libpthread.a"
int buffersize = 512;
SOCKET acceptSocket;
SOCKET connectedSockets[100];
int clients = 0;
void * listenClient(void * args) {
SOCKET s = args;
//char *buf = malloc(buffersize);
char buf[buffersize];
int rc = 0;
//printf("Server output:\n");
do {
rc = recv(s, buf, buffersize, 0);
//printf("Received: %i\n", rc);
//char *out = slice(buf, 0, rc);
char *out;
buf[rc] = '\0';
strcpy(out,buf);
printf("%s\n", out);
int i;
for(i = 0;i<100;i++){
if(connectedSockets[i] != s){
write1(connectedSockets[i],out);
}
}
} while (rc > 0);
}
int listenSocket() {
while (1) {
SOCKET connectedSocket;
long rc = listen(acceptSocket, 10);
if (rc == SOCKET_ERROR) {
printf("Fehler: listen, fehler code: %d\n", WSAGetLastError());
return 1;
} else {
printf("acceptSocket ist im listen Modus....\n");
}
connectedSocket = accept(acceptSocket, NULL, NULL);
if (connectedSocket == INVALID_SOCKET) {
printf("Fehler: accept, fehler code: %d\n", WSAGetLastError());
return 1;
} else {
printf("Neue Verbindung wurde akzeptiert!\n");
}
//listenClient(connectedSocket);
pthread_t thread_id;
pthread_create( & thread_id, NULL, listenClient, connectedSocket);
connectedSockets[clients] = connectedSocket;
clients++;
}
}
int bindSocket(int port) {
long rc;
SOCKADDR_IN addr;
acceptSocket = socket(AF_INET, SOCK_STREAM, 0);
if (acceptSocket == INVALID_SOCKET) {
printf("Fehler: Der Socket konnte nicht erstellt werden, fehler code: %d\n", WSAGetLastError());
return 1;
} else {
printf("Socket erstellt!\n");
}
memset( & addr, 0, sizeof(SOCKADDR_IN));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = ADDR_ANY;
rc = bind(acceptSocket, (SOCKADDR * ) & addr, sizeof(SOCKADDR_IN));
if (rc == SOCKET_ERROR) {
printf("Fehler: bind, fehler code: %d\n", WSAGetLastError());
return 1;
} else {
printf("Socket an port %i gebunden\n", port);
}
return 0;
}
int startServer() {
bindSocket(5000);
listenSocket();
}