3、inet_ntoa函数实现

1、重写htons、htonl

引入动态库

image.png

#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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值