-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrfcserver.cpp
More file actions
130 lines (101 loc) · 3.01 KB
/
rfcserver.cpp
File metadata and controls
130 lines (101 loc) · 3.01 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
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <stack>
using namespace std;
#define RFC_MAX_CLIENTS 6
#define BUFFERSIZE 256
void *clientThread(void *);
static int conn[RFC_MAX_CLIENTS];
int main(int argc, char* argv[])
{
int portNum, sockListen;
socklen_t socklen;
struct sockaddr_in svrAdd, clntAdd;
pthread_t threadid[RFC_MAX_CLIENTS];
if (argc < 2)
{
cerr << "Syntax : ./server <port>" << endl;
return 0;
}
portNum = atoi(argv[1]);
if((portNum > 65535) || (portNum < 2000))
{
cerr << "Please enter a port number between 2000 - 65535" << endl;
return 0;
}
//create socket
sockListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sockListen < 0)
{
cerr << "Cannot open socket" << endl;
return 0;
}
//fcntl(sockListen, F_SETFL, O_NONBLOCK); // set to non-blocking
bzero((char*) &svrAdd, sizeof(svrAdd));
svrAdd.sin_family = AF_INET;
svrAdd.sin_addr.s_addr = INADDR_ANY;
svrAdd.sin_port = htons(portNum);
//bind socket
if(bind(sockListen, (struct sockaddr *)&svrAdd, sizeof(svrAdd)) < 0)
{
cerr << "Cannot bind" << endl;
return 0;
}
listen(sockListen, RFC_MAX_CLIENTS);
cout << "\nRFC Server listening on port " << portNum << endl;
int threadIndex = 0;
// Main loop.
for(;;)
{
socklen = sizeof(clntAdd);
conn[threadIndex] = accept(sockListen, (struct sockaddr *)&clntAdd, &socklen);
if (conn[threadIndex] >= 0)
{
cout << "Connection successful" << endl;
int arg = threadIndex;
pthread_create(&threadid[threadIndex], NULL, clientThread, (void *) arg);
threadIndex++;
if (threadIndex >= RFC_MAX_CLIENTS) {
threadIndex = 0;
}
}
}
return 1;
}
// Client Thread.
void *clientThread (void *param)
{
int idx = *((int *) param);
//idx = (int *) param;
cout << "Starting thread " << pthread_self() << " : " << idx << endl;
char recvbuff[BUFFERSIZE];
bzero(recvbuff, BUFFERSIZE);
char sendbuff[BUFFERSIZE];
bzero(sendbuff, BUFFERSIZE);
int bytessent = 0;
int bytesrecv = 0;
for(;;)
{
strcpy(sendbuff, "{for the client};");
//bytessent = send(conn[idx], sendbuff, strlen(sendbuff), 0);
if (bytessent > 0)
{
cout << "Sent: " << bytessent << endl;
}
bzero(recvbuff, BUFFERSIZE);
//bytesrecv = recv(conn[idx], recvbuff, BUFFERSIZE-1, 0);
if(bytesrecv > 0)
{
cout << "Recv: " << bytesrecv << endl;
recvbuff[bytesrecv] = '\0';
cout << recvbuff << " ... " << endl;
}
}
cout << "Closing thread " << pthread_self() << endl;
//close(conn[idx]);
}