Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加socketAdd tostr 后字符串 反解析 #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions uv/SocketAddr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ Description: https://github.com/wlgq2/uv-cpp

#include "include/SocketAddr.hpp"
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <stdexcept>
#include <cstdlib>


using namespace uv;

Expand Down Expand Up @@ -112,4 +118,23 @@ uint16_t uv::SocketAddr::GetIpAndPort(const sockaddr_storage* addr, std::string&
}
}

std::pair<std::string, int> uv::SocketAddr::ExtractIpAndPort(const std::string& str)
{
size_t delim_pos = str.find(':');
if (delim_pos == std::string::npos) {
throw std::invalid_argument("Invalid format: missing ':' delimiter.");
}

std::string ip = str.substr(0, delim_pos);
std::string port_str = str.substr(delim_pos + 1);

try {
int port = std::stoi(port_str);
return std::make_pair(ip, port);
} catch (const std::exception& e) {
throw std::invalid_argument("Invalid port: cannot convert to an integer.");
}
}



2 changes: 1 addition & 1 deletion uv/include/SocketAddr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SocketAddr

static void AddrToStr(uv_tcp_t* client, std::string& addrStr, IPV ipv = Ipv4);
static uint16_t GetIpAndPort(const sockaddr_storage* addr, std::string& out, IPV ipv = Ipv4);

std::pair<std::string, int> ExtractIpAndPort(const std::string& str);
private:
std::string ip_;
unsigned short port_;
Expand Down