《Java TCP/IP Socket編程》讀書筆記(10)

來源:互聯網
上載者:User
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連接埠


啟動用戶端


查看服務端


再次啟動用戶端


再次查看伺服器端


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.