本文主要是介绍计算机网络-------------服务器代码实现(TCP/IP协议),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
服务器
我们把服务器比作是餐厅,那么我们要开一个餐厅,应该要做如下几步:
1.选择要开什么店(火锅店、KFC、烧烤…),(加载库:WSAStartUp())
2.地方找好了,那么我们就需要找个店长,帮我们看店(创建套接字:socket())
3.店长找好了,接下来我们需要让这个店长在店里工作,(绑定:bind())
4…一切准备工作完成之后,我们就需要接待客人了,观察是否来客人了(监听:listen())
5.来客人时,我们需要接待客人(接收连接:accept())
6.安排客人坐下后,我们就需要让客人点餐,(接收数据:recv())
7.客人点完餐,我们收到客人的菜单后,需要给客人回复(send())
…
8.客人吃完了,店长下班(关闭套接字:closesocket())
9.店长下班后,要把店关了(卸载库:WSACleanup())
以上就是一个服务器工作的过程,实现代码如下(封装到TCPNet类里了):对于一个类,显然有初始化,关闭操作,另外又需要收发数据,因此,TCPNet.h文件里代码如下
#pragma once
#include "INet.h"
#include "Kernel.h"class TCPNet:public INet
{
public:TCPNet(void);~TCPNet(void);
public:bool InitNetWork();void UnInitNetWork();bool SendData(SOCKET sockWaiter,char* szbuf,int nLen);void RecvData(SOCKET sockWaiter);
private:SOCKET m_sockListen;bool m_bFlagQuit;
};
下面是TCPNet.cpp里的代码,首先是构造函数:
TCPNet::TCPNet()
{m_sockListen = NULL;m_bFlagQuit = true;
}
初始化函数:
1.加载库
//1.加载库WORD wVersionRequested;WSADATA wsaData;int err;/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */wVersionRequested = MAKEWORD(2, 2);err = WSAStartup(wVersionRequested, &wsaData);if (err != 0) {/* Tell the user that we could not find a usable *//* Winsock DLL. */printf("WSAStartup failed with error: %d\n", err);return false;}/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {/* Tell the user that we could not find a usable *//* WinSock DLL. */printf("Could not find a usable version of Winsock.dll\n");UnInitNetWork();return false;}elseprintf("The Winsock 2.2 dll was found okay\n");
加载库代码看着挺复杂,其实这部分很简单,只需要到查看帮助文档,然后进入帮助文档后,下面有示例代码,将需要的部分复制过来即可(注意头文件也要导入)
2.创建套接字
//2.雇个店长--m_sockListen = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP );if(m_sockListen == INVALID_SOCKET ){UnInitNetWork();return false;}
3.绑定
//3.店长去店里工作()--sockaddr_in addrServer;addrServer.sin_family =AF_INET;addrServer.sin_addr.S_un.S_addr = 0;addrServer.sin_port = htons(8899);if(SOCKET_ERROR == bind(m_sockListen,(const SOCKADDR *)&addrServer,sizeof(sockaddr_in))){UnInitNetWork();return false;}
4.监听
//4.宣传--listen(); 一旦开始监听,一直处于监听状态if(SOCKET_ERROR == listen(m_sockListen,10)){UnInitNetWork();return false;}
下面个是关闭函数实现:
void TCPNet::UnInitNetWork()
{m_bFlagQuit =false;auto ite = m_lstThread.begin();while(ite != m_lstThread.end()){if(WAIT_TIMEOUT == WaitForSingleObject(*ite,100))TerminateThread(*ite,-1);CloseHandle(*ite);*ite = NULL;ite++;}if(m_sockListen){closesocket(m_sockListen);m_sockListen = NULL;}WSACleanup();
}
下面是接收数据函数实现
void TCPNet::RecvData(SOCKET sockWaiter)
{int nPackSize;char*pszbuf = NULL;while(m_bFlagQuit){//接收包大小int nRelReadNum = recv(sockWaiter,(char*)&nPackSize,sizeof(int),0);if(nRelReadNum <0){if(GetLastError() == 10054){auto ite = m_mapThreadIdToSocket.begin();while(ite != m_mapThreadIdToSocket.end()){if(sockWaiter == ite->first){closesocket(sockWaiter);m_mapThreadIdToSocket.erase(ite);return ;}ite++;}}continue;}//接收数据包pszbuf =new char[nPackSize];int noffset =0;while(nPackSize){nRelReadNum = recv(sockWaiter,pszbuf+noffset,nPackSize,0);noffset+= nRelReadNum;nPackSize -= nRelReadNum;}//处理//cout<<pszbuf<<endl;m_pKernel->DealData(pszbuf,sockWaiter);delete []pszbuf;pszbuf= NULL;}
}
因为,在使用TCP协议的时候可能会出现粘包问题,所以为了解决粘包问题,采用先发送包大小,在发送数据包的方式。在接收数据时由于不一定一次能接收完数据,所以是循环接收数据。
下面是发送数据代码:
bool TCPNet::SendData(SOCKET sockWaiter,char* szbuf,int nLen)
{if(!sockWaiter || !szbuf || nLen <0)return false;if(send(sockWaiter,(const char*)&nLen,sizeof(int),0) <=0)return false;if(send(sockWaiter,szbuf,nLen,0) <=0)return false;return true;
}
以上就是服务器实现代码,如果大家发现那里有问题,欢迎批评指正,请多多关注哈~
这篇关于计算机网络-------------服务器代码实现(TCP/IP协议)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!