-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
87 lines (80 loc) · 1.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
#define _CRT_SECURE_NO_WARNINGS
#include<windows.h>
#include<stdio.h>
#include<time.h>
#define PORT 420
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsas;
int result;
WORD wersja;
wersja = MAKEWORD(1, 1);
result = WSAStartup(wersja, &wsas);
SOCKET s;
s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sa;
memset((void*)(&sa), 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(PORT);
sa.sin_addr.s_addr = htonl(INADDR_ANY);
result = bind(s, (struct sockaddr FAR*) & sa, sizeof(sa));
if (result != 0)
{
printf("Cannot bind socket");
exit(0);
}
result = listen(s, 4);
SOCKET si;
struct sockaddr_in sc;
int lenc;
char buf[20] = "PONG";
while (1)
{
lenc = sizeof(sc);
si = accept(s, (struct sockaddr FAR*) &sc, &lenc);
printf("Client accepted\n");
//trzeba dac do nowego watku i odpowienio konczyc petle
/*while (1)
{
if (recv(si, buf, 30, 0) > 0) {
buf[5] = '\0';
printf("Received %s from client, sending PONG\n", buf);
}
else {
printf("ERROR\n");
}
strcpy(buf, "PONG");
send(si, buf, 30, 0);
}*/
if (recv(si, buf, 20, 0) > 0)
{
if (strcmp(buf, "up") == 0)
{
printf("GURA\n");
}
else if (strcmp(buf, "down") == 0)
{
printf("DUU\n");
}
else if (strcmp(buf, "right") == 0)
{
printf("PRAWO\n");
}
else if (strcmp(buf, "left") == 0)
{
printf("LEWO\n");
}
else
{
printf("NIEZROZUMIALE\n");
}
}
else
{
printf("ERROR\n");
}
}
closesocket(si);
WSACleanup();
return 0;
}