-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_mirror_initiator.c
135 lines (114 loc) · 3.93 KB
/
main_mirror_initiator.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
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
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <sys/types.h> /* sockets */
#include <sys/socket.h> /* sockets */
#include <netinet/in.h> /* internet sockets */
#include <unistd.h> /* read, write, close */
#include <netdb.h> /* gethostbyaddr */
#include <stdlib.h> /* exit */
#include <string.h> /* strlen */
#include <stdio.h>
#include "definitions_client.h"
int main(int argc, char const *argv[])
{
struct sockaddr_in server;
struct sockaddr *serverptr = (struct sockaddr*) &server;
struct hostent *rem;
int port, i, a = 0, b = 0, c = 0, sock_fd, bytesRead;
char userBuf[userBuf_SIZE], messageFromServer[buf_SIZE], buf_OK[] = "OK";
char *p = "-p", *n = "-n", *s = "-s", *server_address, *server_port, *jobs;
if (argc == 7) //diavasma apo arxeio
{
for (i = 1; i < 7; i = i + 2) {
if (strcmp(argv[i], n) == 0) {
a = 1;
server_address = malloc((strlen(argv[i + 1]) + 1) * sizeof (char));
strcpy(server_address, argv[i + 1]);
} else if (strcmp(argv[i], p) == 0) {
b = 1;
server_port = malloc((strlen(argv[i + 1]) + 1) * sizeof (char));
strcpy(server_port, argv[i + 1]);
} else if (strcmp(argv[i], s) == 0) {
c = 1;
jobs = malloc((strlen(argv[i + 1]) + 1) * sizeof (char));
strcpy(jobs, argv[i + 1]);
} else {
if (a == 1)
free(server_address);
if (b == 1)
free(server_port);
if (c == 1)
free(jobs);
printf("(mirror_initiator) acceptable flags: -n -p -s\n");
exit(EXIT_FAILURE);
}
}
}
/* ________OPEN SOCKET________ */
if ((sock_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket");
return EXIT_FAILURE;
}
/* Find server address */
if ((rem = gethostbyname(server_address)) == NULL)
{
herror("gethostbyname");
return EXIT_FAILURE;
}
port = atoi(server_port); /*Convert port number to integer*/
printf("sock_fd: %d\n", sock_fd);
printf("MirrorServerAddress: %s, MirrorServerPort: %d, task: %s\n", server_address, port, jobs);
server.sin_family = AF_INET; /* Internet domain */
memcpy(&server.sin_addr, rem->h_addr, rem->h_length);
server.sin_port = htons(port); /* Server port */
/* Initiate connection */
if (connect(sock_fd, serverptr, sizeof(server)) < 0)
{
herror("connect");
return EXIT_FAILURE;
}
printf("Connecting to IP: %s - PORT: %d\n", argv[2], port);
// ------------------------------------------------
memset(messageFromServer, 0, buf_SIZE);
memset(userBuf, 0, userBuf_SIZE);
strcpy(userBuf, jobs);
/* ________WRITE TO FIFO________ */
int l = strlen(userBuf);
if (userBuf[l - 1] == '\n')
{ //exclude '\n'
userBuf[l - 1] = '\0';
}
write(sock_fd, userBuf, buf_SIZE);
memset(userBuf, 0, userBuf_SIZE);
while (1)
{
if ((bytesRead = read(sock_fd, messageFromServer, buf_SIZE)) > 0)
{
if (strcmp(messageFromServer, "PRINTEND") == 0)
{
printf("\n");
memset(messageFromServer, 0, buf_SIZE);
break;
}
else
{
write(sock_fd, buf_OK, 3);
printf("%s\n", messageFromServer);
memset(messageFromServer, 0, buf_SIZE);
}
}
}
/**********************************************************************************************/
close(sock_fd);
free(server_address);
free(server_port);
free(jobs);
return EXIT_SUCCESS;
}