Java基于UDP协议的简单通讯实例

源码下载:http://download.csdn.net/detail/u011983531/9451560

一、服务端

package com.ghs.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpServer {

    private int PORT=5050;
    private DatagramSocket socket;
    private DatagramPacket packet;
    private byte[] dataBytes;
    private StringBuilder receiveData=new StringBuilder();//保存接收到的数据
    private String sendData;//要发送的数据

    public UdpServer(){
        init();
    }

    private void init(){
        try {
            socket = new DatagramSocket(PORT);
            dataBytes = new byte[1024];
            packet = new DatagramPacket(dataBytes, dataBytes.length);
            while(true){//循环接收数据
                socket.receive(packet);
                int length = packet.getLength();
                if(length>0){//如果数据包不为空,就打印出接收到的数据
                    receiveData.append(new String(packet.getData(),0,length));
                    System.out.println(receiveData);
                    //回复消息
                    sendData = "收到";
                    dataBytes = sendData.getBytes();
                    packet = new DatagramPacket(dataBytes, dataBytes.length,
                            packet.getAddress(),packet.getPort());
                    socket.send(packet);
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        socket.close();
    }

    public static void main(String[] args) {
        new UdpServer();
    }
}

二、客户端

package com.ghs.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;


public class UdpClient {
    private int PORT = 5050;
    private DatagramSocket socket;
    private DatagramPacket packet;
    private byte[] dataBytes;
    private StringBuilder receiveData=new StringBuilder();//保存接收到的数据
    private String sendData;//要发送的数据

    public UdpClient() {
        init();
    }

    private void init(){
        try {
            //发送数据
            socket = new DatagramSocket();
            sendData = "UDP发送数据";
            dataBytes = sendData.getBytes();
            packet = new DatagramPacket(dataBytes, dataBytes.length,
                    InetAddress.getByName("localhost"),PORT);
            socket.send(packet);

            //接受数据
            while(true){
                dataBytes = new byte[1024];
                packet = new DatagramPacket(dataBytes, dataBytes.length);
                socket.receive(packet);
                int length = packet.getLength();
                if(length>0){
                    receiveData.append(new String(packet.getData(),0,length));
                    System.out.println(receiveData);
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        socket.close();
    }

    public static void main(String[] args) {
        new UdpClient();
    }
}

你可能感兴趣的:(Java网络编程)