Visual C++网络编程学习(1)-UDP阻塞模型

Visual C++网络编程学习(1)-UDP阻塞模型

       利用C++搭建了一个简单的UDP阻塞模型,只是做了客户端的发送与服务器的接收。程序我已经测试了一遍,如果大家在看的过程中有什么问题,也希望大家相互交流。代码如下:

       服务器代码:

#include
#include
#pragma comment(lib,"Ws2_32.lib")
using namespace std;

#define SERVEPORT 10000
#define CLIENTPORT 100

class CUdpConnectServe
{
public:
	CUdpConnectServe()
	{};
	~CUdpConnectServe()
	{};
	//socket函数
	int InitSocketConnect();    //初始化SOCKET
	void CloseSocketConnect();  //关闭SOCKET
	int Receive(
		_Out_writes_bytes_to_(len, return) __out_data_source(NETWORK) char FAR * buf,
		_In_ int len,
		_In_ int flags
                     );
	//客户端地址
	sockaddr_in m_soClientAddr;
private:
	SOCKET m_SocketServe;
	sockaddr_in m_soServeAddr;  
	SOCKET m_SocketClient;
	int m_nAddrlen;
};
//初始化SOCKET
int CUdpConnectServe::InitSocketConnect()
{
	//初始化Winsock

	WSADATA wsaData = { 0 };
	WORD wVer = MAKEWORD(2,2);
	if (0!=WSAStartup(wVer, &wsaData))
	{
		cout << "初始化错误,错误代码:" << WSAStartup(wVer, &wsaData) << endl;
		return 0;
	}
	//服务器创建套接字
	if (NULL!=m_SocketServe)
	{
		closesocket(m_SocketServe);	
	}
	m_SocketServe = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (INVALID_SOCKET==m_SocketServe)
	{
		cout << "创建套接字错误,错误代码:" << WSAGetLastError() << endl;
		return 0;
	}
	cout << "套接字创建成功"<< endl;


	//服务器将本地套接字地址绑定到套接字上
	m_soServeAddr.sin_family = AF_INET;
	m_soServeAddr.sin_port = SERVEPORT;
	m_soServeAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

	if (bind(m_SocketServe, (sockaddr*)&m_soServeAddr,sizeof(m_soServeAddr)))
	{
		cout << "套接字绑定错误" << WSAGetLastError() << endl;
		return 0;
	}
	cout << "套接字绑定成功"<< endl;
	return 0;
}
//关闭SOCKET
void CUdpConnectServe::CloseSocketConnect()
{
	closesocket(m_SocketServe);
	WSACleanup();       //释放资源
}
//接收函数
int CUdpConnectServe::Receive(__out_data_source(NETWORK) char * buf, int len, int flags)
{
	m_nAddrlen= sizeof(m_soServeAddr);
	int nRet = recvfrom(m_SocketServe, buf,len, flags, (sockaddr*)&m_soClientAddr,&m_nAddrlen);
	return  nRet;
}
//主函数
void main()
{
	CUdpConnectServe udpConnect;
	udpConnect.InitSocketConnect();

	//初始化客户端地址
	udpConnect.m_soClientAddr.sin_family =AF_INET;
	udpConnect.m_soClientAddr.sin_port = CLIENTPORT;
	udpConnect.m_soClientAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	int buffsize = 10000;
	char *revBuffer = (char*)malloc(sizeof(char)*buffsize);
	while (1)
	{
		int nRet=udpConnect.Receive(revBuffer, buffsize,0);
		cout << "收到" << nRet << "个字节数据" << endl;
		cout << "收到数据为:" ;
		for (int i=0;i

      客户端代码:

#include
#include 
#include
#pragma comment(lib,"Ws2_32.lib")
using namespace std;

#define SERVEPORT 10000
#define CLIENTPORT 100
class CUdpConnectClient
{
public:
	CUdpConnectClient()
	{
		m_nAddrlen = sizeof(m_soServeAddr);
	};
	~CUdpConnectClient()
	{};
	//socket函数
	int InitSocketConnect();    //初始化SOCKET
	void CloseSocketConnect();  //关闭SOCKET
	int Send(
		_In_reads_bytes_(len) const char FAR * buf,
		_In_ int len,
		_In_ int flags
	);
	//服务器地址
	sockaddr_in m_soServeAddr;

   private:
	SOCKET m_SocketServe;
	sockaddr_in m_soClientAddr;
    SOCKET m_SocketClient;
	int m_nAddrlen;
};
//初始化SOCKET
int CUdpConnectClient::InitSocketConnect()
{
	//初始化Winsock

	WSADATA wsaData = { 0 };
	WORD wVer = MAKEWORD(2, 2);
	if (0 != WSAStartup(wVer, &wsaData))
	{
		cout << "初始化错误,错误代码:" << WSAStartup(wVer, &wsaData) << endl;
		return 0;
	}
	//服务器创建套接字
	if (NULL != m_SocketClient)
	{
		closesocket(m_SocketClient);
	}
	m_SocketClient= socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (INVALID_SOCKET == m_SocketClient)
	{
		cout << "创建套接字错误,错误代码:" << WSAGetLastError() << endl;
		return 0;
	}
	cout << "套接字创建成功" << endl;
	return 0;
}
//关闭套接字
void CUdpConnectClient::CloseSocketConnect()
{
	closesocket(m_SocketClient);
	WSACleanup();                     //系统释放Winsock资源 
}

//发送函数
int CUdpConnectClient::Send(const char * buf, int len, int flags)
{
	int nRet = sendto(m_SocketClient, buf, len, flags, (sockaddr *)&m_soServeAddr, m_nAddrlen);
	if (nRet<0)
	{
		cout << "发送数据错误,错误代码:" << WSAGetLastError() << endl;
	}
	return nRet;
}
//主函数
void main()
{
	CUdpConnectClient udpConnect;
	udpConnect.InitSocketConnect();

	//初始化服务器地址
	udpConnect.m_soServeAddr.sin_family = AF_INET;
	udpConnect.m_soServeAddr.sin_port = SERVEPORT;
	udpConnect.m_soServeAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	
	while (1)
	{
		cout << "请输入要发送的字符串:";
		string s1;
		cin >> s1;
		int buffsize = 10000;
		char *revBuffer = (char*)malloc(sizeof(char)*buffsize);
		int i = 0;
		for ( i= 0; i < s1.size(); i++)
		{
			*(revBuffer + i) = s1[i];
		}
		int nRet=udpConnect.Send(revBuffer,i,0);
		cout << "发送了"<< nRet<<"个字节"<

你可能感兴趣的:(Visual,C++)