Add method to return whether or not a socket is connected to a remote

address
This commit is contained in:
2025-07-18 11:09:56 -04:00
parent 5fe24bffd9
commit a7e252acd2

View File

@@ -37,6 +37,13 @@ Sock::Sock(char protocol, const char* address, int port) {
this->address = std::string(address); this->address = std::string(address);
} }
/* This method returns whether or not the socket is connected to a remote address */
bool Sock::get_remote_address() {
struct sockaddr_storage addr;
socklen_t len = sizeof(addr);
return getpeername(this->sock_fd, (struct sockaddr*)&addr, &len) == 0;
}
/* This method sends the given data, through the 'other_sockt' variable.. Client /* This method sends the given data, through the 'other_sockt' variable.. Client
and server classes extend this method, by setting this variable to different values. and server classes extend this method, by setting this variable to different values.
This function needs more testing for TCP, as it focuses on UDP right now. */ This function needs more testing for TCP, as it focuses on UDP right now. */