计算机网络编程课程设计.docx
《计算机网络编程课程设计.docx》由会员分享,可在线阅读,更多相关《计算机网络编程课程设计.docx(15页珍藏版)》请在冰豆网上搜索。
计算机网络编程课程设计
课题一:
基于TCP的Socket通讯编程
1、课程设计目的:
1.能够深刻了解socket编程思想;
2.从实现层面理解TCP和UDP链接的不同。
2、课程设计环境:
1.windowsXP或win7系统;
2.配置有java虚拟机的环境变量;
3.编写java程序的软件Eclipse。
3、课程设计原理:
WindowsSockets规范本意在于提供给应用程序开发者一套简单的API,并让各家网络软件供应商共同遵守。
此外,在一个特定版本Windows的基础上,WindowsSockets也定义了一个二进制接口(ABI),以此来保证应用WindowsSocketsAPI的应用程序能够在任何网络软件供应商的符合WindowsSockets协议的实现上工作。
因此这份规范定义了应用程序开发者能够使用,并且网络软件供应商能够实现的一套库函数调用和相关语义。
遵守这套WindowsSockets规范的网络软件,我们称之为WindowsSockets兼容的,而WindowsSockets兼容实现的提供者,我们称之为WindowsSockets提供者。
一个网络软件供应商必须百分之百地实现WindowsSockets规范才能做到现WindowsSockets兼容。
4、课程设计内容:
(1)网络程序初始化,服务器和客户端WinSockAPI均要求在调用其他WinSock函数以前先调用WSAStartUp函数初始化。
(2)创建套接字Socket()。
(3)配置并启动套接字。
(4)通过Socket发送和接收数据。
(5)程序结束时必须关闭Socket,使用与WSAStartUp()相对应的函数WSACleanUp(),释放所分配的内部缓冲区和其他资源。
代码:
EchoThreadServer:
import.*;
publicclassEchoThreadServer{
publicstaticvoidmain(String[]args)throwsException{
//TODOAuto-generatedmethodstub
ServerSocketserver=null;
Socketclient=null;
InetAddressremAdd=null;
server=newServerSocket(12345);
booleanf=true;
while(f){
……");
client=server.accept();
newThread(newEchoThread(client)).start();
}
server.close();
}
}
EchoThread:
importjava.io.*;
publicclassEchoThreadimplementsRunnable{
privateSocketclient=null;
publicEchoThread(Socketclient)
{
this.client=client;
}
publicvoidrun()
{
BufferedReaderbuf=null;
PrintStreamout=null;
BufferedReaderinput=null;
try{
out=newPrintStream(client.getOutputStream());
buf=newBufferedReader(newInputStreamReader(client.getInputStream()));
input=newBufferedReader(newInputStreamReader(System.in));
booleanflag=true;
while(flag)
{
Stringstr=buf.readLine();
if(str==null||"".equals(str)){
flag=false;
}
elseif("goodbye".equals(str)){
flag=false;
}
else{
out.println("Echo:
"+str);
}
}
client.close();
}catch(Exceptione){
//TODO:
handleexception
}
}
}
EchoClient:
publicclassEchoClient{
publicstaticvoidmain(String[]args)throwsException{
Socketclient=null;
client=newSocket("localhost",12345);
BufferedReaderbuf=null;
PrintStreamout=null;
BufferedReaderinput=null;
input=newBufferedReader(newInputStreamReader(System.in));
buf=newBufferedReader(newInputStreamReader(client.getInputStream()));
out=newPrintStream(client.getOutputStream());
booleanflag=true;
while(flag){
Stringstr=input.readLine();
out.println(str);
if("goodbye".equals(str)){
flag=false;
}
else{
Stringecho=buf.readLine();
}
}
buf.close();
client.close();
}
}
5、课程设计结果截图:
服务器端截图:
客户端截图:
6、课程设计总结:
课题二:
端口扫描
1、课程设计目的:
1.加深对课堂讲授知识的理解;
2.熟练的掌握基本的网络编程技术和方法;
3.建立网络编程整体概念;
4.培养具有研究、设计、编制和调试网络程序的能力。
2、课程设计环境:
1.windowsXP或win7系统;
2.配置有java虚拟机的环境变量;
3.编写java程序的软件Eclipse。
3、课程设计原理:
1.端口扫描器功能简介:
服务器上所开放的端口就是潜在的通信通道,也就是一个入侵通道。
对目标计算机进行端口扫描,能得到许多有用的信息,进行端口扫描的方法很多,可以是手工进行扫描、也可以用端口扫描软件进行。
扫描器通过选用远程TCP/IP不同的端口的服务,并记录目标给予的回答,通过这种方法可以搜集到很多关于目标主机的各种有用的信息,例如远程系统是否支持匿名登陆、是否存在可写的FTP目录、是否开放TELNET服务和HTTPD服务等。
2.实验所用的端口扫描技术:
端口扫描技术有TCPconnect()扫描、TCPSYN扫描、TCPFIN扫描、IP段扫描等等。
本次实验所用的技术是TCPconnect()扫描,这是最基本的TCP扫描,操作系统提供的connect()系统调用可以用来与每一个感兴趣的目标计算机的端口进行连接。
如果端口处于侦听状态,那么connect()就能成功。
否则,这个端口是不能用的,即没有提供服务。
这个技术的一个最大的优点是,你不需要任何权限。
系统中的任何用户都有权利使用这个调用。
4、课程设计内容:
编写一个端口扫描程序,能够显示某个IP或某段IP的计算机的某一个或某些端口是否正在工作。
基本工作过程如下:
(1)设定好一定的端口扫描范围;
(2)设定每个端口扫描的次数,因为有可能有的端口一次扫描可能不通;
(3)创建socket,通过socket的connect方法来连接远程IP地址以及对应的端口;
(4)如果返回false,表示端口没有开放,否则端口开放。
实现代码:
publicclassSocketView{
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame("主机端口扫描程序");
frame.setLayout(newBorderLayout(3,3));
JPanelpan1=newJPanel();
JPanelpan2=newJPanel();
JPanelpan3=newJPanel();
pan1.setLayout(newGridLayout(2,2,5,5));
pan2.setLayout(newBorderLayout(3,3));
pan3.setLayout(newGridLayout(1,2,5,5));
frame.setSize(400,450);
//定义各个组件
JLabellb1=newJLabel("HostAddress");
JLabellb2=newJLabel("PortNumber");
JLabellb3=newJLabel("PortStatus");
finalJTextFieldjf1=newJTextField();
finalJTextFieldjf2=newJTextField();
finalJTextAreaja=newJTextArea();
JButtonjb1=newJButton("TCPScan");
JButtonjb2=newJButton("UDPScan");
JScrollPanejp=newJScrollPane(ja);
pan1.add(lb1);
pan1.add(lb2);
pan1.add(jf1);
pan1.add(jf2);
pan2.add(lb3,BorderLayout.NORTH);
pan2.add(jp,BorderLayout.CENTER);
pan3.add(jb1);
pan3.add(jb2);
frame.add(pan1,BorderLayout.NORTH);
frame.add(pan2,BorderLayout.CENTER);
frame.add(pan3,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
jb1.addActionListener(newSocketPort());
classSocketPortimplementsActionListener{
privateStringip=jf1.getText();;
privateStringhostname=newString();
publicvoidactionPerformed(ActionEvente){
try{
InetAddressaddress=InetAddress.getByName(ip);
hostname=address.getHostName();
ja.setText(hostname);
}catch(Exceptionexception){
ja.setText("Couldnotfind"+ip);
}
try{
PrintWriterfout=newPrintWriter(newFileWriter("PortInf.txt"));
fout.println("InformationOfThePortOnthe"+hostname+"computer");
fout.println();
ja.setText("InformationOfThePortOnthe"+hostname+"computer");
for(intnport=25;nport<27;++nport){
try{
Sockets=newSocket(hostname,nport);
fout.println("Theport"+nport+"isopen!
");
fout.println("Connectedto"+s.getInetAddress()+"onport"+s.getPort()+"fromport"+s.getLocalPort()+"of"+s.getLocalAddress());
ja.setText("Theport"+nport+"isopen!
");
ja.setText("Connectedto"+s.getInetAddress()+"onport"+s.getPort()+"fromport"+s.getLocalPort()+"of"+s.getLocalAddress());
}catch(Exceptionexception){
//TODO:
handleexception
fout.println("Theport"+nport+"isclosed!
");
ja.setText("Theport"+nport+"isclosed!
");
}
}
fout.close();
}catch(Exceptionexception){
exception.printStackTrace();
}
}
}
}
}
5、课程设计结果截图:
6、课程设计总结
课题三:
捕获分析IP数据包
1、课程设计目的:
1.掌握IP数据报的格式;
2.理解IP协议的工作原理及工作过程;
3.学会网络编程的方法和技巧。
2、课程设计环境:
1.windowsXP或win7系统;
2.以太网,可以访问外部网页;
3.VC程序编辑器。
3、课程设计原理:
IP数据包的格式说明IP数据包格式包含了标头固定部分,标头可变部分和数据区三部分。
IP数据报标头部分固定为20个字节,其中包含了12个参数域,各参数域隐含着网间协议的传输机制。
IP具体的标头格式如图所示。
4、课程设计内容:
本设计的目标是捕获网络中的IP数据包,解析数据包的内容,将结果显示在标准输出上,并同时写入日志文件。
程序的具体要求如下:
(1)捕获网络中的IP数据包,解析数据包的内容,显示结果,并将结果写入日志文件。
(2)显示的内容包括:
捕获的IP包的版本、头长度、服务类型、数据包总长度、数据包标识、分段标志、分段偏移值、生存时间、上层协议类型、头校验和、源IP地址和目的IP地址等内容。
(3)设置停止标志,当程序接收到停止命令时即停止。
代码:
#include
#pragmacomment(lib,"Ws2_32.lib");
structether_header
{
u_int8_tether_dhost[6];
u_int8_tether_shost[6];
u_int16_tether_type;
};
typedefu_int32_tin_addr_t;
/*structin_addr
{
in_addr_ts_addr;
};*/
structip_header
{
#ifdefWORDS_BIGENDIAN
u_int8_tip_version:
4;
u_int8_tip_header_length:
4;
#else
u_int8_tip_header_length:
4;
u_int8_tip_version:
4;
#endif
u_int8_tip_tos;
u_int16_tip_length;
u_int16_tip_id;
u_int16_tip_off;
u_int8_tip_ttl;
u_int8_tip_protocol;
u_int16_tip_checksum;
structin_addrip_source_address;
structin_addrip_destination_address;
};
voidip_protocol_packet_callback(u_char*argument,conststructpcap_pkthdr*packet_header,
constu_char*packet_content)
{
structip_header*ip_protocol;
u_intheader_length;
u_intoffset;
u_chartos;
u_int16_tchecksum;
ip_protocol=(structip_header*)(packet_content+14);
checksum=ntohs(ip_protocol->ip_checksum);
header_length=ip_protocol->ip_header_length*4;
tos=ip_protocol->ip_tos;
offset=ntohs(ip_protocol->ip_off);
printf("-------------------------ip协议包--------------------\n");
printf("版本:
%d\n",ip_protocol->ip_version);
printf("首部长度:
%d\n",header_length);
printf("服务类型:
%d\n",tos);
printf("总长度:
%d\n",ntohs(ip_protocol->ip_length));
printf("标识:
%d\n",ntohs(ip_protocol->ip_id));
printf("偏移:
%d\n",(offset&0x1fff)*8);
printf("生存时间:
%d\n",ip_protocol->ip_ttl);
printf("协议:
%d\n",ip_protocol->ip_protocol);
switch(ip_protocol->ip_protocol)
{
case6:
printf("该数据包协议类型是Tcp\n");
break;
case17:
printf("该数据包协议类型是Udp\n");
break;
case1:
printf("该数据包协议类型是Icmp\n");
break;
default:
break;
}
printf("校验和:
%d\n",checksum);
printf("源地址:
%s\n",inet_ntoa(ip_protocol->ip_source_address));
printf("目的地址:
%s\n",inet_ntoa(ip_protocol->ip_destination_address));
}
voidethernet_protocol_packet_callback(u_char*argument,conststructpcap_pkthdr*packet_header,
constu_char*packet_content)
{
u_shortethernet_type;
structether_header*ethernet_protocol;
u_char*mac_string;
staticintpacket_number=1;
printf("****************************************************************\n");
printf("the%dpacketiscaptured\n",packet_number);
printf("------------------------------以太网帧--------------------------\n");
ethernet_protocol=(structether_header*)packet_content;
printf("以太网帧类型:
");
ethernet_type=ntohs(ethernet_protocol->ether_type);
printf("%04x\n",ethernet_type);
switch(ethernet_type)
{
case0x0800:
printf("网络层协议是ip协议\n");
break;
case0x0806:
printf("网络层协议是arp协议\n");
break;
case0x8035:
printf("网络层协议是rarp协议\n");
break;
default:
break;
}
printf("MAC源地址:
");
mac_string=ethernet_protocol->ether_shost;
printf("%02x:
%02x:
%02x:
%02x:
%02x:
%02x\n",*mac_string,*(mac_string+1),*(mac_string+2),
*(mac_string+3),*(mac_string+4),*(mac_string+5));
printf("MAC目的地址:
");
mac_string=ethernet_protocol->ether_dhost;
printf("%02x:
%02x: