-
Notifications
You must be signed in to change notification settings - Fork 1
/
NetworkPrograming_TelnetServerUsingSelect.cpp
155 lines (143 loc) · 8.48 KB
/
NetworkPrograming_TelnetServerUsingSelect.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// TelNetServerUsingSelect.cpp : Defines the entry point for the console application.
//
// TelnetServer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "winsock2.h"
#include "stdlib.h"
#include "stdio.h"
CRITICAL_SECTION CriticalSection;
SOCKET clients[64];
char clientsID[64][100];
int numClients = 0;
void deleteClient(int i) {
clients[i] = clients[numClients - 1];
strcpy(clientsID[i], clientsID[numClients - 1]);
strcpy(clientsID[numClients - 1], "");
numClients--;
}
int main(int argc, char** argv)
{
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
SOCKET listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (argc > 1) {
addr.sin_port = htons(atoi((char*)argv[1]));
}
else {
addr.sin_port = htons(5000);
}
bind(listener, (SOCKADDR *)&addr, sizeof(addr));
listen(listener, 8);
fd_set fdread;
int ret;
SOCKET client;
char buf[256];
for (int i = 0; i < 64; i++) memcpy(clientsID[i], "", 1);
printf("Telnet Server\n");
while (1)
{
FD_ZERO(&fdread);
FD_SET(listener, &fdread);
for (int i = 0; i < numClients; i++)
FD_SET(clients[i], &fdread);
ret = select(0, &fdread, NULL, NULL, NULL);
if (ret == SOCKET_ERROR) {
return 1;
}
if (ret > 0) {
if (FD_ISSET(listener, &fdread)) {
client = accept(listener, NULL, NULL);
clients[numClients] = client;
numClients++;
printf("Client connected: %d\n", client);
send(client, "Username: ", 11, 0);
}
for (int i = 0; i < numClients; i++) {
if (FD_ISSET(clients[i], &fdread)) {
// if client didn't login
if (!strcmp("", clientsID[i])) {
ret = recv(clients[i], buf, sizeof(buf), 0);
if (ret <= 0) {
deleteClient(i);
return -1;
}
buf[ret] = 0;
char username[31];
memcpy(username, &buf, ret - 1);
username[ret - 1] = '\0';
send(client, "Password: ",11, 0);
ret = recv(clients[i], buf, sizeof(buf), 0);
if (ret <= 0)
return -1;
buf[ret] = 0;
char password[31];
memcpy(password, &buf, ret - 1);
password[ret-1] = '\0';
char filename[] = "c:\\Users\\USER\\source\\repos\\NetworkProgramming_ChatServerUsingSelect\\TelNetServerUsingSelect\\db.txt";
//open and get the file handle
InitializeCriticalSection(&CriticalSection);
EnterCriticalSection(&CriticalSection);
FILE* fh;
fopen_s(&fh, filename, "r");
//check if file exists
if (fh == NULL) {
printf("File does not exists");
return 0;
}
//read line by line
const size_t line_size = 255;
char* line = (char*)malloc(line_size);
while (fgets(line, line_size, fh) != NULL) {
int len = strlen(line);
for (int j = 0; j < len; j++) {
if (line[j] == 32) {
char c_username[31];
memcpy(c_username, &line[0], j);
c_username[j] = '\0';
char c_password[31];
memcpy(c_password, &line[j+1], len-j-2);
c_password[len-j-2] = '\0';
if (strcmp(username, c_username) == 0 && strcmp(password, c_password) == 0) {
memcpy(clientsID[i], username, strlen(username) + 1);
break;
}
}
}
}
// free(fh);
free(line);
LeaveCriticalSection(&CriticalSection);
if (!strcmp("", clientsID[i])) {
send(clients[i], "Login error, Username: ", 24, 0);
}
else {
send(clients[i], "Command: ", 9, 0);
}
}
else {
// client logged in
InitializeCriticalSection(&CriticalSection);
EnterCriticalSection(&CriticalSection);
ret = recv(client, buf, sizeof(buf), 0);
if (ret <= 0)
return -1;
buf[ret - 1] = 0;
strcat(buf, " > c:\\Users\\USER\\source\\repos\\NetworkProgramming_ChatServerUsingSelect\\TelNetServerUsingSelect\\output.txt");
printf("%s", buf);
system(buf);
send(clients[i], "Command: ", 9, 0);
LeaveCriticalSection(&CriticalSection);
}
}
}
}
}
closesocket(client);
closesocket(listener);
WSACleanup();
return 0;
}