-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_win.cpp
88 lines (71 loc) · 1.46 KB
/
server_win.cpp
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include "berasn1.hpp"
#include "xtypes.hpp"
#define BUFFER_SIZE 0x1000
byte buf[BUFFER_SIZE];
int
main(void)
{
int res;
std::cout << "Server started" << std::endl;
WSADATA wsaData;
res = WSAStartup(MAKEWORD(2,2), &wsaData);
if (res)
{
fprintf(stderr, "WSAStartup failed with error: %d\n", res);
return 1;
}
struct berasn1_conn conn;
struct berasn1_conn newconn;
res = berasn1_bind_listen(&conn, (char*)"1234");
if (res)
{
fprintf(stderr, "Failed to bind, stop\n");
exit(EXIT_FAILURE);
}
while (1)
{
res = berasn1_accept(&newconn, &conn);
if (res)
{
fprintf(stderr, "Failed to accept, stop\n");
exit(EXIT_FAILURE);
}
fprintf(stdout, "Accepted a connection.\n");
while (1)
{
ssize_t res;
res = berasn1_recv(&newconn, buf, BUFFER_SIZE);
if (!res)
break;
if (res < 0)
{
berasn1_close(&newconn);
exit(EXIT_FAILURE);
}
if (newconn.is_receiving)
{
fprintf(stderr, "Incoming message too large to handle, stop\n");
berasn1_close(&newconn);
exit(EXIT_FAILURE);
}
fprintf(stdout, "Got a %zu-byte long msg.\n", (size_t)res);
res = berasn1_send(&newconn, buf, (size_t)res);
if (!res)
break;
if (res < 0)
{
berasn1_close(&newconn);
exit(EXIT_FAILURE);
}
}
fprintf(stdout, "Dropped a connection.\n");
berasn1_close(&newconn);
}
berasn1_close(&conn);
WSACleanup();
return 0;
}