计算机网络编程课程设计报告书.doc

上传人:牧羊曲112 文档编号:4297091 上传时间:2023-04-14 格式:DOC 页数:14 大小:270.50KB
返回 下载 相关 举报
计算机网络编程课程设计报告书.doc_第1页
第1页 / 共14页
计算机网络编程课程设计报告书.doc_第2页
第2页 / 共14页
计算机网络编程课程设计报告书.doc_第3页
第3页 / 共14页
计算机网络编程课程设计报告书.doc_第4页
第4页 / 共14页
计算机网络编程课程设计报告书.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《计算机网络编程课程设计报告书.doc》由会员分享,可在线阅读,更多相关《计算机网络编程课程设计报告书.doc(14页珍藏版)》请在三一办公上搜索。

1、课题一:基于TCP的Socket通讯编程一、 课程设计目的:1. 能够深刻了解socket编程思想;2. 从实现层面理解TCP和UDP的不同。二、 课程设计环境:1. windows XP或 win7 系统;2. 配置有java虚拟机的环境变量;3. 编写java程序的软件Eclipse。三、 课程设计原理: Windows Sockets规本意在于提供给应用程序开发者一套简单的API,并让各家网络软件供应商共同遵守。此外,在一个特定版本Windows的基础上,Windows Sockets也定义了一个二进制接口(ABI),以此来保证应用Windows Sockets API的应用程序能够在任

2、何网络软件供应商的符合Windows Sockets协议的实现上工作。因此这份规定义了应用程序开发者能够使用,并且网络软件供应商能够实现的一套库函数调用和相关语义。 遵守这套Windows Sockets规的网络软件,我们称之为Windows Sockets兼容的,而Windows Sockets兼容实现的提供者,我们称之为Windows Sockets提供者。一个网络软件供应商必须百分之百地实现Windows Sockets规才能做到现Windows Sockets兼容。四、 课程设计容:(1) 网络程序初始化,服务器和客户端WinSock API均要求在调用其他WinSock函数以前先调用

3、WSAStartUp函数初始化。 (2)创建套接字Socket()。 (3)配置并启动套接字。 (4)通过Socket发送和接收数据。 (5)程序结束时必须关闭Socket,使用与WSAStartUp()相对应的函数WSACleanUp(),释放所分配的部缓冲区和其他资源。代码:EchoThreadServer:import .*;public class EchoThreadServer public static void main(String args) throws Exception/ TODO Auto-generated method stubServerSocket serv

4、er = null;Socket client = null;InetAddress remAdd = null;server = new ServerSocket(12345);boolean f = true;while (f) System.out.println(连接正在建立,请等待);client = server.accept();System.out.println(客户端的IP地址和端口号是: + client.getLocalSocketAddress();new Thread(new EchoThread(client).start();server.close();Ech

5、oThread:import java.io.*;import .Socket;public class EchoThread implements Runnableprivate Socket client = null;public EchoThread(Socket client)this.client = client;public void run()BufferedReader buf = null;PrintStream out = null;BufferedReader input = null;try out = new PrintStream(client.getOutpu

6、tStream();buf = new BufferedReader(new InputStreamReader(client.getInputStream();input = new BufferedReader(new InputStreamReader(System.in);boolean flag = true ;while(flag)String str = buf.readLine();System.out.println(Client: + str);if (str = null | .equals(str) flag = false;else if(goodbye.equals

7、(str) flag = false;else out.println(Echo: + str);client.close(); catch (Exception e) / TODO: handle exceptionEchoClient:import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintStream;import .Socket;public class EchoClient public static void main(String args) throws Excepti

8、onSocket client = null;client = new Socket(localhost,12345);BufferedReader buf = null;PrintStream out = null;BufferedReader input = null;input = new BufferedReader(new InputStreamReader(System.in);buf = new BufferedReader(new InputStreamReader(client.getInputStream();out = new PrintStream(client.get

9、OutputStream();boolean flag = true;while(flag)System.out.println(客户端输入信息:);String str = input.readLine(); out.println(str);if (goodbye.equals(str) flag = false;else String echo = buf.readLine();System.out.println(echo);buf.close();client.close();五、 课程设计结果截图:服务器端截图:客户端截图:六、 课程设计总结:课题二:端口扫描一、 课程设计目的:1

10、. 加深对课堂讲授知识的理解;2. 熟练的掌握基本的网络编程技术和方法;3. 建立网络编程整体概念;4. 培养具有研究、设计、编制和调试网络程序的能力。二、 课程设计环境:1.windows XP或 win7 系统;2.配置有java虚拟机的环境变量;3.编写java程序的软件Eclipse。三、 课程设计原理:1 端口扫描器功能简介:服务器上所开放的端口就是潜在的通信通道,也就是一个入 侵通道。对目标计算机进行端口扫描,能得到许多有用的信息,进行端口扫描的方法很多,可以是手工进行扫描、也可以用端口扫描软件进行。扫描器通过选用远程TCP/IP不同的端口的服务,并记录目标给予的回答,通过这种方法

11、可以搜集到很多关于目标主机的各种有用的信息,例如远程系统是否支持匿名登陆、是否存在可写的 FTP 目录、是否开放 TELNET 服务和 HTTPD 服务等。 2实验所用的端口扫描技术:端口扫描技术有 TCP connect()扫描、 TCP SYN 扫描、TCP FIN 扫描、IP段扫描等等。本次实验所用的技术是TCP connect()扫描,这是最基本的TCP扫描,操作系统提供的 connect()系统调用可以用来与每一个感兴趣的目标计算机的端口进行连接。如果端口处于侦听状态,那么connect()就能成功。否则,这个端口是不能用的,即 没有提供服务。这个技术的一个最大的优点是,你不需要任何

12、权限。系统中的任何用户都有权利使用这个调用。四、 课程设计容:编写一个端口扫描程序,能够显示某个IP或某段IP的计算机的某一个或某些端口是否正在工作。基本工作过程如下:(1) 设定好一定的端口扫描围;(2) 设定每个端口扫描的次数,因为有可能有的端口一次扫描可能不通; (3) 创建 socket,通过socket的connect方法来连接远程IP地址以及对应的端口; (4)如果返回false,表示端口没有开放,否则端口开放。实现代码:package .han.socket;import java.awt.BorderLayout;import java.awt.GridLayout;impor

13、t java.awt.event.ActionEvent;import java.awt.event.ActionListener;import .InetAddress;import .Socket;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;publ

14、ic class SocketView public static void main(String args) JFrame frame = new JFrame(主机端口扫描程序);frame.setLayout(new BorderLayout(3,3);JPanel pan1 = new JPanel();JPanel pan2 = new JPanel();JPanel pan3 = new JPanel();pan1.setLayout(new GridLayout(2,2,5,5);pan2.setLayout(new BorderLayout(3,3);pan3.setLayo

15、ut(new GridLayout(1,2,5,5);frame.setSize(400, 450);/定义各个组件JLabel lb1 = new JLabel(Host Address);JLabel lb2 = new JLabel(Port Number);JLabel lb3 = new JLabel(Port Status);final JTextField jf1 = new JTextField();final JTextField jf2 = new JTextField();final JTextArea ja = new JTextArea();JButton jb1 =

16、 new JButton(TCP Scan);JButton jb2 = new JButton(UDP Scan);JScrollPane jp = new JScrollPane(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,Bord

17、erLayout.CENTER);frame.add(pan3,BorderLayout.SOUTH);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);jb1.addActionListener(new SocketPort(); class SocketPort implements ActionListener private String ip = jf1.getText(); private String hostname = new String();public void act

18、ionPerformed(ActionEvent e) try InetAddress address = InetAddress.getByName(ip); System.out.println(address); hostname = address.getHostName(); System.out.println(hostname);ja.setText(hostname); catch (Exception exception) System.out.println(Could not find + ip);ja.setText(Could not find + ip);try P

19、rintWriter fout = new PrintWriter( new FileWriter(PortInf.txt); fout.println(Information Of The Port On the + hostname +computer ); fout.println(); ja.setText(Information Of The Port On the + hostname +computer);for(int nport = 25;nport 27;+nport)try Socket s = new Socket(hostname,nport); fout.print

20、ln(The port + nport + is open!); fout.println(Connected to + s.getInetAddress() + on port + s.getPort() + from port + s.getLocalPort() + of + s.getLocalAddress(); ja.setText(The port + nport + is open!);ja.setText(Connected to + s.getInetAddress() + on port + s.getPort() + from port + s.getLocalPort

21、() + of + s.getLocalAddress(); catch (Exception exception) / TODO: handle exceptionfout.println(The port + nport + is closed!);ja.setText(The port + nport + is closed!);fout.close(); catch (Exception exception) exception.printStackTrace(); 五、 课程设计结果截图:六、七、 课程设计总结课题三:捕获分析IP数据包一、 课程设计目的:1. 掌握IP数据报的格式;

22、2. 理解IP协议的工作原理及工作过程;3. 学会网络编程的方法和技巧。二、 课程设计环境:1. windows XP或 win7 系统;2. 以太网,可以访问外部网页;3. VC程序编辑器。三、 课程设计原理: IP 数据包的格式说明IP数据包格式包含了标头固定部分,标头可变部分和数据区三部分。IP数据报标头部分固定为20个字节,其中包含了12个参数域,各参数域隐含着网间协议的传输机制。IP具体的标头格式如图所示。四、 课程设计容:本设计的目标是捕获网络中的IP数据包,解析数据包的容,将结果显示在标准输出上,并同时写入日志文件。程序的具体要求如下:(1)捕获网络中的IP数据包,解析数据包的容

23、,显示结果,并将结果写入日志文件。(2)显示的容包括:捕获的IP包的版本、头长度、服务类型、数据包总长度、数据包标识、分段标志、分段偏移值、生存时间、上层协议类型、头校验和、源IP地址和目的IP地址等容。(3)设置停止标志,当程序接收到停止命令时即停止。代码:#include #pragma comment( lib, Ws2_32.lib );struct ether_header u_int8_t ether_dhost6; u_int8_t ether_shost6; u_int16_t ether_type;typedef u_int32_t in_addr_t;/*struct in

24、_addr in_addr_t s_addr;*/struct ip_header#ifdef WORDS_BIGENDIAN u_int8_t ip_version:4; u_int8_t ip_header_length:4;#else u_int8_t ip_header_length:4; u_int8_t ip_version:4;#endif u_int8_t ip_tos; u_int16_t ip_length; u_int16_t ip_id; u_int16_t ip_off; u_int8_t ip_ttl; u_int8_t ip_protocol; u_int16_t

25、 ip_checksum; struct in_addr ip_source_address; struct in_addr ip_destination_address;void ip_protocol_packet_callback(u_char * argument,const struct pcap_pkthdr * packet_header, const u_char * packet_content) struct ip_header * ip_protocol; u_int header_length; u_int offset; u_char tos; u_int16_t c

26、hecksum; ip_protocol=(struct ip_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(版本 :%dn,ip_protocol-ip_version); printf(首部长度:%dn,header_length); prin

27、tf(服务类型 :%dn,tos); printf(总长度 :%dn,ntohs(ip_protocol-ip_length); printf(标识 :%dn,ntohs(ip_protocol-ip_id); printf(偏移:%dn,(offset&0x1fff)*8); printf(生存时间:%dn,ip_protocol-ip_ttl); printf(协议:%dn,ip_protocol-ip_protocol); switch(ip_protocol-ip_protocol) case 6: printf(该数据包协议类型是 Tcpn); break; case 17: pri

28、ntf(该数据包协议类型是 Udpn); break; case 1: printf(该数据包协议类型是 Icmpn); break; default: break; printf(校验和:%dn,checksum); printf(源地址 :%sn,inet_ntoa(ip_protocol-ip_source_address); printf(目的地址 :%sn,inet_ntoa(ip_protocol-ip_destination_address);void ethernet_protocol_packet_callback(u_char *argument,const struct

29、pcap_pkthdr * packet_header, const u_char * packet_content) u_short ethernet_type; struct ether_header *ethernet_protocol; u_char *mac_string; static int packet_number=1;printf(*n); printf( the %d packet is captured n,packet_number); printf(-以太网帧-n); ethernet_protocol=(struct ether_header *)packet_c

30、ontent; printf(以太网帧类型:); ethernet_type=ntohs(ethernet_protocol-ether_type); printf(%04xn,ethernet_type); switch(ethernet_type) case 0x0800: printf(网络层协议是 ip 协议n); break; case 0x0806: printf(网络层协议是 arp 协议n); break; case 0x8035: printf(网络层协议是 rarp 协议n); break; default: break; printf(MAC源地址:); mac_stri

31、ng=ethernet_protocol-ether_shost;printf(%02x:%02x:%02x:%02x:%02x:%02xn,*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:%02x:%02x:%02x:%02xn,*mac_string,*(mac_string+1),*(mac_str

32、ing+2), *(mac_string+3),*(mac_string+4),*(mac_string+5); switch(ethernet_type) case 0x0800 ip_protocol_packet_callback(argument,packet_header,packet_content); break; default: break; printf(*n); packet_number+;int main() pcap_t *pcap_handle; char error_contentPCAP_ERRBUF_SIZE; char *net_interface; st

33、ruct bpf_program bpf_filter; char bpf_filter_string=ip; bpf_u_int32 net_mask; bpf_u_int32 net_ip; net_interface=pcap_lookupdev(error_content); pcap_lookupnet(net_interface,&net_ip,&net_mask,error_content); pcap_handle=pcap_open_live(net_interface,20480,1,0,error_content); pcap_compile(pcap_handle,&b

34、pf_filter,bpf_filter_string,0,net_ip); pcap_setfilter(pcap_handle,&bpf_filter); if(pcap_datalink(pcap_handle)!=DLT_EN10MB) return 0; pcap_loop(pcap_handle,-1,ethernet_protocol_packet_callback,NULL); pcap_close(pcap_handle);五、 课程设计结果截图:六、 课程设计总结:七、 主要参考资料:1希仁计算机网络M第5版:电子工业,20082基温Visual C+程序开发基础M:高等教育,20013f15瑞Visual c+网络通信程序开发指南M:机械工业,2004

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号