-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlog_s.c
More file actions
76 lines (65 loc) · 1.6 KB
/
Copy pathlog_s.c
File metadata and controls
76 lines (65 loc) · 1.6 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// a server in the internet domain
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "log_s_functions.h"
int main(int argc, char *argv[])
{
int sock, length, n, pid;
struct sockaddr_in serv_addr, cli_addr;
socklen_t fromlen;
struct sockaddr_in server;
struct sockaddr_in from;
struct hostent *hp;
char buf[1024];
fd_set readfds;
//Port Validation
if (argc < 2) {
fprintf(stderr, "ERROR: provide a hostname port");
}
//Socket Creation
sock = makeSocket();
//Getting Host Address
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
//Address Validation
if (hp==0){
error("Unknown host");
}
bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length);
server.sin_port = htons(argv[1]); //Teara implemented argv[1] so that the first argument will be the port passed
length=sizeof(struct sockaddr_in);
fromlen = sizeof(struct sockaddr_in);
//UDP
while (1) {
//Message recieving
n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from, &length);
if (n < 0){
error("ERROR receiving from");
}
// if log_s receives the message that "echs_s is stopping", log_s will stop
if(buf == "echo_s is stopping")
{
fileWrite(buf);
return 0;
}
pid = fork();
if(pid <0)
error("Error on fork");
if(pid == 0)
{
fileWrite(buf);
exit(0);
}
}
// close all sockets
close(sock);
return 0;
}