-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerMain.cpp
More file actions
82 lines (67 loc) · 2.16 KB
/
ServerMain.cpp
File metadata and controls
82 lines (67 loc) · 2.16 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
#include "UDPServerSocket.h"
#include <iostream>
#include "Sserver.h"
#include "MIS.h"
#include "TCPServerSocket.h"
#include "MisError.h"
#include <stdlib.h> /* calloc, exit, free */
#include Sserver
#define MAX 1514
using namespace std;
int main(){
char *addr=NULL;
TCPServerSocket *node=new TCPServerSocket(addr, 9999, MAX);
Server *ser=new Sserver();
node->initializeSocket(addr,9999);
TCPSocket*serve=node->getConnection(0,0,MAX,MAX);
//------------------------------------------
char*data;
data=(char*)calloc(MAX,sizeof(char));
//------------------------------------------
int *ip=new int();
*(ip)=0;
string* output= new string();
*(output)= "";
MIS* mis=new MIS();
while (true){
cout<<"start reading..."<<endl;
serve->readFromSocket(data,MAX);
cout<<"stop reading..."<<endl;
int x=ser->process(data);
cout<<"x= "<<x<<endl;
ser->print();
if(x==1){cout<<"WOW"<<endl;break;}
}
auto send = [=](char*res)->void{serve->writeToSocket(res,MAX);};
mis->lines=ser->lines;
char*res;
res=(char*)calloc(MAX,sizeof(char));
memset(res,0,MAX);
while(true){
try{
bool t = mis->exe(ip,output);
cout<<t<<endl;
if(output!=""){
cout<<"sending output... ";
res[0]='o';
for(int x=1;x<5;x++)res[x]=output[x-1];
serve->writeToSocket(buffer,MAX);
cout<<" ...output sent"<<endl;
}
if(t){cout<<"end of the file"<<endl;break;}
}
catch(const MisError& msg){
string text=msg.formattedError();
res[0]='e';
cout<<"ip "<<*(ip)<<endl;
output=to_string(*ip)+" "+text+"\n";
for(int i=1;i<output->length();i++)res[i]=output[i-1];
res[output->length()-1]=output[output->length()-1];
cout<<"sending ERR... ";
serve->writeToSocket(res,MAX);
cout<<" ...output sent"<<endl;
break;
}
}
cout<<"Good bye";
}