UDP实现以太网帧封装模拟(第一天)

本贴最后更新于 3137 天前,其中的信息可能已经斗转星移

又一个课程项目,计算机网络的,要求模拟帧的封装与解封帧,

一、设计要求

1. 编写程序,从文件中读取数据;

2. 将数据组装成IEEE802.3格式的帧;

3. 通过两台计算机的通信,把帧从一台计算机传输到另一台计算机。

二、实现功能

1. 设计界面,打开文件,将数据封装成一个或若干个帧,显示帧的各个字段(数据段可不显示)。

2. 通过网络通信或串口通信,将帧发送到另一台计算机。

3. 接收方对帧进行解析,显示帧的各个字段。

4. 将数据存储到文件中,并与发送方的文件进行比较,检验通信的正确性。

三、代码实现

采用的编程语言是java,利用java良好的GUI界面实现用户操作输入数据,采用UDP通信。

用5个类来实现这次项目。

  1. 首先第一个类是GUI界面布局以及事件处理
package com.liuxin.wl;

import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.*;

public class MyFrame extends JFrame
{
private int serverPort;
public static InetAddress local;
public static UdpServerSocket udp;
private FileReader fread;
private BufferedReader breader;
private FileWriter fwiter;
private BufferedWriter bwiter;
// 布局第一行
private JLabel row1Label=new JLabel("选择文件");
private JButton row1Btn=new JButton("打开文件");
// 布局第二行
private JLabel row2Label=new JLabel("发送内容");
private JTextArea row2Textarea=new JTextArea();
// 布局第三行
private JLabel row3Label=new JLabel("本地 IP");
private JTextField row3LocalIp=new JTextField();
// 布局第四行
private JLabel row3Label1=new JLabel("端口");
private JTextField row3LocalPort=new JTextField();
// 布局第五行
private JLabel row4Label=new JLabel("目的地 IP");
private JTextField row4ToIp=new JTextField();
// 布局第六行
private JLabel row4Label1=new JLabel("端口");
private JTextField row4ToPort=new JTextField();
// 布局第七行
private JButton row5BtnSend=new JButton("发送");
private JButton row5Btnexit=new JButton("关闭退出");
private JLabel row6Label=new JLabel("接受信息");
private JLabel row7Label=new JLabel("接受内容");
private JTextField row6ReceiveInfo=new JTextField();
private JTextArea row7TextArea=new JTextArea();
private JTextArea concel=new JTextArea();
FileDialog openFileDialog = new FileDialog(this,"打开文件",FileDialog.LOAD);
public MyFrame(){
super("网络通信实例");
addWindowListener(new MyWin(this));
setSize(500, 700);
GridLayout layout=new GridLayout(7, 2, 10, 10);
setLayout(null);
FlowLayout layout1=new FlowLayout(FlowLayout.CENTER,10,10);
// 第一行添加到总布局之中
row1Label.setBounds(20, 10, 100, 20);
row1Btn.setBounds(110, 10, 100, 20);
// 第二行
row2Label.setBounds(20, 60, 100, 20);
row2Textarea.setBounds(110, 50, 300, 100);
row2Textarea.setColumns(20);
row2Textarea.setLineWrap(true);
row2Textarea.setWrapStyleWord(true);
row2Textarea.setCaretPosition(row2Textarea.getText().length());
// 第三行
row3Label.setBounds(20, 170, 100, 30);
row3LocalIp.setBounds(110, 170, 200,30);
row3Label1.setBounds(320, 170, 30, 30);
row3LocalPort.setBounds(360, 170, 50, 30);
// 第四行
row4Label.setBounds(20, 210, 100, 30);
row4ToIp.setBounds(110, 210, 200,30);
row4Label1.setBounds(320, 210, 30, 30);
row4ToPort.setBounds(360, 210, 50, 30);
row5BtnSend.setBounds(320, 250, 100, 30);
row5Btnexit.setBounds(120, 250, 100, 30);
add(row5Btnexit);
add(row1Label);
add(row1Btn);
add(row2Label);
add(row2Textarea);
add(row3Label);
add(row3LocalIp);
add(row3Label1);
add(row3LocalPort);
add(row4Label);
add(row4ToIp);
add(row4Label1);
add(row4ToPort);
add(row5BtnSend);
// 接受队列
row6Label.setBounds(20, 290, 100, 30);
add(row6Label);

	row6ReceiveInfo.setBounds(110, 290, 300, 30);
	add(row6ReceiveInfo);
	
	
	row7Label.setBounds(20, 330, 100, 30);
	add(row7Label);
	
	row7TextArea.setBounds(110, 330, 300, 100);
	add(row7TextArea);
	concel.setBounds(110, 470, 300, 100);
	add(concel);
	setVisible(true);

// 关闭退出
row5Btnexit.addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			udp.isReceive=false;
			udp.close();
			System.exit(0);
		}
	});

// 发送
row5BtnSend.addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("判断参数");
			if(utility.EmptyString(row4ToIp.getText())||utility.EmptyString(row4ToPort.getText())){
				concel.setText("参数为空!");
			}
			else {
				concel.setText("正在发送……");
				InetAddress to;
				try {

// InetAddress.

					to = InetAddress.getByName( row4ToIp.getText());
					System.out.println(to.getHostAddress());
					udp.send(row2Textarea.getText(),to, Integer.valueOf(row4ToPort.getText()));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					concel.setText("发送抛出异常!");
				}
				concel.setText("发送成功!");
			}
		}
	});
	//		打开文件
	row1Btn.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			openFileDialog.setVisible(true);
			if(openFileDialog.getFile()!=null)
			{
				File file=new File(openFileDialog.getDirectory(),openFileDialog.getFile());
				try {
					fread=new FileReader(file);
					breader=new BufferedReader(fread);
					String s;
					StringBuffer sb=new StringBuffer();
					while((s=breader.readLine())!=null)
					{
						sb.append(s+"\n");
						System.out.println(s+"\n");
					}
					row2Textarea.setText(sb.toString());
					breader.close();
					fread.close();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
	});
}
public static void main(String args[]){
	MyFrame f=new MyFrame();
	try {
		local=InetAddress.getLocalHost();
		f.udp = new UdpServerSocket(local.getHostAddress(), 8001);
		f.row3LocalIp.setText(local.getHostAddress());
		f.row3LocalPort.setText("8001");
		while (UdpServerSocket.isReceive) {  
			if(f.udp.isReceive)
            f.row7TextArea.setText(f.row7TextArea.getText()+f.udp.receive()+"\n");    	                
        }    
	} catch (Exception e) {
		// TODO Auto-generated catch block
		System.out.println("接受报错");
		e.printStackTrace();
	}
	
}

}
该类主要实现 GUI 的界面布局以及相应事件处理。用户可以选择打开电脑中的某一个文件或者手动输入需要发送的内容,然后输入目的地的 IP 和端口号,有一个按钮选择发送。
下方有一个窗口部分用来显示接收到的数据。

 

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...