1、重写htons、htonl
引入动态库
#include <iostream>
#include <WinSock2.h>
//#pragma comment(lib,"ws2_32.lib")
using namespace std;
WORD Htons(WORD h)
{
char* p = (char*)&h;
*p ^= p[1];
p[1] ^= *p;
*p ^= p[1];
return h;
}
DWORD Htonl(DWORD h)
{
char* p = (char*)&h;
*p ^= p[3];
p[3] ^= *p;
*p ^= p[3];
p[1] ^= p[2];
p[2] ^= p[1];
p[1] ^= p[2];
return h;
}
union ULong
{
DWORD n;
char s[4];
};
union UShort
{
WORD n;
char s[2];
};
WORD Htons(WORD h)
{
UShort u{h};
u.s[0] ^= u.s[1];
u.s[1] ^= u.s[0];
u.s[0] ^= u.s[1];
return u.n;
}
DWORD Htonl(DWORD h)
{
ULong u{h};
u.s[0] ^= u.s[3];
u.s[3] ^= u.s[0];
u.s[0] ^= u.s[3];
u.s[2] ^= u.s[1];
u.s[1] ^= u.s[2];
u.s[2] ^= u.s[1];
return u.n;
}
int main()
{
int n = 0x12345678;
int m = Htons(n);
cout << hex << showbase << n << endl;
cout << hex << showbase << m << endl;
m = Htonl(n);
cout << hex << showbase << m << endl;
return 0;
}
2、inet_ntoa,Inet_Ntop
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <WinSock2.h>
#include <Ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
//const char* inet_ntop(int af, const void* src, char* dst, size_t size);
char* inet_NtoA(struct in_addr in)
{
static char s[32];
sprintf(s, "%d.%d.%d.%d", in.S_un.S_un_b.s_b1, in.S_un.S_un_b.s_b2,
in.S_un.S_un_b.s_b3, in.S_un.S_un_b.s_b4);
return s;
}
//PCTSTR WSAAPI InetNtop(
// _In_ INT Family,
// _In_ PVOID pAddr,
// _Out_ PTSTR pStringBuf,
// _In_ size_t StringBufSize
//);
int main()
{
in_addr in{ 192,168,1,220 };
char s1[32], s2[32];
const char* p = InetNtop(AF_INET,&in,s1,sizeof(s1));
in = { 192,168,2,96 };
auto q = InetNtop(AF_INET, &in, s2, sizeof(s2));
return 0;
}
//int main()
//{
// in_addr in{192,168,1,220};
// const char* p = inet_NtoA(in);
// in = { 192,168,2,96 };
// auto q = inet_NtoA(in);
//
// return 0;
//}
3、inet_addr()、inet_pton()
#define _CRT_SECURE_NO_WARNINGS
//#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <WinSock2.h>
#include <WS2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
unsigned long InetAddr(const char* cp)
{
unsigned long d=0;
char* q = (char*) & d;
char s[32];
strcpy(s, cp);
auto p = strtok(s, ".");//192
if (!p)
return 0;
q[0] = atoi(p);
p = strtok(NULL, ".");//168
if (!p)
return 0;
q[1] = atoi(p);
p = strtok(NULL, ".");//1
if (!p)
return 0;
q[2] = atoi(p);
p = strtok(NULL, ".");
if (!p)
return 0;
q[3] = atoi(p);
return d;
}
//unsigned long InetAddr(const char* cp)
//{
// unsigned long d=0;
// char* p = (char*) & d;
// auto q = cp;
// //字符查找,strchr找出3个"."
// int i = -1;
// while (true)
// {
// *p++ = atoi(q);
// q = strchr(q, '.');
// if (++i > 2)
// break;
// if (!q)
// return 0;
// ++q;
// }
//
// return d;
//}
/*
INT WSAAPI InetPton(
_In_ INT Family,
_In_ PCTSTR pszAddrString,
_Out_ PVOID pAddrBuf
);
*/
int main()
{
/*int m, n;*/
/*int res = InetPton(AF_INET, "192.168.1.220", &m);
if (res == 1)
{
}
res = InetPton(AF_INET, "192.168.2.96", &n);
if (res == 1)
{
}*/
auto n = InetAddr("192.168.1.220");
//auto m = inet_addr("192.168.2.96");
return 0;
}
", &m);
if (res == 1)
{
}
res = InetPton(AF_INET, "192.168.2.96", &n);
if (res == 1)
{
}*/
auto n = InetAddr("192.168.1.220");
//auto m = inet_addr("192.168.2.96");
return 0;
}