第3个回答 2011-02-26
”的字符串,提取出IP地址字符串和整型端口号。
其中IP和端口号分隔符“|”可替换,默认为“:”,但不可为“.”。
代码如下(VC6+WinXP调试通过):
#include <iostream>
#include <string>
using namespace std;
void getIP( const string fullIP, string& IP, unsigned int& port, const char dot = ':' )
{
IP = fullIP.substr( 0, fullIP.find_first_of(dot) );
port = atoi( fullIP.substr( fullIP.find_first_of(dot)+1, fullIP.length() ).c_str() );
}
void main()
{
string ip;
unsigned int port = 0;
char abc[] = "127.0.0.1:1024";
getIP( (string)abc, ip, port );
cout << ip << " " << port << endl;
string efg = "192.168.0.1|80";
getIP( efg, ip, port, '|' );
cout << ip << " " << port << endl;
}