上传者: 43966957
|
上传时间: 2023-10-18 09:22:29
|
文件大小: 108KB
|
文件类型: DOC
"网络编程技术实验报告 " "实验名称"实验四 基于数据报套接字的回射程序设计与实验 " "队 别 " "【实验目的】 " "巩固套接字编程 " "熟悉流式套接字编程 " "【实验要求】 " "使用流式套接字编程实现回射程序,具体包括: " "1. 客户端:主动请求建立连接,接收界面输入字符串,发送数据给服务器,接收服 " "务器返回的应答,输出显示。 " "2. 服务器:在指定端口上提供服务,接收客户端的发送的回射请求字符串,将接收 " "到的内容回射给客户端,输出显示。 " "3. 提炼出网络操作的基本功能,构造网络操作类,修改原有程序 " "【实验环境】 " "DEV C++ " "【实验作业】 " "网络操作类设计: " "class CSocketFrame " "{ " "public: " "int start_up(); " "int clean_up(); " "int set_address(char *hname,char *sname,struct sockaddr_in *sap,char " "*protocol); " "int quit(SOCKET s); " "SOCKET tcp_server(ULONG uIP,USHORT uPort); " "SOCKET tcp_server(char *hname,char *sname); " "SOCKET tcp_client(char *hname,char *sname); " "SOCKET tcp_client(ULONG uIP,USHORT uPort); " "}; " "int CSocketFrame::start_up(void) " "{ " "WORD wVersionRequested; " "WSADATA wsaData; " "int iResult; " "wVersionRequested=MAKEWORD(2,2); " "iResult=WSAStartup(wVersionRequested,&wsaData); " "if(iResult!=0) " "{ " "printf("WSAStartup 调用错误,错误号:%d\n",WSAGetLastError()); " "return -1; " "} " "/* " "if(LOBYTE(wsaData.wVersion!=2""HIBYTE(wsaData.wVersion)!=2)) " "{ " "printf("无法找到可用的WSD版本\n"); " "WSACleanup(); " "return -1; " "//告诉用户无法找到可用的WSD " " " " " "} " "else " "{ " "printf("WS2.2初始化成功!\n"); " "} */ " "return 0; " "} " "int CSocketFrame::clean_up(void) " "{ " "int iResult; " "iResult=WSACleanup(); " "if(iResult==SOCKET_ERROR) " "{ " "//WSACleanup()调用失败 " "printf("WSACleanup 调用错误,错误号:%d\n",WSAGetLastError()); " "return -1; " "} " "else " "printf("Winsocket dll释放成功!\n") ; " "return 0; " "} " "int CSocketFrame::set_address(char *hname,char *sname,struct sockaddr_in " "*sap,char *protocol) " "{ " "struct servent *sp; " "struct hostent *hp; " "char *endptr; " "unsigned short port; " "unsigned long ulAddr=INADDR_NONE; " "//将地址结构socketsddr_in初始化为0,并将地址族设为AF_INET " "memset(sap,0,sizeof(*sap)); " "sap->sin_family=AF_INET; " "if(hname!=NULL) " "{ " "//如果hname不为空,转化地址格式 " "ulAddr=inet_addr(hname); " "if(ulAddr==INADDR_NONE""ulAddr==INADDR_ANY) " "{ " "//调用错误,调用gethostbyname获得主机地址 " "hp=gethost