Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Community-Fossil
Community-Fossil
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
int main(int argc, char** args){ try{ std::cout<<"Starting server."<<std::endl; // start server boost::asio::io_service io; tcp::endpoint endpoints(tcp::v4(), 4321); auto server = std::shared_ptr<Server>(new Server(io, endpoints)); io.run(); std::cout<<"Server started. Type \"help\" to view commands."<<std::endl; // start command acceptor auto cmd = std::unique_ptr<Cmd>(new Cmd(server)); } catch(std::exception &e){ std::cerr<<e.what()<<std::endl; return 1; } return 0; } |
C-/C++-Quelltext |
|
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 |
Server::Server(boost::asio::io_service &io, const tcp::endpoint &endpoints):socket(io), acceptor(io, endpoints){ running = true; thread = std::thread(&Server::run, this); } Server::~Server(){ thread.join(); } void Server::run(){ while(running){ try{ auto client = std::shared_ptr<Client>(new Client(acceptor.get_io_service())); acceptor.async_accept(client->socket, boost::bind(&Server::accept, this)); } catch(std::exception &e){ std::cerr<<e.what()<<std::endl; } } } void Server::accept(){ std::cout<<"new client"<<std::endl; } void Server::stop(){ running = false; } |
Community-Fossil
Community-Fossil
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
void Client::send(const boost::system::error_code &error, const std::size_t bytes){ if(!error){ if(!file.eof()){ file.read(buffer, 512); boost::asio::async_write(socket, boost::asio::buffer(buffer, file.gcount()), boost::bind(&Client::send, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } else{ std::cout<<"File send."<<std::endl; // passiert auch file.close(); read(); } } else if(error == boost::asio::error::eof){ if(connected){ disconnect(); } return; } else{ read(); } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
void FileClient::download(){ boost::asio::async_read(socket, boost::asio::buffer(buffer), boost::bind(&FileClient::handleDownload, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } void FileClient::handleDownload(const boost::system::error_code &error, std::size_t bytes){ if(!error){ file.write(buffer, bytes); download(); return; } else if(error == boost::asio::error::eof){ file.write(buffer, bytes); file.close(); } else{ file.close(); } requests.pop(); downloading = false; request(); } |
Community-Fossil
Werbeanzeige