-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlog_s.c
72 lines (66 loc) · 2.51 KB
/
log_s.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
#include <sys/types.h> // include all the library
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
void error(const char *msg)
{
perror(msg);
exit(0);
}
/********
complete by Siyuan Sun
********/
int main(int argc, char *argv[])
{
int sock, length, n;//define the socket, the length of address and the status
socklen_t fromlen; // define the length
struct sockaddr_in server;//define the structure of the address of log.s
struct sockaddr_in from; // define the structure of the address of echo.s
if (argc < 2) {
fprintf(stderr, "ERROR, no port provided\n"); // if there is a error tell the user that no port number provided
exit(0);
}
sock=socket(AF_INET, SOCK_DGRAM, 0); // create a scoket
if (sock < 0) error("Opening socket");// output the error message if creating a socket is failed
length = sizeof(server);// get the size of log.s address
bzero(&server,length);// initial the address to 0
server.sin_family=AF_INET; // fill the structure with inforamtion of the log.s
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(atoi(argv[1]));
if (bind(sock,(struct sockaddr *)&server,length)<0)//bind the socket to an address
error("binding");
fromlen = sizeof(struct sockaddr_in);
/********
complete by Qi Gao
********/
while (1) {
char buf[1024];// define the buffer to store the message get from echo.s
memset(buf, 0, sizeof(buf));// set the buffer to zero
n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);// get the message from the echo.s
if (n < 0) error("recvfrom");// print out error message if the message is not got correctly
printf("Got message\n");
// -------------------------------
// Functionality to close the log server -- Enoch Ng (3rd deliverable, user 4)
if (strcmp("echo_s is stopping", buf) == 0) {
printf("Received shutdown signal");
close(sock);
return 0;
}
// -------------------------------
FILE *fp;
if((fp=fopen("echo.log","at+"))==NULL){// open a file
printf("Cannot open file strike any key exit!");// if not successfully opened, print out an error message
exit(1);
}
int numChar=sizeof(buf);
fputs(buf,fp);// write the meassage get from echo.s to the file
fclose(fp);
if(strcmp(buf,"echo_s is stopping")==0){
break;
}
return 0;
}