-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
114 lines (98 loc) · 2.86 KB
/
server.c
File metadata and controls
114 lines (98 loc) · 2.86 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
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
/*
Compilation: gcc -o server server.c
Execution : ./server 5000
Requirements:
Server runs on the cse01 machine
Return number of vowels to the server
When receiving "Bye" or "bye", disconnect from client
*/
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <error.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) //command line arguments
{
int sockfd, newsockfd, portno, clilen, n; //variable declaration
struct sockaddr_in serv_addr, cli_addr;
char buffer[256];
if (argc < 2) //checking the number of command line arguments
{
printf("\nPort number is missing...\n");
exit(0);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error(EXIT_FAILURE, 0, "ERROR opening socket");
bzero((char *)&serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]); //user defined port number
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
error(EXIT_FAILURE, 0, "ERROR binding");
printf("\nServer Started and listening to the port %d\n", portno); //success
while (1) // running
{
listen(sockfd, 5); //Connecting with the client
clilen = sizeof(cli_addr);
fork(); //forks the process for different client instances
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error(EXIT_FAILURE, 0, "ERROR on accept");
printf("\nClient is connected...\n");
//Receiving the message from the client
while (1) //forever
{
int vowels = 0;
bzero(buffer, 256);
n = read(newsockfd, buffer, 255);
if (n < 0)
{
error(EXIT_FAILURE, 0, "ERROR reading from socket");
}
else if ((strcmp(buffer, "bye") == 0 || strcmp(buffer, "Bye") == 0)) //Start of meat
{
close(newsockfd); //Closing the connection
break;
}
else
{
printf("\nClient has sent: %s\n", buffer);
int i;
for (i = 0; buffer[i] != '\0'; ++i) //count up the vowels
{
if (buffer[i] == 'a' || buffer[i] == 'A')
{
++vowels;
}
if (buffer[i] == 'e' || buffer[i] == 'E')
{
++vowels;
}
if (buffer[i] == 'i' || buffer[i] == 'I')
{
++vowels;
}
if (buffer[i] == 'o' || buffer[i] == 'O')
{
++vowels;
}
if (buffer[i] == 'u' || buffer[i] == 'U')
{
++vowels;
}
}
//Sending the message to the client
bzero(buffer, 256);
sprintf(buffer, "%d", vowels); //converts int vowels to string for buffer
printf("%s", buffer); //end of meat
n = write(newsockfd, buffer, strlen(buffer)); //send buffer to client
}
}
}
return 0;
}