基于Java自身包實(shí)現(xiàn)消息方式的系統(tǒng)間通信的方式有:TCP/IP+BIO,TCP/IP+NIO,UDP/IP+BIO,UDP/IP+NIO.下面就這4種類(lèi)型一一做個(gè)詳細(xì)的介紹:
一.TCP/IP+BIO
在java中可基于Socket,ServerSocket來(lái)實(shí)現(xiàn)TCP/IP+BIO的系統(tǒng)間通信。Socket主要用于實(shí)現(xiàn)建立連接以及網(wǎng)絡(luò)IO的操作,ServerSocket主要用于實(shí)現(xiàn)服務(wù)器端端口的監(jiān)聽(tīng)及Socket對(duì)象的獲取?;赟ocket實(shí)現(xiàn)客戶端的代碼如下:
public class Client { /** * @param args */ public static void main(String[] args) throws Exception{ String host="127.0.0.1"; int port=9527; Socket socket=new Socket(host,port); BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out=new PrintWriter(socket.getOutputStream(),true); BufferedReader systemIn=new BufferedReader(new InputStreamReader(System.in)); boolean flag=true; while(flag){ String command=systemIn.readLine(); if(command==null || "quit".equalsIgnoreCase(command.trim())){ flag=false; &n