IPV6 UDP编程实例 & IPV6 TCP编程实例

UdpServer.cpp

#include <iostream>
#include <WinSock2.h>
#include <ws2tcpip.h>

#pragma comment(lib, "ws2_32.lib")

int main(int argc, char** argv)
{
	using namespace std;

	cout<<"Server Start..."<<endl;

	WORD wVersion = 0;
	WSADATA wsaData;
	memset(&wsaData, 0, sizeof(wsaData));
	wVersion = MAKEWORD(2, 2);

	if (0 != WSAStartup(wVersion, &wsaData))
	{
		return -1;
	}

	if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
	{
		WSACleanup();
		return -1;
	}

	struct addrinfo hints;
	struct addrinfo* res = NULL;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_protocol = IPPROTO_UDP;
	hints.ai_flags = AI_PASSIVE;

	if (0 != getaddrinfo("192.168.0.19", "6677", &hints, &res))
	{
		WSACleanup();
		return -1;
	}

	SOCKET sk = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 
	bind(sk, res->ai_addr, res->ai_addrlen);

	char recvBuf[1024] = {'\0'};
	int lenRecv = 0;

	struct sockaddr_storage storage;
	int len = sizeof(storage);
	memset(&storage, 0, len);

	lenRecv = recvfrom(sk, recvBuf, 1024, 0, (struct sockaddr*)&storage, &len);
	cout<<"Recv From Client:"<<recvBuf<<" Len:"<<lenRecv<<endl;
	sendto(sk, recvBuf, lenRecv, 0, (struct sockaddr*)&storage, len);

	WSACleanup();
	closesocket(sk);

	return 0;
}

UdpClient.cpp

#include <iostream>
#include <WinSock2.h>
#include <ws2tcpip.h>

#pragma comment(lib, "ws2_32.lib")

int main(int argc, char** argv)
{
	using namespace std;

	cout<<"Client Start..."<<endl;

	WORD wVersion = 0;
	WSADATA wsaData;
	memset(&wsaData, 0, sizeof(wsaData));
	wVersion = MAKEWORD(2, 2);

	if (0 != WSAStartup(wVersion, &wsaData))
	{
		return -1;
	}

	if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
	{
		WSACleanup();
		return -1;
	}

	struct addrinfo hints;
	struct addrinfo* res = NULL;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_protocol = IPPROTO_UDP;
	hints.ai_flags = AI_CANONNAME;

	if (0 != getaddrinfo("192.168.0.19", "6677", &hints, &res))
	{
		WSACleanup();
		return -1;
	}

	SOCKET sk = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 
	bind(sk, res->ai_addr, res->ai_addrlen);

	char recvBuf[1024] = {'\0'};
	char sendBuf[1024] = {"abcd"};
	int len = res->ai_addrlen;
	int lenRecv = 0;

	sendto(sk, sendBuf, strlen(sendBuf), 0, res->ai_addr, len);
	lenRecv = recvfrom(sk, recvBuf, 1024, 0, res->ai_addr, &(len));
	cout<<"Recv From Server:"<<recvBuf<<" Len:"<<lenRecv<<endl;

	WSACleanup();
	closesocket(sk);

	return 0;
}


========================================================================

TcpServer.cpp

#include <iostream>
#include <WinSock2.h>
#include <ws2tcpip.h>

#pragma comment(lib, "ws2_32.lib")

int main(int argc, char** argv)
{
	using namespace std;

	cout<<"Server Start..."<<endl;

	WORD wVersion = 0;
	WSADATA wsaData;
	memset(&wsaData, 0, sizeof(wsaData));
	wVersion = MAKEWORD(2, 2);

	if (0 != WSAStartup(wVersion, &wsaData))
	{
		return -1;
	}

	if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
	{
		WSACleanup();
		return -1;
	}

	struct addrinfo hints;
	struct addrinfo* res = NULL;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_flags = AI_PASSIVE;

	if (0 != getaddrinfo(NULL, "6677", &hints, &res))
	{
		WSACleanup();
		return -1;
	}

	SOCKET sk = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 

	bind(sk, res->ai_addr, res->ai_addrlen);

	if (0 != listen(sk, 5))
	{
		WSACleanup();
		closesocket(sk);
		return -1;
	}

	struct sockaddr_storage storage;
	int len = sizeof(storage);
	memset(&storage, 0, len);

	SOCKET newSk = accept(sk, (struct sockaddr*)&storage, &len);
	closesocket(sk);

	char recvBuf[1024] = {'\0'};
	int lenRecv = 0;
	lenRecv = recv(newSk, recvBuf, 1024, 0);
	cout<<"Recv From Client:"<<recvBuf<<" Len:"<<lenRecv<<endl;
	send(newSk, recvBuf, lenRecv, 0);

	WSACleanup();
	closesocket(newSk);

	return 0;
}

TcpClient.cpp

#include <iostream>
#include <WinSock2.h>
#include <ws2tcpip.h>

#pragma comment(lib, "ws2_32.lib")

int main(int argc, char** argv)
{
	using namespace std;

	cout<<"Client Start..."<<endl;

	WORD wVersion = 0;
	WSADATA wsaData;
	memset(&wsaData, 0, sizeof(wsaData));
	wVersion = MAKEWORD(2, 2);

	if (0 != WSAStartup(wVersion, &wsaData))
	{
		return -1;
	}

	if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
	{
		WSACleanup();
		return -1;
	}

	struct addrinfo hints;
	struct addrinfo* res = NULL;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_flags = AI_CANONNAME;

	if (0 != getaddrinfo("123.123.123.19", "6677", &hints, &res))
	{
		WSACleanup();
		return -1;
	}

	SOCKET sk = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 

	if (0 != connect(sk, res->ai_addr, res->ai_addrlen))
	{
		WSACleanup();
		closesocket(sk);
		return -1;
	}

	char sendBuf[1024] = {"abcdef"};
	char recvBuf[1024] = {'\0'};
	int lenRecv = 0;

	send(sk, sendBuf, strlen(sendBuf), 0);
	lenRecv = recv(sk, recvBuf, 1024, 0);
	cout<<"Recv From Server:"<<recvBuf<<" Len:"<<lenRecv<<endl;
	

	WSACleanup();
	closesocket(sk);

	return 0;
}





























你可能感兴趣的:(IPV6 UDP编程实例 & IPV6 TCP编程实例)