3.5.3.3 基于文本编解码和UDP套接字
客户端
package com.suifeng.tcpip.chapter3.vote; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Arrays; /** * UDP 客户端 * * @author Suifeng * */ public class VoteUDPClient { // 超时时间 private static final int TIMEOUT = 3000; // 最大连接次数 private static final int MAXTRIES = 5; public static void main(String[] args) throws IOException { if (args.length != 3) { throw new IllegalArgumentException("Paramters:<Server> <Port> <Candidate>"); } // 服务器地址 InetAddress serverAddress = InetAddress.getByName(args[0]); // 服务器端口 int serverPort = Integer.parseInt(args[1]); // 候选人编号 int candidate = Integer.parseInt(args[2]); // UDP客户端 DatagramSocket socket = new DatagramSocket(); socket.connect(serverAddress, serverPort); // 接收数据阻塞时间 socket.setSoTimeout(TIMEOUT); System.out.println("UDP 客户端已建立...."); VoteMsg vote = new VoteMsg(false, false, candidate, 0); VoteMsgCoder coder = new VoteMsgTextCoder(); byte[] encodeVote = coder.toWire(vote); System.out.println("Sending Text-encode Request (" + encodeVote.length + "bytes)"); System.out.println(vote); DatagramPacket message = new DatagramPacket(encodeVote, encodeVote.length); socket.send(message); message = new DatagramPacket(new byte[VoteMsgTextCoder.MAX_WIRE_LENGTH], VoteMsgTextCoder.MAX_WIRE_LENGTH); socket.receive(message); encodeVote = Arrays.copyOfRange(message.getData(), 0, message.getData().length); System.out.println("Received Text-encoded Response ("+encodeVote.length+" bytes)"); vote = coder.fromWire(encodeVote); System.out.println(vote); } }
服务器端
package com.suifeng.tcpip.chapter3.vote; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; import java.util.Arrays; /** * UDP服务器端 * * @author Suifeng * */ public class VoteUDPServer { // 最大显示数组字节数 private static final int ECHO_MAX = 255; public static void main(String[] args) throws IOException { if (args.length != 1) { throw new IllegalArgumentException("Parameter:<Port>"); } // 服务器端口 int serverPort = Integer.parseInt(args[0]); // 服务器端 DatagramSocket socket = new DatagramSocket(serverPort); System.out.println("UDP服务器已启动...."); VoteMsgCoder coder = new VoteMsgTextCoder(); VoteService service = new VoteService(); byte[] inBuffer =new byte[VoteMsgTextCoder.MAX_WIRE_LENGTH]; while (true) { System.out.println("正在等待客户端发送数据...."); // 数据报 DatagramPacket packet = new DatagramPacket(inBuffer, inBuffer.length); // 接收客户端发送的数据(阻塞) socket.receive(packet); byte[] encodeMsg = Arrays.copyOfRange(packet.getData(), 0, packet.getData().length); System.out.println("Handing request from "+packet.getSocketAddress()+ " ("+encodeMsg.length+")"); VoteMsg msg = coder.fromWire(encodeMsg); msg = service.handleRequest(msg); packet.setData(coder.toWire(msg)); System.out.println("Sending Resoponse ("+packet.getLength()+" bytes"); System.out.println(msg); socket.send(packet); } } }
启动服务器端,监听39393端口
启动客户端
查看服务端
再次启动客户端
再次查看服务器端
作者:licl19870605 发表于2013-1-28 23:08:29 原文链接
阅读:32 评论:0 查看评论