-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.c
More file actions
129 lines (105 loc) · 3.5 KB
/
client.c
File metadata and controls
129 lines (105 loc) · 3.5 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/******************************************************************************
*
* FILENAME:
* client.c
*
* DESCRIPTION:
* The example of client using Unix domain socket.
*
* REVISION(MM/DD/YYYY):
* 12/02/2013 Shengkui Leng (lengshengkui@outlook.com)
* - Initial version
*
******************************************************************************/
#include "common.h"
int main(void)
{
uds_client_t *clnt;
clnt = client_init(UDS_SOCK_PATH, 10);
if (clnt == NULL) {
printf("client: init error\n");
return STATUS_INIT_ERROR;
}
/********************** Get version of server ***********************/
{
uds_command_t req;
uds_response_version_t *ver;
req.command = CMD_GET_VERSION;
req.data_len = 0;
ver = (uds_response_version_t *)client_send_request(clnt, &req);
if (ver == NULL) {
printf("client: send request error\n");
client_close(clnt);
return STATUS_ERROR;
}
if (ver->common.status == STATUS_SUCCESS) {
if (ver->common.data_len !=
(sizeof(uds_response_version_t) - sizeof(uds_command_t)) ) {
printf("Invalid data length\n");
} else {
printf("Version: %d.%d\n", ver->major, ver->minor);
}
} else {
printf("client: CMD_GET_VERSION error(%d)\n", ver->common.status);
}
free(ver);
}
/********************** Get message from server ***********************/
{
uds_command_t req;
uds_response_get_msg_t *res;
req.command = CMD_GET_MESSAGE;
req.data_len = 0;
res = (uds_response_get_msg_t *)client_send_request(clnt, &req);
if (res == NULL) {
printf("client: send request error\n");
client_close(clnt);
return STATUS_ERROR;
}
if (res->common.status == STATUS_SUCCESS) {
printf("Message: %s\n", res->data);
} else {
printf("client: CMD_GET_MESSAGE error(%d)\n", res->common.status);
}
free(res);
}
/********************** Put message to server ***********************/
{
uds_request_put_msg_t req;
uds_command_t *res;
char str[] = "This is a message from client";
req.common.command = CMD_PUT_MESSAGE;
req.common.data_len = strlen(str)+1;
snprintf((char *)req.data, UDS_PUT_MSG_SIZE-1, "%s", str);
req.data[UDS_PUT_MSG_SIZE-1] = 0;
res = (uds_command_t *)client_send_request(clnt, (uds_command_t *)&req);
if (res == NULL) {
printf("client: send request error\n");
client_close(clnt);
return STATUS_ERROR;
}
if (res->status == STATUS_SUCCESS) {
printf("client: CMD_PUT_MESSAGE OK\n");
} else {
printf("client: CMD_PUT_MESSAGE error(%d)\n", res->status);
}
free(res);
}
/********************** Send an unknown request to server ***********************/
{
uds_command_t req;
uds_command_t *res;
req.command = 0xFFFF;
req.data_len = 0;
res = (uds_command_t *)client_send_request(clnt, &req);
if (res == NULL) {
printf("client: send request error\n");
client_close(clnt);
return STATUS_ERROR;
}
printf("client: response status(%d)\n", res->status);
free(res);
}
client_close(clnt);
return STATUS_SUCCESS;
}