-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_lin.cpp
65 lines (52 loc) · 1.02 KB
/
client_lin.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "berasn1.hpp"
#include "xtypes.hpp"
#define BUFFER_SIZE 0x1000
byte buf[BUFFER_SIZE];
int
main(void)
{
std::cout << "Client started" << std::endl;
struct berasn1_conn conn;
int res;
res = berasn1_connect(&conn, (char*)"127.0.0.1", (char*)"1234");
if (res)
{
fprintf(stderr, "Failed to connect to server\xa");
exit(EXIT_FAILURE);
}
while (!feof(stdin))
{
fgets((char*)buf, BUFFER_SIZE, stdin);
size_t len = strlen((char*)buf);
ssize_t res;
size_t off = 0;
while (off < len)
{
res = berasn1_send(&conn, buf + off, len - off);
if (res < 0)
{
fprintf(stderr, "Failed to send\xa");
exit(EXIT_FAILURE);
}
if (res == 0)
goto end;
off += (size_t)res;
}
res = berasn1_recv(&conn, buf, BUFFER_SIZE);
if (res < 0)
{
fprintf(stderr, "Failed to recv\xa");
exit(EXIT_FAILURE);
}
if (res == 0)
break;
fwrite(buf, 1, (size_t)res, stdout);
}
end:
berasn1_close(&conn);
return 0;
}