#include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace boost::asio::ip; using boost::asio::ip::tcp; class project_item { public: std::string project_name; std::string stored_ip_address; }; int main(int argc, char *argv[]){ std::string stored_ip_address= "0.0.0.0"; std::string response_ip_address="0.0.0.0"; char buf[128]; boost::system::error_code error; std::vector project_names; std::vector project_addresses; std::vector stored_ip_addresses; unsigned int number_of_projects=0; cout << "Student server started" << endl; try { boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 2323)); for (;;) { tcp::socket socket(io_service); acceptor.accept(socket); boost::asio::ip::tcp::endpoint remote_ep = socket.remote_endpoint(); address remote_ad = remote_ep.address(); std::string new_ip_address = remote_ad.to_string(); std::cout << "received call from: " << new_ip_address << std::endl; /* if (new_ip_address==stored_ip_address) { response_ip_address="0.0.0.0"; } else { response_ip_address=stored_ip_address; }; */ #if 1 // handle different projects. size_t len=socket.read_some(boost::asio::buffer(buf), error); buf[len]=0; std::vector::iterator i1; bool has_project=false; for (i1=stored_ip_addresses.begin();i1project_name==buf) { has_project=true; break; }; }; if (!has_project) { project_item new_item; new_item.project_name=buf; new_item.stored_ip_address=new_ip_address; stored_ip_addresses.push_back(new_item); response_ip_address="0.0.0.0"; } else { if (i1->stored_ip_address==new_ip_address) response_ip_address="0.0.0.0"; else { response_ip_address=i1->stored_ip_address; i1->stored_ip_address=new_ip_address; }; }; std::cout << "Project \t Stored IP \n"; for (i1=stored_ip_addresses.begin(); i1project_name << "\t \t " << i1->stored_ip_address << std::endl; }; #endif boost::system::error_code ignored_error; boost::asio::write(socket, boost::asio::buffer(response_ip_address), boost::asio::transfer_all(), ignored_error); //stored_ip_address=new_ip_address; //std::cout << "stored: " << stored_ip_address << std::endl; } } catch (std::exception& e) { std::cerr << e.what() << std::endl; } };