// Test.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include <string>
#include <winsock2.h>typedef struct ip_address
{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;typedef struct
{
ip_address src_addr;
unsigned short src_port;
ip_address dest_addr;
unsigned short dest_port;
}socket_pair;int _tmain(int argc, _TCHAR* argv[])
{
socket_pair cp;
std::string a("192.168.0.1:80-10.0.11.21:5689");
sscanf(a.c_str(), "%d.%d.%d.%d:%d-%d.%d.%d.%d:%d",
&cp.src_addr.byte1,
&cp.src_addr.byte2,
&cp.src_addr.byte3,
&cp.src_addr.byte4,
&cp.src_port,
&cp.dest_addr.byte1,
&cp.dest_addr.byte2,
&cp.dest_addr.byte3,
&cp.dest_addr.byte4,
&cp.dest_port);
printf("hele");
return 0;
}
//#include "stdafx.h"
#include <string>
#include <winsock2.h>typedef struct ip_address
{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;typedef struct
{
ip_address src_addr;
unsigned short src_port;
ip_address dest_addr;
unsigned short dest_port;
}socket_pair;int _tmain(int argc, _TCHAR* argv[])
{
socket_pair cp;
std::string a("192.168.0.1:80-10.0.11.21:5689");
sscanf(a.c_str(), "%d.%d.%d.%d:%d-%d.%d.%d.%d:%d",
&cp.src_addr.byte1,
&cp.src_addr.byte2,
&cp.src_addr.byte3,
&cp.src_addr.byte4,
&cp.src_port,
&cp.dest_addr.byte1,
&cp.dest_addr.byte2,
&cp.dest_addr.byte3,
&cp.dest_addr.byte4,
&cp.dest_port);
printf("hele");
return 0;
}
u_char byte2;
u_char byte3;
u_char byte4; 这几个与 %d 不匹配。空间太小。
static socket_pair cp; 就可以了呢