《Socket编程利用TCP协议的实验报告(共9页).doc》由会员分享,可在线阅读,更多相关《Socket编程利用TCP协议的实验报告(共9页).doc(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上 Socket编程报告(TCP/IP协议) 一 实验目的通过C+来实现TCP/IP协议下的Socket编程,掌握TCP协议的算法实现。二 实验原理客户端 建立Socket,通过端口号和地址确定目标服务器,使用Connect连接到服务器,send发送消息,等待处理,通信完成后调用CloseSocket关闭Socket 服务端 建立Socket,声明自身的端口号和地址并绑定到Socket,使用Listen打开监听,然后不断用Accept去查看是否有连接,如果有,捕获Socket,并通过Recv获取消息的内容,通信完成后调用CloseSocket关闭这个对应Accept到的
2、Socket,如果不再需要等待任何客户端连接,那么用CloseSocket关闭掉自身的Socket三 实验代码(C+实现)服务器端代码如下:#pragma once#include #include #pragma comment (lib,ws2_32.lib)class CServerNetpublic: /初始化服务器,返回0表示成功int Init(const char* address,int port); /更新数据void Run();private:SOCKET m_sock;int CServerNet:Init( const char* address,int port )
3、int rlt = 0; /用于记录错误信息,并输出int iErrorMsg; /初始化WinSockWSAData wsaData;iErrorMsg = WSAStartup(MAKEWORD(1,1),&wsaData);if (iErrorMsg != NO_ERROR)/初始化WinSock失败printf(wsastartup failed with error : %dn,iErrorMsg); rlt = 1;return rlt;/创建服务端Socketm_sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if (m_sock =
4、 INVALID_SOCKET) /创建Socket异常printf(socket failed with error : %dn,WSAGetLastError(); rlt = 2;return rlt;/声明信息sockaddr_in serverAddr;serverAddr.sin_family = AF_INET;serverAddr.sin_port = port;serverAddr.sin_addr.s_addr = inet_addr(address);/绑定iErrorMsg = bind(m_sock,(sockaddr*)&serverAddr,sizeof(serv
5、erAddr);if (iErrorMsg 0)/绑定失败printf(bind failed with error : %dn,iErrorMsg);rlt = 3;return rlt; return rlt;void CServerNet:Run()/公开连接listen(m_sock,5);sockaddr_in tcpAddr;int len = sizeof(sockaddr);SOCKET newSocket;char buf1024;int rval;do/接收信息newSocket = accept(m_sock,(sockaddr*)&tcpAddr,&len); if (
6、newSocket = INVALID_SOCKET)/非可用socket else/新socket连接 printf(new socket connect : %dn,newSocket); /消息处理doprintf(processn);/接收数据memset(buf,0,sizeof(buf);rval = recv(newSocket,buf,1024,0);if (rval = SOCKET_ERROR)/这应该是个异常,当客户端没有调用closeSocket就直接退出游戏的时候,将会进入这里printf(recv socket errorn); if (rval = 0)/recv
7、返回0表示正常退出printf(ending connection);else/显示接收到的数据printf(recv %sn,buf);while(rval != 0);/关闭对应Accept的socketclosesocket(newSocket); while (1);/关闭自身的Socketclosesocket(m_sock);int main(int argc, char *argv)CServerNet serverNet;int iRlt = serverNet.Init(127.0.0.1,8888);if (iRlt = 0)printf(init ok.n);server
8、Net.Run();elseprintf(serverNet init failed with error : %dn,iRlt);system(pause);客户端代码如下:#pragma once#include #include#pragma comment(lib, Ws2_32.lib)class CClientNetpublic:/连接上指定服务器int Connect(int port,const char* address);/发送信息int SendMsg(const char* msg,int len);/关闭void Close();private:SOCKET m_so
9、ck;int CClientNet:Connect( int port,const char* address )int rlt = 0;/用于记录错误信息并输出int iErrMsg;/启动WinSockWSAData wsaData;iErrMsg = WSAStartup(MAKEWORD(1,1),&wsaData);if (iErrMsg != NO_ERROR)/有错误printf(failed with wsaStartup error : %dn,iErrMsg);rlt = 1;return rlt;/创建Socketm_sock = socket(AF_INET,SOCK_
10、STREAM,IPPROTO_TCP);if (m_sock = INVALID_SOCKET)/创建Socket失败printf(socket failed with error : %dn,WSAGetLastError();rlt = 2;return rlt;/目标服务器数据sockaddr_in sockaddrServer;sockaddrServer.sin_family = AF_INET;sockaddrServer.sin_port = port;sockaddrServer.sin_addr.s_addr = inet_addr(address);/连接,sock与目标服
11、务器连接iErrMsg = connect(m_sock,(sockaddr*)&sockaddrServer,sizeof(sockaddrServer);if (iErrMsg 0)printf(connect failed with error : %dn,iErrMsg);rlt = 3;return rlt;return rlt;int CClientNet:SendMsg(const char* msg,int len)int rlt = 0;int iErrMsg = 0;/发送消息,指定sock发送消息iErrMsg = send(m_sock,msg,len,0);if (i
12、ErrMsg 0)/发送失败printf(send msg failed with error : %dn,iErrMsg);rlt = 1;return rlt;return rlt;void CClientNet:Close()closesocket(m_sock);int main()CClientNet client;/连接到127.0.0.1(即本地),端口号为8888的服务端printf(Connectn);client.Connect(8888,127.0.0.1);/发送消息printf(send msgn);client.SendMsg(hello0,sizeof(hello0);/关闭socketprintf(closen);client.Close();system(pause);四 实验结果五 心得体会 通过本次实验,我了解了TCP/IP协议与UDP协议的异同之处,理解了Socket在通信中的地位和作用。在调试程序过程中的查错纠错也让我对程序有了深刻的理解,虽然程序是借鉴他人的,但我通过调试之后的进一步解析阅读也掌握了其中的原理,收获颇丰。专心-专注-专业