-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
85 lines (65 loc) · 2.13 KB
/
client.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
#include "nbb.h"
#include <assert.h>
int main()
{
char* service_name = (char*)malloc(sizeof(char)*50);
char* service_name_other = (char*)malloc(sizeof(char)*50);
char* client_name = (char*)malloc(sizeof(char)*50);
char* client_name_other = (char*)malloc(sizeof(char)*50);
char* msg = (char*)malloc(50*sizeof(char));
int msg_len;
int size;
int option;
char* array = (char*)malloc(sizeof(char) * MAX_MSG_LEN);
int array_size = MAX_MSG_LEN;
strcpy(service_name, GUI);
strcpy(service_name_other, "GUI2");
strcpy(client_name, "Client");
strcpy(client_name_other, "Client2");
if(nbb_connect_service(client_name, service_name) < 0) {
printf("Error getting channel!\n");
return -1;
}
if(nbb_connect_service(client_name_other, service_name_other) < 0) {
printf("Error getting channel!\n");
return -1;
}
printf("Available server: (1) GUI (2) GUI2\n");
while(1) {
printf("Choose a server: ");
scanf("%d", &option);
printf("Enter message to send to server: ");
scanf("%s", msg);
msg_len = strlen(msg);
printf("User entered '%s' (%d bytes)\n", msg, msg_len);
if(option == 1) nbb_send(service_name, msg, msg_len);
if(option == 2) nbb_send(service_name_other, msg, msg_len);
else continue;
printf("bytes available: %d\n", nbb_bytes_available(option));
printf("How many bytes do you want to read: ");
scanf("%d", &size);
if(size == -1) {
break;
}
if(size > nbb_bytes_available(option)) {
continue;
}
if (size >= array_size) {
// Grow the array size and ignore old contents (no need for realloc)
free(array);
array = (char *) malloc(sizeof(char) * 2 * array_size);
assert(array != NULL);
array_size = 2 * array_size;
}
nbb_read_bytes(option, array, size);
// array might be not null-terminated
printf("read %d bytes: '%.*s'\n", size, size, array);
}
printf("\n********Statistic********\n");
printf("bytes_available: %d\n", nbb_bytes_available(1));
printf("bytes_read: %d\n", nbb_bytes_read(1));
printf("bytes_written: %d\n", nbb_bytes_written(1));
free(array);
free(msg);
free(service_name);
}