-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yarintz33
committed
Dec 31, 2021
1 parent
105156a
commit 4ae0579
Showing
6 changed files
with
306 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
|
||
#include "Server.h" | ||
|
||
string socketIO::read(){ | ||
char c=0; | ||
string s = ""; | ||
while(c != '\n'){ | ||
recv(clientID,&c,sizeof(char),0); | ||
s += c; | ||
} | ||
return s; | ||
} | ||
void socketIO::write(string text){ | ||
const char* txt=text.c_str(); | ||
send(clientID,txt,strlen(txt),0); | ||
} | ||
|
||
void socketIO::write(float f){ | ||
ostringstream oss; | ||
oss <<f; | ||
string str(oss.str()); | ||
write(str); | ||
} | ||
|
||
void socketIO::read(float* f){ | ||
} | ||
|
||
|
||
Server::Server(int port)throw (const char*) { | ||
socNum = socket(AF_INET,SOCK_STREAM,0); | ||
if(socNum < 0) | ||
throw "Error! socket failed"; | ||
hasStopped = false; | ||
server.sin_family = AF_INET; | ||
server.sin_addr.s_addr = INADDR_ANY; | ||
server.sin_port = htons(port); | ||
|
||
if(bind(socNum,(struct sockaddr*)&server, sizeof(server))<0) | ||
throw "bind failure"; | ||
|
||
if(listen(socNum, 3)<0) | ||
throw "listen failure"; | ||
} | ||
|
||
void sigHandler(int sigNum){ | ||
cout<<"sidH"<<endl; | ||
} | ||
|
||
void Server::start(ClientHandler& cltHandler)throw(const char*){ | ||
this->trd = new thread([&cltHandler,this](){ | ||
signal(SIGALRM,sigHandler); | ||
while(this->hasStopped == false){ | ||
socklen_t clientSize = sizeof(this->client); | ||
alarm(1); | ||
int acceptedClient = accept(this->socNum,(struct sockaddr*)&this->client,&clientSize); | ||
if(acceptedClient>0){ | ||
cltHandler.handle(acceptedClient); | ||
close(acceptedClient); | ||
} | ||
alarm(0); | ||
|
||
} | ||
close(this->socNum); | ||
}); | ||
} | ||
|
||
void Server::stopServer(){ | ||
this->hasStopped=true; | ||
this->trd->join(); | ||
} | ||
|
||
Server::~Server() { | ||
} |
Oops, something went wrong.