-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
61 lines (54 loc) · 1.44 KB
/
client.c
File metadata and controls
61 lines (54 loc) · 1.44 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
// man bind
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MY_SOCK_PATH "/data/local/tmp/ShellPhoneSocket"
#define LISTEN_BACKLOG 50
#define BUF_SIZE 500
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int
main(int argc, char *argv[])
{
int sfd, cfd;
struct sockaddr_un my_addr, peer_addr;
socklen_t peer_addr_size;
char buf[BUF_SIZE];
if ( argc < 2 ) {
fprintf(stderr, "Usage: %s number\n",argv[0]);
exit(EXIT_FAILURE);
}
sfd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (sfd == -1)
handle_error("socket");
memset(&my_addr, 0, sizeof(struct sockaddr));
/* Clear structure */
my_addr.sun_family = AF_LOCAL;
strncpy(my_addr.sun_path, MY_SOCK_PATH,
sizeof(my_addr.sun_path) - 1);
if (connect(sfd,(struct sockaddr *) &my_addr,sizeof(struct sockaddr)) == -1) {
fprintf(stderr,"socket connect fail\n");
exit(EXIT_FAILURE);
}
int len = strlen(argv[1])+1;
if (len + 1 > BUF_SIZE) {
fprintf(stderr,
"Ignoring long cmd %s\n", argv[1]);
exit(EXIT_FAILURE);
}
printf("Writing %s\n",argv[1]);
if (write(sfd, argv[1], len) != len) {
fprintf(stderr, "partial/failed write\n");
exit(EXIT_FAILURE);
}
printf("Reading ...\n");
int nread = read(sfd, buf, BUF_SIZE);
if (nread == -1) {
perror("read");
exit(EXIT_FAILURE);
}
printf("Received %ld bytes: %s\n", (long) nread, buf);
return 0;
}