-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
106 lines (92 loc) · 4.05 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
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
#include <stdio.h> // Import for `printf` & `perror` functions
#include <errno.h> // Import for `errno` variable
#include <fcntl.h> // Import for `fcntl` functions
#include <unistd.h> // Import for `fork`, `fcntl`, `read`, `write`, `lseek, `_exit` functions
#include <sys/types.h> // Import for `socket`, `bind`, `listen`, `connect`, `fork`, `lseek` functions
#include <sys/socket.h> // Import for `socket`, `bind`, `listen`, `connect` functions
#include <netinet/ip.h> // Import for `sockaddr_in` stucture
#include <string.h> // Import for string functions
void connection_handler(int sockFD); // Handles the read & write operations to the server
void main()
{
int socketFileDescriptor, connectStatus;
struct sockaddr_in serverAddress;
struct sockaddr server;
socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0);
if (socketFileDescriptor == -1)
{
perror("Error while creating server socket!");
_exit(0);
}
serverAddress.sin_family = AF_INET; // IPv4
serverAddress.sin_port = htons(8081); // Server will listen to port 8080
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); // Binds the socket to all interfaces
connectStatus = connect(socketFileDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
if (connectStatus == -1)
{
perror("Error while connecting to server!");
close(socketFileDescriptor);
_exit(0);
}
connection_handler(socketFileDescriptor);
close(socketFileDescriptor);
}
// Handles the read & write operations with the server
void connection_handler(int sockFD)
{
char readBuffer[1000], writeBuffer[1000]; // A buffer used for reading from user / writting to the server
ssize_t readBytes, writeBytes; // Number of bytes read from / written to the socket
char tempBuffer[1000];
do
{
bzero(readBuffer, sizeof(readBuffer)); // Empty the read buffer
bzero(tempBuffer, sizeof(tempBuffer));
readBytes = read(sockFD, readBuffer, sizeof(readBuffer)); // read the ition from nstrucserver
if (readBytes == -1)
perror("Error while reading from client socket!");
else if (readBytes == 0)
printf("No error received from server! Closing the connection to the server now!\n");
else if (strchr(readBuffer, '^') != NULL)
{
// Skip read from client
// ^ => this will just print the message to the client
strncpy(tempBuffer, readBuffer, strlen(readBuffer) - 1);
printf("%s\n", tempBuffer);
writeBytes = write(sockFD, "^", strlen("^"));
if (writeBytes == -1)
{
perror("Error while writing to client socket!");
break;
}
}
else if (strchr(readBuffer, '$') != NULL)
{
// Server sent an error message and is now closing it's end of the connection
strncpy(tempBuffer, readBuffer, strlen(readBuffer) - 2);
printf("%s\n", tempBuffer);
printf("Closing the connection to the server now!\n");
break;
}
else
{
bzero(writeBuffer, sizeof(writeBuffer)); // Empty the write buffer
// now there are 2 cases, password or the normal i/p from client
if (strchr(readBuffer, '#') != NULL)
strcpy(writeBuffer, getpass(readBuffer)); // getpass functions will take input from the client specially designed for passwords
else
{
printf("%s\n", readBuffer);
scanf("%[^\n]%*c", writeBuffer); // else Take user input!
}
// sending the data to the server
writeBytes = write(sockFD, writeBuffer, strlen(writeBuffer));
if (writeBytes == -1)
{
perror("Error while writing to client socket!");
printf("Closing the connection to the server now!\n");
break;
}
}
} while (readBytes > 0);
close(sockFD);
}