-
Notifications
You must be signed in to change notification settings - Fork 7
/
Socket_connect.c
81 lines (64 loc) · 1.72 KB
/
Socket_connect.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
#include <stdio.h>
#include <stdlib.h>
#include <utils/Log.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/uio.h>
#include "Socket_connect.h"
#define MAX_BUF_SIZE (255)
char* name = "/data/CarIOSocket-navi";
extern "C" int ConnectSocket()
{
int sockfd,
ret;
struct sockaddr_un servaddr;
socklen_t clilen;
char buf[MAX_BUF_SIZE];
ALOGD("[Navi client]socket client start\n");
ALOGD("[Navi client]open a socket .... \n");
/* open a socket */
if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
{
ALOGD("[Navi client]socket failed\n");
goto fun_exit;
}
ALOGD("[Navi client]set the addr .... \n");
/* set the addr */
//unlink(name);
bzero(&servaddr, sizeof(servaddr));
servaddr.sun_family = AF_LOCAL;
strcpy(servaddr.sun_path, name);
/* connect to server */
if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
{
ALOGD("[Navi client]connect failed\n");
goto fun_exit;
}
fd_set rset;
FD_ZERO(&rset);
FD_SET(sockfd, &rset);
int res = 0;
while(1)
{
ALOGD("[Navi client]select .....\n");
select(sockfd+1, &rset, NULL, NULL, NULL);
if (FD_ISSET(sockfd, &rset))
{
ALOGD("[Navi client]recv .....\n");
res = recv(sockfd, buf, MAX_BUF_SIZE, 0);
if(res <= 0)
{
ALOGD("[Navi client]recv failed\n");
goto fun_exit;
}
else
{
ALOGD("[Navi client] recv: %s :%d\n", buf, res);
}
}
}
fun_exit:
if (sockfd>0) close(sockfd);
return ret;
}