首页 > 领导讲话 > 企业讲话 / 正文
计算机网络编程课程设计
2020-12-30 17:34:04 ℃ 课题一:基于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的应用程序能够在任何网络软件供应商的符合Windows Sockets协议的实现上工作。因此这份规范定义了应用程序开发者能够使用,并且网络软件供应商能够实现的一套库函数调用和相关语义。
遵守这套Windows Sockets规范的网络软件,我们称之为Windows Sockets兼容的,而Windows Sockets兼容实现的提供者,我们称之为Windows Sockets提供者。一个网络软件供应商必须百分之百地实现Windows Sockets规范才能做到现Windows Sockets兼容。
四、 课程设计内容:
(1) 网络程序初始化,服务器和客户端WinSock API均要求在调用其他WinSock函数以前先调用WSAStartUp函数初始化。
(2)创建套接字Socket()。
(3)配置并启动套接字。
(4)通过Socket发送和接收数据。
(5)程序结束时必须关闭Socket,使用与WSAStartUp()相对应的函数WSACleanUp(),释放所分配的内部缓冲区和其他资源。
代码:
EchoThreadServer: import java.net.*; public class EchoThreadServer {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ServerSocket server = 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();
} } EchoThread: import java.io.*; import java.net.Socket; public class EchoThread implements Runnable{
private 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.getOutputStream());
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(str)) {
flag = false;
}
else {
out.println(“Echo:“ + str);
}
}
client.close();
} catch (Exception e) {
// TODO: handle exception
}
} } EchoClient: import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; public class EchoClient {
public static void main(String[] args) throws Exception{
Socket 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.getOutputStream());
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. 加深对课堂讲授知识的理解; 2. 熟练的掌握基本的网络编程技术和方法; 3. 建立网络编程整体概念; 4. 培养具有研究、设计、编制和调试网络程序的能力。
二、 课程设计环境:
1.windows XP或 win7 系统; 2.配置有java虚拟机的环境变量; 3.编写java程序的软件Eclipse。
三、 课程设计原理: 1. 端口扫描器功能简介:服务器上所开放的端口就是潜在的通信通道,也就是一个入 侵通道。对目标计算机进行端口扫描,能得到许多有用的信息,进行端口扫描的方法很多,可以是手工进行扫描、也可以用端口扫描软件进行。扫描器通过选用远程TCP/IP不同的端口的服务,并记录目标给予的回答,通过这种方法可以搜集到很多关于目标主机的各种有用的信息,例如远程系统是否支持匿名登陆、是否存在可写的 FTP 目录、是否开放 TELNET 服务和 HTTPD 服务等。
2.实验所用的端口扫描技术:端口扫描技术有 TCP connect()扫描、 TCP SYN 扫描、TCP FIN 扫描、IP段扫描等等。本次实验所用的技术是TCP connect()扫描,这是最基本的TCP扫描,操作系统提供的 connect()系统调用可以用来与每一个感兴趣的目标计算机的端口进行连接。如果端口处于侦听状态,那么connect()就能成功。否则,这个端口是不能用的,即 没有提供服务。这个技术的一个最大的优点是,你不需要任何权限。系统中的任何用户都有权利使用这个调用。
四、 课程设计内容:
编写一个端口扫描程序,能够显示某个IP或某段IP的计算机的某一个或某些端口是否正在工作。基本工作过程如下:
(1) 设定好一定的端口扫描范围; (2) 设定每个端口扫描的次数,因为有可能有的端口一次扫描可能不通;
(3) 创建 socket,通过socket的connect方法来连接远程IP地址以及对应的端口;
(4)如果返回false,表示端口没有开放,否则端口开放。
实现代码:
package com.han.socket;
import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.InetAddress; import java.net.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; public 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.setLayout(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 = 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,BorderLayout.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 actionPerformed(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 {
PrintWriter 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.println(“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() + “ of “ + s.getLocalAddress());
} catch (Exception exception) {
// TODO: handle exception
fout.println(“The port “ + nport + “ is closed!“);
ja.setText(“The port “ + nport + “ is closed!“);
}
}
fout.close();
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
} }
五、 课程设计结果截图:
六、
七、 课程设计总结
课题三:捕获分析IP数据包 一、 课程设计目的:
1. 掌握IP数据报的格式; 2. 理解IP协议的工作原理及工作过程; 3. 学会网络编程的方法和技巧。
二、 课程设计环境:
1. windows XP或 win7 系统; 2. 以太网,可以访问外部网页; 3. VC程序编辑器。
三、 课程设计原理:
IP 数据包的格式说明IP数据包格式包含了标头固定部分,标头可变部分和数据区三部分。IP数据报标头部分固定为20个字节,其中包含了12个参数域,各参数域隐含着网间协议的传输机制。IP具体的标头格式如图所示。
四、 课程设计内容:
本设计的目标是捕获网络中的IP数据包,解析数据包的内容,将结果显示在标准输出上,并同时写入日志文件。
程序的具体要求如下: (1)捕获网络中的IP数据包,解析数据包的内容,显示结果,并将结果写入日志文件。
(2)显示的内容包括:捕获的IP包的版本、头长度、服务类型、数据包总长度、数据包标识、分段标志、分段偏移值、生存时间、上层协议类型、头校验和、源IP地址和目的IP地址等内容。
(3)设置停止标志,当程序接收到停止命令时即停止。
代码:
#include <pcap.h> #pragma comment( lib, “Ws2_32.lib“ ); struct ether_header {
u_int8_t ether_dhost[6];
u_int8_t ether_shost[6];
u_int16_t ether_type; };
typedef u_int32_t in_addr_t; /*struct in_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 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 checksum;
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(“版本 :%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)
{
case 6:
printf(“该数据包协议类型是 Tcp\n“);
break;
case 17:
printf(“该数据包协议类型是 Udp\n“);
break;
case 1:
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)); } void ethernet_protocol_packet_callback(u_char *argument,const struct 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_content;
printf(“以太网帧类型:“);
ethernet_type=ntohs(ethernet_protocol->ether_type);
printf(“%04x\n“,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_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:%02x:%02x:%02x:%02x\n“,*mac_string,*(mac_string+1),*(mac_string+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_content[PCAP_ERRBUF_SIZE];
char *net_interface;
struct 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,&bpf_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版.北京:电子工业出版社,2008. [2]张基温.Visual C++程序开发基础[M].北京:高等教育出版社,2001. [3]f15瑞.Visual c++网络通信程序开发指南[M].北京:机械工业出版社,2004.
- 上一篇:2021学习民法典心得体会,(10)
- 下一篇:2020年秋季幼儿园园务工作总结
猜你喜欢
- 2023-11-02 2023年企业的暑期实践总结4篇
- 2023-11-01 有关企业诚信承诺书5篇
- 2023-10-30 企业调研报告1100字7篇
- 2023-10-24 企业中层干部的个人工作总结12篇
- 2023-10-24 2023年供热企业绩效考核总结5篇(范文推荐)
- 2023-10-19 企业致客户的感谢信9篇(精选文档)
- 2023-10-16 企业行政部门年度工作总结4篇(范文推荐)
- 2023-10-10 2023年企业的暑期实践总结4篇
- 2023-09-26 2023年度有关在企业的实习报告7篇
- 2023-08-26 企业单位讣告3篇(范例推荐)
- 搜索
-
- 【辈分】胡家仙家谱全部名字 01-19
- 实现伟大中国梦,我们应该怎么做 06-05
- 学习2020第三次中央新疆工作座谈会精神 10-15
- 国家开放大学电大专科《品牌管理》2021 11-14
- 2020基层党员干部学习中央第三次新疆工 11-14
- 最新党风廉政以违纪违法典型案例“以案 03-12
- 2020年关于节约粮食的倡议书六篇 10-05
- 14篇学习新民主主义革命时期历史发言提 04-28
- 党校小组鉴定评语100字_班主任评语大全 11-27
- 防范打击电信网络诈骗宣传工作方案 08-05
- 11-25国庆70周年庆典晚会 庆典晚会串词
- 11-25办公室礼仪的十大原则 浅谈办公室的电话礼仪
- 01-17用心灵轻轻地歌唱_心灵的歌唱
- 01-17也许你不是我一生的唯一|也许不是我
- 01-17爱了,请珍惜;不爱,趁早放手|爱就珍惜不爱就放手
- 01-17岁月带走的是记忆,但回忆会越来越清晰|有趣又有深意的句子
- 01-17曾经的美好只是曾经,我只想珍惜身边的人|我只想珍惜你
- 01-18从容不惊 [学会笑眼去看世界,不惊不乍,淡定从容]
- 02-03当代大学生学习态度调查报告
- 02-03常用护患英语会话
- 标签列表