实验五 基于UDP编程实验

实验五 基于UDP编程
实验目的:
1、理解UDP及基于数据报通信的基本原理;
2、学会基于UDP编程的代码编写及理解各语句内容;
3、掌握基于UDP编程的具体应用。
实验要求:
1、建立两个独立的基于UDP编程的Java工程,用于服务器和客户端系统;
2、建立UDP的JAVA类,并能正确运行且能实现数据报通信;
3、对JAVA类的功能进行拓广,使基于UDP编程用于某一具体的应用 。
3、调试使以上两个工程能正常运行,使服务端和客户端能正常通信。
4、对服务器端和客户端的程序代码进行阅读和理解,要求做到每条语句都能明白其具体含义,每个类都了解其功能。
5、对以上程序进行适当的修改,要求数据报通信功能保持实现。
(注:修改的方向有:图形化界面、多台电脑通信、结合线程编程等。)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package myinteaddress1;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/**
 *
 * @author Administrator
 */
public class UDPChat implements Runnable,ActionListener{
    JTextArea showArea;
    JLabel lbl1,lbl2,lbl3;
    JTextField msgText,sendPortText,receivePortText,IPAddressText;
    JFrame mainiJframe;
    JButton sendBtn,startBtn;
    JScrollPane JSPane;
    JPanel Pane1,Pane2;
    Container con;
    Thread thread=null;
    DatagramPacket sendPack,receivePack;
    DatagramSocket sendSocket,receiveSocket;
    private InetAddress sendIP;
    private int sendPort,receivePort;
    private byte intBuf[],outBuf[];
    public static final int BUFSIZE=1024;
    public UDPChat(){
        mainiJframe=new JFrame("聊天---UDP协议");
        con=mainiJframe.getContentPane();
        showArea=new JTextArea();
        showArea.setEditable(false);
        showArea.setLineWrap(true);
        lbl1=new JLabel("接受端口号");
        lbl2=new JLabel("发送端口号");
        lbl3=new JLabel("对方的地址");
        sendPortText=new JTextField();
        sendPortText.setColumns(5);
        receivePortText=new JTextField();
        receivePortText.setColumns(5);
        IPAddressText=new JTextField();
        IPAddressText.setColumns(8);
        startBtn=new JButton("开始");
        startBtn.addActionListener(this);
        Pane1=new JPanel();
        Pane1.setLayout(new FlowLayout());
        Pane1.add(lbl1);
        Pane1.add(receivePortText);
        Pane1.add(lbl2);
        Pane1.add(sendPortText);
        Pane1.add(lbl3);
        Pane1.add(IPAddressText);
        Pane1.add(startBtn);
        JSPane=new JScrollPane(showArea);
        msgText=new JTextField();
        msgText.setColumns(40);
        msgText.setEditable(false);
        msgText.addActionListener(this);
        sendBtn=new JButton("发送");
        sendBtn.setEnabled(false);
        sendBtn.addActionListener(this);
        Pane2=new JPanel();
        Pane2.add(msgText);
        Pane2.add(sendBtn);
        con.add(Pane1,BorderLayout.NORTH);
        con.add(JSPane,BorderLayout.CENTER);
        con.add(Pane2,BorderLayout.SOUTH);
        mainiJframe.setSize(600,400);
        mainiJframe.setVisible(true);
        mainiJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main (String[] args){
        new UDPChat();
    }


    public void actionPerformed(ActionEvent e) {
        try{
            if(e.getSource()==startBtn)
            {
                intBuf=new byte[BUFSIZE];
                sendPort=Integer.parseInt(sendPortText.getText());
                sendIP=InetAddress.getByName(IPAddressText.getText());
                sendSocket=new DatagramSocket();
                receivePort=Integer.parseInt(receivePortText.getText());
                receivePack=new DatagramPacket(intBuf,BUFSIZE);
                receiveSocket=new DatagramSocket(receivePort);
                thread=new Thread(this);
                thread.setPriority(Thread.MIN_PRIORITY);
                thread.start();
                startBtn.setEnabled(false);
                sendBtn.setEnabled(true);
                msgText.setEditable(true);

            }
            else{
                outBuf=msgText.getText().getBytes();
                sendPack=new DatagramPacket(outBuf,outBuf.length,sendIP,sendPort);
                sendSocket.send(sendPack);
                showArea.append("我说:"+msgText.getText()+"\n");
                msgText.setText(null);
            }
        }catch(UnknownHostException el){
            showArea.append("无法连接到指定的地址\n");
        }catch(SocketException el){
            showArea.append("无法打开指定端口\n");
        }catch(IOException el){
            showArea.append("发送数据失败\n");
        }
    }
    public void run(){
        String msgstr;
        while(true){
            try{
                receiveSocket.receive(receivePack);
                msgstr=new String(receivePack.getData(),0,receivePack.getLength());
                showArea.append("对方说:"+msgstr+"\n");
            }catch(IOException el){
                showArea.append("接受数据出错\n");
            }
        }
    }
}

效果截图

猜你喜欢

转载自blog.csdn.net/u014775977/article/details/51227721