课题:基TCPSocket通讯编程
课程设计目:
1 够深刻解socket编程思想
2 实现层面理解TCPUDP链接
二 课程设计环境:
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)程序结束时必须关闭SocketWSAStartUp()相应函数WSACleanUp()释放分配部缓区资源
代码:
EchoThreadServer
import javanet*
public class EchoThreadServer {
public static void main(String[] args) throws Exception{
TODO Autogenerated method stub
ServerSocket server null
Socket client null
InetAddress remAdd null
server new ServerSocket(12345)
boolean f true
while (f) {
Systemoutprintln(连接正建立请等……)
client serveraccept()
Systemoutprintln(客户端IP址端口号 + clientgetLocalSocketAddress())
new Thread(new EchoThread(client))start()
}
serverclose()
}
}
EchoThread
import javaio*
import javanetSocket
public class EchoThread implements Runnable{
private Socket client null
public EchoThread(Socket client)
{
thisclient client
}
public void run()
{
BufferedReader buf null
PrintStream out null
BufferedReader input null
try {
out new PrintStream(clientgetOutputStream())
buf new BufferedReader(new InputStreamReader(clientgetInputStream()))
input new BufferedReader(new InputStreamReader(Systemin))
boolean flag true
while(flag)
{
String str bufreadLine()
Systemoutprintln(Client + str)
if (str null || equals(str)) {
flag false
}
else if(goodbyeequals(str)) {
flag false
}
else {
outprintln(Echo + str)
}
}
clientclose()
} catch (Exception e) {
TODO handle exception
}
}
}
EchoClient
import javaioBufferedReader
import javaioInputStreamReader
import javaioPrintStream
import javanetSocket
public class EchoClient {
public static void main(String[] args) throws Exception{
Socket client null
client new Socket(localhost12345)
BufferedReader buf null
PrintStream out null
BufferedReader input null
input new BufferedReader(new InputStreamReader(Systemin))
buf new BufferedReader(new InputStreamReader(clientgetInputStream()))
out new PrintStream(clientgetOutputStream())
boolean flag true
while(flag){
Systemoutprintln(客户端输入信息)
String str inputreadLine()
outprintln(str)
if (goodbyeequals(str)) {
flag false
}
else {
String echo bufreadLine()
Systemoutprintln(echo)
}
}
bufclose()
clientclose()
}
}
五 课程设计结果截图:
服务器端截图:
客户端截图:
六 课程设计总结:
课题二:端口扫描
课程设计目:
1 加深课堂讲授知识理解
2 熟练掌握基网络编程技术方法
3 建立网络编程整体概念
4 培养具研究设计编制调试网络程序力
二 课程设计环境:
1windows XP win7 系统
2配置java虚拟机环境变量
3编写java程序软件Eclipse
三 课程设计原理
1. 端口扫描器功简介:服务器开放端口潜通信通道入 侵通道目标计算机进行端口扫描许信息进行端口扫描方法手工进行扫描端口扫描软件进行扫描器通选远程TCPIP端口服务记录目标予回答通种方法搜集关目标机种信息例远程系统否支持匿名登陆否存写 FTP 目录否开放 TELNET 服务 HTTPD 服务等
2.实验端口扫描技术:端口扫描技术 TCP connect()扫描 TCP SYN 扫描TCP FIN 扫描IP段扫描等等次实验技术TCP connect()扫描基TCP扫描操作系统提供 connect()系统调感兴趣目标计算机端口进行连接果端口处侦听状态connect()成功否端口 没提供服务技术优点需权限系统中户权利调
四 课程设计容:
编写端口扫描程序够显示某IP某段IP计算机某某端口否正工作基工作程:
(1) 设定定端口扫描范围
(2) 设定端口扫描次数端口次扫描通
(3) 创建 socket通socketconnect方法连接远程IP址应端口
(4)果返回false表示端口没开放否端口开放
实现代码:
package comhansocket
import javaawtBorderLayout
import javaawtGridLayout
import javaawteventActionEvent
import javaawteventActionListener
import javanetInetAddress
import javanetSocket
import javaxswingJButton
import javaxswingJFrame
import javaxswingJLabel
import javaxswingJPanel
import javaxswingJScrollPane
import javaxswingJTextArea
import javaxswingJTextField
public class SocketView {
public static void main(String[] args) {
JFrame frame new JFrame(机端口扫描程序)
framesetLayout(new BorderLayout(33))
JPanel pan1 new JPanel()
JPanel pan2 new JPanel()
JPanel pan3 new JPanel()
pan1setLayout(new GridLayout(2255))
pan2setLayout(new BorderLayout(33))
pan3setLayout(new GridLayout(1255))
framesetSize(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)
pan1add(lb1)
pan1add(lb2)
pan1add(jf1)
pan1add(jf2)
pan2add(lb3BorderLayoutNORTH)
pan2add(jpBorderLayoutCENTER)
pan3add(jb1)
pan3add(jb2)
frameadd(pan1BorderLayoutNORTH)
frameadd(pan2BorderLayoutCENTER)
frameadd(pan3BorderLayoutSOUTH)
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)
framesetVisible(true)
jb1addActionListener(new SocketPort())
class SocketPort implements ActionListener {
private String ip jf1getText()
private String hostname new String()
public void actionPerformed(ActionEvent e) {
try {
InetAddress address InetAddressgetByName(ip)
Systemoutprintln(address)
hostname addressgetHostName()
Systemoutprintln(hostname)
jasetText(hostname)
} catch (Exception exception) {
Systemoutprintln(Could not find + ip)
jasetText(Could not find + ip)
}
try {
PrintWriter fout new PrintWriter( new FileWriter(PortInftxt))
foutprintln(Information Of The Port On the + hostname +computer )
foutprintln()
jasetText(Information Of The Port On the + hostname +computer)
for(int nport 25nport < 27++nport){
try {
Socket s new Socket(hostnamenport)
foutprintln(The port + nport + is open)
foutprintln(Connected to + sgetInetAddress() + on port + sgetPort() + from port + sgetLocalPort() + of + sgetLocalAddress())
jasetText(The port + nport + is open)
jasetText(Connected to + sgetInetAddress() + on port + sgetPort() + from port + sgetLocalPort() + of + sgetLocalAddress())
} catch (Exception exception) {
TODO handle exception
foutprintln(The port + nport + is closed)
jasetText(The port + nport + is closed)
}
}
foutclose()
} catch (Exception exception) {
exceptionprintStackTrace()
}
}
}
}
}
五 课程设计结果截图:
六
七 课程设计总结
课题三:捕获分析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
#pragma comment( lib Ws2_32lib )
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_version4
u_int8_t ip_header_length4
#else
u_int8_t ip_header_length4
u_int8_t ip_version4
#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 * argumentconst 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)
checksumntohs(ip_protocol>ip_checksum)
header_lengthip_protocol>ip_header_length*4
tosip_protocol>ip_tos
offsetntohs(ip_protocol>ip_off)
printf(ip协议包\n)
printf(版 d\nip_protocol>ip_version)
printf(首部长度d\nheader_length)
printf(服务类型 d\ntos)
printf(总长度 d\nntohs(ip_protocol>ip_length))
printf(标识 d\nntohs(ip_protocol>ip_id))
printf(偏移d\n(offset&0x1fff)*8)
printf(生存时间d\nip_protocol>ip_ttl)
printf(协议d\nip_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\nchecksum)
printf(源址 s\ninet_ntoa(ip_protocol>ip_source_address))
printf(目址 s\ninet_ntoa(ip_protocol>ip_destination_address))
}
void ethernet_protocol_packet_callback(u_char *argumentconst 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_number1
printf(****************************************************************\n)
printf( the d packet is captured \npacket_number)
printf(太网帧\n)
ethernet_protocol(struct ether_header *)packet_content
printf(太网帧类型)
ethernet_typentohs(ethernet_protocol>ether_type)
printf(04x\nethernet_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_stringethernet_protocol>ether_shost
printf(02x02x02x02x02x02x\n*mac_string*(mac_string+1)*(mac_string+2)
*(mac_string+3)*(mac_string+4)*(mac_string+5))
printf(MAC目址 )
mac_stringethernet_protocol>ether_dhost
printf(02x02x02x02x02x02x\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(argumentpacket_headerpacket_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_interfacepcap_lookupdev(error_content)
pcap_lookupnet(net_interface&net_ip&net_maskerror_content)
pcap_handlepcap_open_live(net_interface2048010error_content)
pcap_compile(pcap_handle&bpf_filterbpf_filter_string0net_ip)
pcap_setfilter(pcap_handle&bpf_filter)
if(pcap_datalink(pcap_handle)DLT_EN10MB)
return 0
pcap_loop(pcap_handle1ethernet_protocol_packet_callbackNULL)
pcap_close(pcap_handle)
}
五 课程设计结果截图:
六 课程设计总结:
七 参考资料:
[1]谢希仁.计算机网络[M].第5版.北京:电子工业出版社2008.
[2]张基温.Visual C++程序开发基础[M].北京:高等教育出版社2001.
[3]f15瑞.Visual c++网络通信程序开发指南[M].北京:机械工业出版社2004.
文档香网(httpswwwxiangdangnet)户传
《香当网》用户分享的内容,不代表《香当网》观点或立场,请自行判断内容的真实性和可靠性!
该内容是文档的文本内容,更好的格式请下载文档