Programımda bir soket bağlantısı oluşturuyorum Socket
yaptığım bir sınıf kullanarak. Program sonsuz bir döngü içinde çalışır ve çıkmak için ctrl-c'yi kullanırım. Şimdi programı kapattığımda, yok edicim çağrılmıyor (here okunduğumda hafıza serbest bırakılıyor ancak program kesildiğinde soket serbest değil). Böyle bir programdan çıkan yıkıcıyı çağırmanın bir yolu var mı?Program kesintiye uğradığında Destructor çağrısı
soket sınıfı tanımı:
class Socket
{
private:
int m_sock;
sockaddr_in m_addr;
public:
Socket();
virtual ~Socket();
// Server initialization
bool create();
bool bind (const int port);
bool listen() const;
bool accept (Socket&) const;
// Client initialization
bool connect (const std::string host, const int port);
// Data Transimission
bool send (const std::string) const;
int recv (std::string&) const;
void set_non_blocking (const bool);
bool is_valid() const { return m_sock != -1; }
};
yoketme tanımı:
Socket::~Socket(){
close(m_sock);
}
ana işlevi:
try {
// Create the socket
ServerSocket server (15000);
std::cout << "Server running on 0.0.0.0:15000" << std::endl;
std::string res;
while(true) {
ServerSocket new_sock;
server.accept (new_sock);
try {
while(true) {
std::string data;
new_sock >> data;
res = process_query(data, kv_map, zset_map);
new_sock << res;
}
}
catch(SocketException&) {}
}
}
catch (SocketException& e){
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
return 0;
kodunu görmek için kullanışlı olabilirdi –
soruya bu tür standart cevaptır soket ve sinyal (ctrl-c) tespiti için boost :: asio kullanmak. Bütün komplikasyonları sizin için halleder. –
@RichardHodges Herhangi bir harici kütüphane kullanmıyorum. C++ kullanarak yazıyorum. – ayushgp