UDP程序設計基礎

UDP程序設計基礎

UDP與TCP不同,基於UDP的信息傳遞更快,但是使用UDP傳遞數據的時候,用戶無法知道數據能否正確的到達主機,也並不能確定到達的目的地的順序是否和發送的順序相同,但是他快啊!!

基本模式

將數據打包(成為數據包),然後然後將數據發往目的地。

接受別人發來的數據包,然後查看數據包

程序步驟

使用DatagramSocket()創建一個數據報套接字

使用DatagramPacket(byte[ ] buf,int offset , int length , InterAddress address , int port ),創建要發送的數據包

使用DatagramSocket類的send方法發送數據包

使用DatagramSocket( int port )創建數據報套接字,綁定到指定的端口

使用DatagramPacket(byte[ ] buf,int offset , int length)創建字節數組來接收數據包

使用DatagramPacket類的receive()方法接受UDP包

DatagramPacket類

用來表示數據包

DatagramPacket(byte[ ] buf , int length )//指定數據包的內存空間和大小

DatagramPacket(byte[ ] buf , int length , InterAddress address , int port)//指定數據包的內存空間和大小還制定了數據包的目的地址和端口。在發送數據時,必須指定接收方的Socket地址和端口號,因此使用這種構造函數可以創建發送數據的DataPacket對象。

DatagramSocket類

用於表示發送和接受數據包的套接字。

DatagramSocket()//構造數據報套接字並將其綁定到本地主機上任意可用的端口

DatagramSocket( int port )///創建數據報套接字並將其綁定到本地主機上指定的端口

DatagramSocket( int port , InterAddress addr)

//創建數據報套接字並將其綁定到指定的本地地址,適用於有多個網卡和多個IP地址的情況

例題

UDP程序設計基礎

<code>import java.net.*;

public class Weather extends Thread {
\tString weather = "josephous handsome";
\tint port = 9898;
\tInetAddress iaddress = null;
\tMulticastSocket socket = null;
\t
\tWeather() {
\t\ttry {
\t\t\tiaddress = InetAddress.getByName("224.255.10.0");
\t\t\tsocket = new MulticastSocket(port);
\t\t\tsocket.setTimeToLive(1);
\t\t\tsocket.joinGroup(iaddress);
\t\t} catch (Exception e) {
\t\t\te.printStackTrace();
\t\t}
\t}
\t
\tpublic void run() {
\t\twhile (true) {
\t\t\tDatagramPacket packet = null;
\t\t\tbyte data[] = weather.getBytes();
\t\t\tpacket = new DatagramPacket(data, data.length, iaddress, port);
\t\t\tSystem.out.println(new String(data));
\t\t\ttry {
\t\t\t\tsocket.send(packet);
\t\t\t\tsleep(3000);
\t\t\t} catch (Exception e) {
\t\t\t\te.printStackTrace();
\t\t\t}
\t\t}
\t}
\tpublic static void main(String[] args) {
\t\tWeather w = new Weather();
\t\tw.start();
\t}
}/<code>
UDP程序設計基礎

UDP程序設計基礎

<code>import java.awt.*;
import java.awt.event.*;
import java.net.*;

import javax.swing.*;

public class Receive extends JFrame implements Runnable, ActionListener {
\t/**
\t *
\t */
\tprivate static final long serialVersionUID = 1L;
\tint port;
\tInetAddress group = null;
\tMulticastSocket socket = null;
\tJButton ince = new JButton("開始接收");
\tJButton stop = new JButton("停止接收");
\tJTextArea inceAr = new JTextArea(10, 10);
\tJTextArea inced = new JTextArea(10, 10);

\tThread thread;
\tboolean b = false;
\t
\tpublic Receive() {
\t\tsuper("廣播數據報");
\t\tsetDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
\t\tthread = new Thread(this);
\t\tince.addActionListener(this);
\t\tstop.addActionListener(this);
\t\tinceAr.setForeground(Color.blue);
\t\tJPanel north = new JPanel();
\t\tnorth.add(ince);
\t\tnorth.add(stop);
\t\tadd(north, BorderLayout.NORTH);
\t\tJPanel center = new JPanel();
\t\tcenter.setLayout(new GridLayout(1, 2));
\t\tcenter.add(inceAr);

\t\tfinal JScrollPane scrollPane = new JScrollPane();
\t\tcenter.add(scrollPane);
\t\tscrollPane.setViewportView(inced);
\t\tadd(center, BorderLayout.CENTER);
\t\tvalidate();
\t\tport = 9898;
\t\ttry {
\t\t\tgroup = InetAddress.getByName("224.255.10.0");
\t\t\tsocket = new MulticastSocket(port);
\t\t\tsocket.joinGroup(group);
\t\t} catch (Exception e) {
\t\t\te.printStackTrace();
\t\t}
\t\tsetBounds(100, 50, 360, 380);
\t\tsetVisible(true);
\t}
\t
\tpublic void run() {
\t\twhile (true) {
\t\t\tbyte data[] = new byte[1024];
\t\t\tDatagramPacket packet = null;
\t\t\tpacket = new DatagramPacket(data, data.length, group, port);
\t\t\ttry {
\t\t\t\tsocket.receive(packet);
\t\t\t\tString message = new String(packet.getData(), 0, packet
\t\t\t\t\t\t.getLength());
\t\t\t
\t\t\t\tinceAr.setText("正在接收的內容:\\n" + message);
\t\t\t\tinced.append(message + "\\n");
\t\t\t} catch (Exception e) {
\t\t\t\te.printStackTrace();

\t\t\t}
\t\t\tif (b == true) {
\t\t\t\tbreak;
\t\t\t}
\t\t}
\t}
\t
\tpublic void actionPerformed(ActionEvent e) {
\t\tif (e.getSource() == ince) {
\t\t\tince.setBackground(Color.red);
\t\t\tstop.setBackground(Color.yellow);
\t\t\tif (!(thread.isAlive())) {
\t\t\t\tthread = new Thread(this);
\t\t\t}
\t\t\tthread.start();
\t\t\tb = false;
\t\t}
\t\tif (e.getSource() == stop) {
\t\t\tince.setBackground(Color.blue);
\t\t\tstop.setBackground(Color.red);
\t\t\tb = true;
\t\t}
\t}
\t
\tpublic static void main(String[] args) {
\t\tReceive rec = new Receive();
\t\trec.setSize(460, 200);
\t}
}/<code>
UDP程序設計基礎

注:文章轉自互聯網


分享到:


相關文章: