-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.c
83 lines (59 loc) · 1.5 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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#define SOCKET_NAME "/tmp/DemoSocket"
#define BUFFER_SIZE 128
int main(int argc, char *argv[])
{
struct sockaddr_un addr;
int i;
int ret;
int data_socket;
char buffer[BUFFER_SIZE];
/* *** Create data socket *** */
data_socket=socket(AF_UNIX,SOCK_STREAM,0); //The communication file descriptor returned is a data socket, and not a connection socket/master socket file descriptor like in the case of server
if (data_socket==-1)
{
perror("socket");
exit(EXIT_FAILURE);
}
/* *** clear the whole struct *** */
memset(&addr,0,sizeof(struct sockaddr_un));
/* *** Connect the socket to the socket address *** */
addr.sun_family=AF_UNIX;
strcpy(addr.sun_path,SOCKET_NAME);
ret=connect(data_socket,(const struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if(ret==-1)
{
fprintf(stderr,"The server is down.\n");
exit(EXIT_FAILURE);
}
/* *** Send arguments *** */
do
{
printf("Enter a number to send to the server: \n");
scanf("%d",&i);
ret=write(data_socket,&i,sizeof(int));
if(ret==-1)
{
perror("write");
exit(EXIT_FAILURE);
}
} while(i);
/* *** Request the result *** */
memset(buffer,0,BUFFER_SIZE);
ret=read(data_socket,buffer,BUFFER_SIZE);
if (ret== -1)
{
perror("read");
exit(EXIT_FAILURE);
}
printf("Received from the Server: %s\n",buffer);
/* *** Close socket *** */
close(data_socket);
exit(EXIT_SUCCESS);
}