《网络编程实验指导书.pdf》由会员分享,可在线阅读,更多相关《网络编程实验指导书.pdf(39页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、网络编程实验指导书 执笔:王新祥 教研室:计算机网络 网络编程上机操作实验是本课程必不可少的实践环节。上机实验的目的是使学生熟悉网络编程的全过程,加深对网络编程方法的理解,得到程序设计基本方法和技巧的训练,从而能真正掌握网络通信程序的设计方法,以解决网络通信的实际问题。实验一 获取计算机的网卡地址(2 学时)一、实验目的 1、掌握使用 NetBIOS 得到 MAC 地址的方法。2、掌握通过发送 ARP 来获取网络中计算机 MAC 地址的方法。二、实验内容与步骤(一)实验内容 1、使用 NetBIOS 得到本机的 MAC 地址;2、使用 ARP 获取网络中计算机的 MAC 地址。(二)实验步骤
2、1、利用 NetBIOS 得到 MAC 地址 首先发送 NCBENUM 命令,获取网卡的数目和每张网卡的内部编号,然后对每个网卡标号发送 NCBASTAT 命令获取其 MAC 地址。注意:这里的网卡是指捆绑了 NetBEUI 协议的通信协议栈,可以在网卡的属性处查看到。(1)在 VC+6.0 中创建空的控制台应用程序工程;(2)新建一个源文件,并注意将此源文件添加到创建的工程中,然后输入如下代码:1(3)编译、链接并执行文件,分析程序输出的结果。2、通过发送 ARP 来获取网卡的 MAC 地址 在 Windows 2000 及以上系统中也可以通过发送 ARP 来获取网络中计算机的 MAC 地址
3、,当然也包括自己的计算机。函数 SendARP 的原型如下:DWORD WINAPI SendARP(IPAddr DestIP,IPAddr SrcIP,PULONG pMacAddr,PULONG PhyAddrLen );2其中 pMacAddr 参数返回一个 ULONG 变量数组的指针。该数组的前 6 个字节包括了 DespIP 对应的物理地址。(1)在 VC+6.0 中创建空的控制台应用程序工具;(2)新建一个源文件并将其添加到工程中,然后输入如下代码:(3)注意:要将“218.196.13.163”这个 IP 地址替换成自己所使用机器的 IP 地址。(4)将“实验一的支撑文件”目录
4、下的所有文件拷贝到工程所在的目录;(5)编译、链接并执行程序,注意分析程序的输出结果。(三)注意事项 实验内容 1 的程序目录下要添加四个文件,即“IPExport.h”、“IPHlpApi.h”、“IPHlpApi.Lib”及“IPTypes.h”。并在工程设置对话框的“Link”选项卡中的“Object/libray Modules:”编辑框内的最后面添加“ws2_32.lib IPHlpApi.Lib”内容,以便对这两个库文件的引用。或者在源文件的头部添加如下两行代码:#pragma comment(lib,ws2_32.lib)#pragma comment(lib,IPHlpApi.
5、Lib)三、实验要求 程序能正常运行,能得到准确的结果。3实验二 简易聊天室(4 学时)一、实验目的 1、熟悉 VC+编程环境;2、熟悉使用 VC+设计程序的过程;3、熟练掌握 NetBIOS 编程的基本思路和方法。二、实验原理 NetBIOS 网络编程的核心是通过这个标准接口,以基于统一的命令集的网络控制块 NCB 作为参数的“Netbios(PNCB pncb)”函数调用,使用 VC+6.0 实现具有聊天功能的局域网内控制台应用程序。使用 Netbios 函数的关键是对 NCB 结构的理解,该结构体的定义如下:typedef struct _NCB UCHAR ncb_command;UC
6、HAR ncb_retcode;UCHAR ncb_lsn;UCHAR ncb_num;PUCHAR ncb_buffer;WORD ncb_length;UCHAR ncb_callnameNCBNAMSZ;UCHAR ncb_nameNCBNAMSZ;UCHAR ncb_rto;UCHAR ncb_sto;void(*ncb_post)(struct _NCB*);UCHAR ncb_lana_num;UCHAR ncb_cmd_cplt;UCHAR ncb_reserve10;HANDLE ncb_event;NCB;三、实验内容与步骤(一)实验内容 使用 Netbios 函数实现聊天室
7、功能。(二)实验步骤 1、在 Visual C+中创建头文件“NetBIOS.h”,并输入如下代码:#include#include#pragma comment(lib,netapi32.lib)#define MAXSESSIONS 254 4#define MAXNAMES 254#define NBCheck(x)if(NRC_GOODRET!=x.ncb_retcode)printf(Line%d:Got 0 x%x from NetBios()n,_LINE_,x.ncb_retcode);#define PNBCheck(x)if(NRC_GOODRET!=x-ncb_retco
8、de)printf(Line%d:Got 0 x%x from NetBios()n,_LINE_,x-ncb_retcode);#define Online 1 /A user is on line#define Offline 2 /A user is off line#define Listen 3 /A user is waiting for connection#define Message 4 /A user is on line typedef struct DataGram /the format of a datagram UCHAR flag;/the type of me
9、ssage char message256;/the content of message DataGram,*PDataGram;char LocalNameNCBNAMSZ;/local name char GroupNameNCBNAMSZ;/group name UCHAR NameNum;UCHAR GroupNameNum;#define DGLEN sizeof(struct DataGram)void MakeNetbiosName(char*achDest,char*szSrc)int cchSrc;cchSrc=strlen(szSrc);if(cchSrc NCBNAMS
10、Z)cchSrc=NCBNAMSZ;memset(achDest,NCBNAMSZ);memcpy(achDest,szSrc,cchSrc);if(cchSrc!=NCBNAMSZ)achDestcchSrc=0;UCHAR LanaEnum(LANA_ENUM*lenum)NCB ncb;UCHAR uRetCode;memset(&ncb,0,sizeof(NCB);ncb.ncb_command=NCBENUM;ncb.ncb_buffer=(UCHAR*)lenum;ncb.ncb_length=sizeof(LANA_ENUM);uRetCode=Netbios(&ncb);NBC
11、heck(ncb)return uRetCode;5 UCHAR RestNCB(UCHAR jiangxue_lana)NCB jiangxue_ncb;memset(&jiangxue_ncb,0,sizeof(NCB);jiangxue_ncb.ncb_command=NCBRESET;jiangxue_ncb.ncb_callname0=MAXSESSIONS;jiangxue_ncb.ncb_callname2=MAXNAMES;jiangxue_ncb.ncb_lana_num=jiangxue_lana;Netbios(&jiangxue_ncb);NBCheck(jiangxu
12、e_ncb)return(jiangxue_ncb.ncb_cmd_cplt);UCHAR Astatus(UCHAR jiangxue_lana,PVOID pBuffer,int cbBuffer,char*szName)NCB jiangxue_ncb;ZeroMemory(&jiangxue_ncb,sizeof(NCB);jiangxue_ncb.ncb_command=NCBASTAT;jiangxue_ncb.ncb_lana_num=jiangxue_lana;jiangxue_ncb.ncb_buffer=(PUCHAR)pBuffer;jiangxue_ncb.ncb_le
13、ngth=cbBuffer;strcpy(char*)jiangxue_ncb.ncb_callname,szName);Netbios(&jiangxue_ncb);NBCheck(jiangxue_ncb)return(NRC_GOODRET=jiangxue_ncb.ncb_retcode);BOOL Adapterlist(UCHAR nlana,char*szName)/get the state of each adapter with some protocol.int cbBuffer;ADAPTER_STATUS*pStatus;NAME_BUFFER*pNames;int
14、i;HANDLE hHeap;char cname16;hHeap=GetProcessHeap();/allocate the largest buffer that might be needed.cbBuffer=sizeof(ADAPTER_STATUS)+255*sizeof(NAME_BUFFER);pStatus=(ADAPTER_STATUS*)HeapAlloc(hHeap,0,cbBuffer);if(NULL=pStatus)return FALSE;if(!Astatus(nlana,(PVOID)pStatus,cbBuffer,szName)HeapFree(hHe
15、ap,0,pStatus);return FALSE;/list the adapter status structure and names if(pStatus-adapter_type=0 xFE)strcpy(cname,Ethernet);6 else strcpy(cname,The other type);printf(The%s Number on LANA%d with version%d.%d is:%02x%02x%02x%02x%02x%02xn,cname,nlana,pStatus-rev_major,pStatus-rev_minor,pStatus-adapte
16、r_address0,pStatus-adapter_address1,pStatus-adapter_address2,pStatus-adapter_address3,pStatus-adapter_address4,pStatus-adapter_address5);printf(The number of names in the local names table is%dn,pStatus-name_count);pNames=(NAME_BUFFER*)(pStatus+1);for(i=0;iname_count;i+)printf(t%.*sn,NCBNAMSZ,pNames
17、i.name);HeapFree(hHeap,0,pStatus);return TRUE;UCHAR AddName(char*kemin_LocalName,UCHAR kemin_lana,UCHAR*kemin_LocalNameNum)NCB kemin_ncb;memset(&kemin_ncb,0,sizeof(NCB);kemin_ncb.ncb_command=NCBADDNAME;kemin_ncb.ncb_lana_num=kemin_lana;strcpy(char*)kemin_ncb.ncb_name,kemin_LocalName);Netbios(&kemin_
18、ncb);NBCheck(kemin_ncb)*kemin_LocalNameNum=kemin_ncb.ncb_num;return(kemin_ncb.ncb_cmd_cplt);UCHAR AddGroupName(char*limin_GroupName,UCHAR limin_lana,UCHAR*limin_GroupNameNum)NCB limin_ncb;memset(&limin_ncb,0,sizeof(NCB);limin_ncb.ncb_command=NCBADDGRNAME;limin_ncb.ncb_lana_num=limin_lana;strcpy(char
19、*)limin_ncb.ncb_name,limin_GroupName);Netbios(&limin_ncb);NBCheck(limin_ncb)*limin_GroupNameNum=limin_ncb.ncb_num;return(limin_ncb.ncb_cmd_cplt);UCHAR DeleteName(char*liufei_LocalName,UCHAR liufei_lana)NCB liufei_ncb;memset(&liufei_ncb,0,sizeof(NCB);liufei_ncb.ncb_command=NCBDELNAME;7 liufei_ncb.ncb
20、_lana_num=liufei_lana;strcpy(char*)liufei_ncb.ncb_name,liufei_LocalName);Netbios(&liufei_ncb);NBCheck(liufei_ncb)return(liufei_ncb.ncb_cmd_cplt);UCHAR NetbiosListen(int lana,PNCB pncb,char*SourName)memset(pncb,0,sizeof(NCB);pncb-ncb_command=NCBLISTEN;/synchronous call strcpy(char*)pncb-ncb_name,Sour
21、Name);strcpy(char*)pncb-ncb_callname,*);pncb-ncb_lana_num=lana;return Netbios(pncb);UCHAR NetbiosCall(PNCB yixiong_pncb,UCHAR lana,char*yixiong_SourName,char*yixiong_DestName)memset(yixiong_pncb,0,sizeof(NCB);yixiong_pncb-ncb_command=NCBCALL;yixiong_pncb-ncb_lana_num=lana;strcpy(char*)yixiong_pncb-n
22、cb_name,yixiong_SourName);strcpy(char*)yixiong_pncb-ncb_callname,yixiong_DestName);return Netbios(yixiong_pncb);UCHAR SessionSend(UCHAR lana,UCHAR zhangke_SessionNum,char*pBuffer,WORD Length)NCB zhangke_ncb;memset(&zhangke_ncb,0,sizeof(NCB);zhangke_ncb.ncb_command=NCBSEND;zhangke_ncb.ncb_lsn=zhangke
23、_SessionNum;zhangke_ncb.ncb_buffer=(UCHAR*)pBuffer;zhangke_ncb.ncb_length=Length;return Netbios(&zhangke_ncb);UCHAR SessionRecv(UCHAR lana,UCHAR guojun_SessionNum,char*pBuffer)NCB guojun_ncb;memset(&guojun_ncb,0,sizeof(NCB);guojun_ncb.ncb_command=NCBRECV;guojun_ncb.ncb_lsn=guojun_SessionNum;guojun_n
24、cb.ncb_buffer=(UCHAR*)pBuffer;guojun_ncb.ncb_length=512;return Netbios(&guojun_ncb);UCHAR HangupSession(UCHAR wanglinjia_SessionNum)8 NCB wanglinjia_ncb;UCHAR ucRc;memset(&wanglinjia_ncb,0,sizeof(NCB);wanglinjia_ncb.ncb_command=NCBHANGUP;wanglinjia_ncb.ncb_lsn=wanglinjia_SessionNum;ucRc=Netbios(&wan
25、glinjia_ncb);return(ucRc);UCHAR SDGSend(UCHAR lana,char*wugang_DestName,UCHAR wugang_LocalNameNum,char*pBuffer,WORD Length)NCB wugang_ncb;UCHAR ucRc;memset(&wugang_ncb,0,sizeof(NCB);wugang_ncb.ncb_command=NCBDGSEND;strcpy(char*)wugang_ncb.ncb_callname,wugang_DestName);wugang_ncb.ncb_num=wugang_Local
26、NameNum;wugang_ncb.ncb_buffer=(UCHAR*)pBuffer;wugang_ncb.ncb_length=Length;ucRc=Netbios(&wugang_ncb);NBCheck(wugang_ncb)return(ucRc);UCHAR ADGSend(UCHAR lana,char*DestName,UCHAR LocalNameNum,HANDLE hEvent,char*pBuffer,WORD Length)NCB ncb;memset(&ncb,0,sizeof(NCB);ncb.ncb_command=NCBDGSEND|ASYNCH;ncb
27、.ncb_lana_num=lana;strcpy(char*)ncb.ncb_callname,DestName);ncb.ncb_num=LocalNameNum;ncb.ncb_buffer=(UCHAR*)pBuffer;ncb.ncb_length=Length;ncb.ncb_cmd_cplt=0 xff;ncb.ncb_event=hEvent;Netbios(&ncb);return ncb.ncb_cmd_cplt;UCHAR SDGRecv(UCHAR lana,UCHAR wangyan_Num,char*DestName,char*pBuffer,WORD*len)NC
28、B wangyan_ncb;memset(&wangyan_ncb,0,sizeof(NCB);wangyan_ncb.ncb_command=NCBDGRECV;wangyan_ncb.ncb_lana_num=lana;wangyan_ncb.ncb_num=wangyan_Num;9 wangyan_ncb.ncb_buffer=(UCHAR*)pBuffer;wangyan_ncb.ncb_length=*len;Netbios(&wangyan_ncb);NBCheck(wangyan_ncb)strcpy(DestName,(char*)wangyan_ncb.ncb_callna
29、me);return wangyan_ncb.ncb_cmd_cplt;UCHAR DatagramSendBC(UCHAR lana,UCHAR num,char*buf,WORD len)/send the broadcasting datagram from the given name-based number NCB ncb;ZeroMemory(&ncb,sizeof(NCB);ncb.ncb_command=NCBDGSENDBC;ncb.ncb_lana_num=lana;ncb.ncb_num=num;ncb.ncb_buffer=(PUCHAR)buf;ncb.ncb_le
30、ngth=len;Netbios(&ncb);NBCheck(ncb)return ncb.ncb_retcode;UCHAR DatagramRecvBC(UCHAR lana,UCHAR num,char*buf,WORD*len,char*Callname)/receive the broadcasting datagram from the given name-based number NCB ncb;ZeroMemory(&ncb,sizeof(NCB);ncb.ncb_command=NCBDGRECVBC;ncb.ncb_lana_num=lana;ncb.ncb_num=nu
31、m;ncb.ncb_buffer=(PUCHAR)buf;ncb.ncb_length=*len;Netbios(&ncb);NBCheck(ncb)*len=ncb.ncb_length;strcpy(Callname,(char*)ncb.ncb_callname);return ncb.ncb_retcode;int CommandTranslating(const char*Command)/return the different value for the different command if(strcmp(Command,lookup)=0)return 1;/look up
32、 the status of the locall adapter if(strcmp(Command,session)=0)return 2;/session communication if(strcmp(Command,sendtoname)=0)return 3;/unicast or multicast communication if(strcmp(Command,sendtoall)=0)return 4;/broadcast communication if(strcmp(Command,exit)=0)return 5;/quit return 0;2、在 VC+中创建源文件
33、“NetChat.cpp”,并输入如下代码:#include NetBIOS.h 10 UCHAR Elana10;UCHAR Elength=0;LANA_ENUM lenum;DWORD WINAPI SessionReceiveThread(LPVOID param)/the thread to receive the data for the session PNCB pncb=(PNCB)param;char buffer512;UCHAR SessionNum;UCHAR retcode;char DestNameNCBNAMSZ;SessionNum=pncb-ncb_lsn;m
34、emset(DestName,0,NCBNAMSZ);strcpy(DestName,(char*)pncb-ncb_callname);for(;)memset(buffer,0,512);retcode=SessionRecv(Elana0,SessionNum,buffer);if(retcode=0)printf(n%s says to you(on session):,DestName);puts(buffer);printf(please input message to send to%s:,DestName);continue;else printf(n%s leaves th
35、e connection to you!n,DestName);HangupSession(SessionNum);break;return 1;void ServerSession(PNCB pncb)HANDLE hSThread;DWORD dwSExitCode;DWORD id;char sbuffer512;UCHAR retcode;UCHAR SessionNum;char DestNameNCBNAMSZ;SessionNum=pncb-ncb_lsn;memset(DestName,0,NCBNAMSZ);strcpy(DestName,(char*)pncb-ncb_ca
36、llname);hSThread=CreateThread(NULL,0,SessionReceiveThread,(PVOID)pncb,0,&id);/create a thread to receive the message 11 dwSExitCode=0;retcode=0;printf(%s has accepted the connection to%s!n,LocalName,DestName);Sleep(1000);memset(sbuffer,0,512);strcpy(sbuffer,Welcome to Server!);SessionSend(Elana0,Ses
37、sionNum,sbuffer,sizeof(sbuffer);printf(When you send a message to the other party,n);printf(the message exit means stopping to send!nn);for(;)printf(please input message to send to%s:,DestName);memset(sbuffer,0,512);gets(sbuffer);if(strcmp(sbuffer,exit)=0)TerminateThread(hSThread,dwSExitCode);CloseH
38、andle(hSThread);HangupSession(SessionNum);break;retcode=SessionSend(Elana0,SessionNum,sbuffer,sizeof(sbuffer);if(retcode!=0)break;void ClientSession(PNCB pncb)HANDLE hCThread;DWORD dwCExitCode;DWORD id;char cbuffer512;UCHAR retcode;UCHAR SessionNum;char DestNameNCBNAMSZ;SessionNum=pncb-ncb_lsn;memse
39、t(DestName,0,NCBNAMSZ);strcpy(DestName,(char*)pncb-ncb_callname);hCThread=CreateThread(NULL,0,SessionReceiveThread,(PVOID)pncb,0,&id);/create a thread to receive the message dwCExitCode=0;retcode=0;printf(%s has connected to the server%s!n,LocalName,DestName);printf(When you send a message to the ot
40、her party,n);printf(the message exit means stopping to send!n);Sleep(1000);for(;)memset(cbuffer,0,512);gets(cbuffer);12 if(strcmp(cbuffer,exit)=0)TerminateThread(hCThread,dwCExitCode);CloseHandle(hCThread);HangupSession(SessionNum);break;retcode=SessionSend(Elana0,SessionNum,cbuffer,sizeof(cbuffer);
41、if(retcode!=0)break;printf(please input message to send to%s:,DestName);DWORD WINAPI GDGRecvTread(LPVOID param)/receive the message from the group WORD len=DGLEN;char GbufferDGLEN;PDataGram pdata=(PDataGram)Gbuffer;char GCallNameNCBNAMSZ;while(1)memset(GCallName,0,NCBNAMSZ);if(SDGRecv(Elana0,GroupNa
42、meNum,GCallName,Gbuffer,&len)=NRC_GOODRET)if(strcmp(GCallName,LocalName)=0)printf(The multicasting message has send out!n);else printf(nMulticasting senders name:%sn,GCallName);printf(the content of message:%sn,pdata-message);printf($);return 1;DWORD WINAPI SDGRecvTread(LPVOID param)/receive the mes
43、sage from Single user /receive the information from the given name number,/if the name number is set to 0 xff,then receive all the local data WORD len1=DGLEN;char buffer1DGLEN;PDataGram pdata1=(PDataGram)buffer1;char CallName1NCBNAMSZ;while(1)memset(CallName1,0,NCBNAMSZ);if(SDGRecv(Elana0,NameNum,Ca
44、llName1,buffer1,&len1)=NRC_GOODRET)if(pdata1-flag=Message)13 printf(nUnicasting senders name:%sn,CallName1);printf(the content of message:%sn,pdata1-message);printf($);if(pdata1-flag=Online)printf(nThe user%s is on line and has the group name%s!n$,CallName1,pdata1-message);return 1;void DGsendCMD(UC
45、HAR lana,UCHAR NameNum,char*DestName)char buff256;char bufferDGLEN;WORD len=DGLEN;PDataGram pdata=(PDataGram)buffer;printf(please input message to send to the name%s:,DestName);memset(buff,0,256);gets(buff);pdata-flag=Message;strcpy(pdata-message,buff);SDGSend(lana,DestName,NameNum,(char*)pdata,len)
46、;DWORD WINAPI DGRecvBCTread(LPVOID param)/receive the broadcasting message from single user /receive the information from the given name number,WORD len;char bufferDGLEN;char buffDGLEN;PDataGram pdata1=(PDataGram)buffer;PDataGram pdata2=(PDataGram)buff;char DestNameNCBNAMSZ;while(1)len=DGLEN;memset(
47、DestName,0,NCBNAMSZ);if(DatagramRecvBC(Elana0,NameNum,buffer,&len,DestName)!=NRC_GOODRET)continue;switch(pdata1-flag)case Message:/Ordinary message if(strcmp(DestName,LocalName)=0)printf(Broadcasting message has sent out!n);14 break;printf(nThe user%s broadcasts the message:%sn$,DestName,pdata1-mess
48、age);break;case Online:/a user comes on line if(strcmp(DestName,LocalName)=0)break;printf(nThe user%s goes on line and has the group name%s!n$,DestName,pdata1-message);Sleep(1000);len=DGLEN;pdata2-flag=Online;memset(pdata2-message,0,256);strcpy(pdata2-message,GroupName);SDGSend(Elana0,DestName,NameN
49、um,(char*)pdata2,len);break;case Offline:/a user gets off line if(strcmp(DestName,LocalName)=0)break;printf(nThe user%s has been off line!n$,DestName);break;case Listen:/a user is waiting for a connection if(strcmp(DestName,LocalName)=0)printf(Listening message has sent out!n);break;printf(nThe serv
50、er%s is waiting for a client to connect!n$,DestName);break;return 1;void DGsendBCCMD(UCHAR lana,UCHAR NameNum)char buff256;char bufferDGLEN;WORD len=DGLEN;PDataGram pdata=(PDataGram)buffer;printf(please input message to broadcast:);memset(buff,0,256);gets(buff);pdata-flag=Message;memset(pdata-messag