-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_2.cpp
129 lines (99 loc) · 3.33 KB
/
test_2.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
#include <iostream>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <fstream>
#include <string>
#include <cstring>
#include <signal.h>
/* Server port number, at which it listens */
#define PORT 1721
pthread_t thread_tcp [100], thread_file [10];
int threadno_tcp = 0;
int mistfd; // server file descriptor
/* message type that will be sent by the client through TCP connection */
struct msg {
char data [4098];
char read[10];
};
/* Structure to hold the necessary parameters to pass into the threaded function */
struct req {
int des;
socklen_t addlen;
sockaddr_in clientaddr;
};
void* tcp_connection (void*);
/* Signal handlre for ctrl+c to terminate the infinite server properly by closing the socket */
void sig_handler(int signo) {
/* signal handler */
if (signo == SIGINT) {
std::cout << "\t Exiting..." << '\n';
close (mistfd);
exit (1);
}
}
int main(int argc, char const *argv[]) {
/* server name : Mistcp */
sockaddr_in mistaddr; // server address
sockaddr_in clientaddr; // client address
socklen_t addrlen = sizeof(clientaddr);
/* Create socket */
if ((mistfd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
std::cout << "\n\t Socket creation failed...\n\t Exiting..." << '\n';
return 0;
}
std::cout << "\n\t Socket created..." << '\n';
memset ((sockaddr*)&mistaddr, 0, sizeof (mistaddr));
mistaddr.sin_family = AF_INET; // IPv4 address family
mistaddr.sin_addr.s_addr = htonl (INADDR_ANY); // Give the local machine address
mistaddr.sin_port = htons (PORT); // Port at which server listens to the requests
/* Bind the IP address and the port number to craete the socket */
if (bind (mistfd, (sockaddr*)&mistaddr, sizeof (mistaddr)) != 0) {
std::cout << "\n\t Binding failed...\n\t Exiting..." << '\n';
return 0;
}
std::cout << "\n\t Binding succesful..." << '\n';
if ((listen (mistfd, 5)) != 0) {
std::cout << "\n\t Server not listning..." << '\n';
return 0;
}
/* Signal catching */
signal(SIGINT, sig_handler);
signal(SIGTSTP, sig_handler);
std::cout << "\n\t Server listning..." << '\n';
/*
* Infinite Server listning loop
* which creates a seperate thread for each connection established
*/
while (1) {
int connfd;
if ((connfd = accept(mistfd,(sockaddr*)&clientaddr, &addrlen)) < 0)
std::cout << "\n\t Client connection declined..." << '\n';
else
std::cout << "\n\t Client connection accepted..." << '\n';
/* Filling the parameter values of the threaded function */
req *r = new req; // allocate memory
bzero (r, sizeof (req)); // Clear memory
r->addlen = addrlen;
r->clientaddr = clientaddr;
r->des = connfd;
pthread_create (&thread_tcp [threadno_tcp++], NULL, tcp_connection, (void*)r);
if (threadno_tcp == 100)
threadno_tcp = 0;
}
}
void* tcp_connection (void* arg) {
req sock = *((req*)arg);
msg buffer;
while (1) {
/* Read the packet sent by the established client */
read (sock.des, &buffer, sizeof(buffer));
std::cout << "\n\t File name: " << buffer.data;
}
}