Scoket:UDP通讯模型
 
UDP是一种不可靠的网络通讯方式,这里的不可靠是从理论上讲的,从实用角度看还是很可靠的,比如QQ消息等就使用的是UDP的通信的方式。
 
一、最简单的通信模型
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
* 数据报套接字服务端
*
* @author leizhimin 2009-12-2 22:09:07
*/

public class TestUDPServer {
        public static void main(String[] args) throws IOException {
                System.out.println("--------------server端------------");
                byte[] buff = new byte[1024];
                //创建一个数据包,用来服务端接收到的数据
                DatagramPacket dp = new DatagramPacket(buff, 0, buff.length);
                //创建一个绑定本机9999端口的数据报套接字
                DatagramSocket ds = new DatagramSocket(9999);
                while (true) {
                        //阻塞式的接收一个数据包
                        ds.receive(dp);
                        System.out.println(new String(buff, 0, buff.length));
                }
        }
}
 
package lavasoft.udpsokt;

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

/**
* 数据报套接字客户端
*
* @author leizhimin 2009-12-2 22:34:22
*/

public class TestUDPClient {
        public static void main(String[] args) throws IOException {
                System.out.println("--------------client端------------");
                byte[] buff = "Hello Server!".getBytes();
                //创建一个数据包,其中指定了发送的目标地
                DatagramPacket dp = new DatagramPacket(buff, 0, buff.length, new InetSocketAddress("192.168.1.101", 9999));
                //创建一个数据报套接字,绑定到随机可用的端口上
                DatagramSocket ds = new DatagramSocket();
                //通过该数据包套接向目标发送数据
                ds.send(dp);
                //关闭套接字
                ds.close();
        }
}
 
先启动服务端,然后运行三个客户端。
 
服务端控制台如下:
--------------server端------------
Hello Server!
Hello Server!
Hello Server!
 
三个客户端的控制台输出都是:
--------------client端------------

Process finished with exit code 0
 
二、双向的交互式通信
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
* 数据报套接字服务端:接收发送消息
*
* @author leizhimin 2009-12-2 22:09:07
*/

public class TestUDPServer {
        public static void main(String[] args) throws IOException {
                System.out.println("--------------server端------------");
                byte[] buff = new byte[1024];
                //创建一个数据包,用来服务端接收到的数据
                DatagramPacket dp = new DatagramPacket(buff, 0, buff.length);
                //创建一个绑定本机9999端口的数据报套接字
                DatagramSocket ds = new DatagramSocket(9999);
                while (true) {
                        //阻塞式的接收一个数据包
                        ds.receive(dp);
                        //打印接收的数据
                        System.out.println("[" + dp.getAddress().getHostAddress() + ":" + dp.getPort() + "]" + "的消息:" + new String(buff, 0, buff.length));
                        String msg = "[" + dp.getAddress().getHostAddress() + ":" + dp.getPort() + "]" + "服务端已经收到你的数据包了!";
                        //响应客户端,回复一个消息,当然这个端口地址需要从接收到的数据包中获取
                        DatagramPacket dps = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, dp.getSocketAddress());
                        ds.send(dps);
                }
        }
}
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

/**
* 数据报套接字客户端;发送接收消息
*
* @author leizhimin 2009-12-2 22:34:22
*/

public class TestUDPClient {
        public static void main(String[] args) throws IOException {
                System.out.println("--------------client端------------");
                byte[] buff = "Hello Server!".getBytes();
                //创建一个数据包,其中指定了发送的目标地
                DatagramPacket dp = new DatagramPacket(buff, 0, buff.length, new InetSocketAddress("192.168.14.117", 9999));
                //创建一个数据报套接字,绑定到随机可用的端口上
                DatagramSocket ds = new DatagramSocket();
                //通过该数据包套接向目标发送数据
                ds.send(dp);

                byte[] buffres = new byte[1024];
                //创建一个用来接收服务器反馈的数据包
                DatagramPacket dbres = new DatagramPacket(buffres, 0, buffres.length, ds.getLocalAddress(), ds.getLocalPort());
                //接收数据
                ds.receive(dbres);
                //打印接收的数据
                System.out.println(new String(buffres, 0, buffres.length));
                //关闭套接字
                ds.close();
        }
}
 
服务端控制台:
--------------server端------------
[192.168.14.117:4198]的消息:Hello Server!
 
客户端控制台:
--------------client端------------
[192.168.14.117:4198]服务端已经收到你的数据包了!
 
如果将消息的发送接收做成多线程异步IO模式,就可以简单实现聊天工具的功能了。