-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunix-client.cc
95 lines (76 loc) · 2.08 KB
/
unix-client.cc
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
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/un.h>
#define SOCK_PATH "/tmp/socket"
#define BUF_LEN 128 * 1024 + 255
int
main (int argc, char *argv[])
{
struct sockaddr_un address;
int status = 0;
int sock = -1;
char buffer[BUF_LEN];
int lg = 0;
do
{
sock = socket (AF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
{
perror ("socket() failed");
break;
}
memset (&address, 0, sizeof(address));
address.sun_family = AF_UNIX;
strcpy (address.sun_path, SOCK_PATH);
std::cout << "Client: " << time (0) << " Before the Connect" << std::endl;
status = connect (sock, (struct sockaddr *) &address, SUN_LEN (&address));
if (status < 0)
{
std::cout << "Client: " << time (0) << " connect (timeout ?)" << std::endl;
perror ("connect() failed");
break;
}
std::cout << "Client: " << time (0) << " Connected ! " << std::endl;
memset (buffer, 'F', sizeof(buffer));
std::cout << "Client: About to send ... " << std::endl;
status = send (sock, buffer, sizeof(buffer), 0);
std::cout << "Server: send -> " << status << std::endl;
if (status < 0)
{
perror ("Client: send() failed");
break;
}
std::cout << "Client: send done:" << status << std::endl;
lg = 0;
while (lg < BUF_LEN)
{
status = recv (sock, &buffer[lg], BUF_LEN - lg, 0);
if (status < 0)
{
perror ("recv() failed");
break;
}
else if (status == 0)
{
std::cout << "The server closed the connection" << std::endl;
break;
}
lg += status;
}
std::cout << "read " << lg << " byte(s)" << std::endl;
}
while (0);
if (sock != -1)
{
close (sock);
}
std::cout << "client end." << std::endl;
return 0;
}