TCP:TCP在網絡通訊上面具備頑強的生命力,例如遠程鏈接(Telnet)和文件傳輸(FTP)都須要不定長度的數據被可靠地傳輸,可是這種可靠的傳輸是須要付出代價的,由於對數據內容正確性的檢驗佔用計算機處理時間和網絡寬帶,一次TCP效率不及UDP。java
TCP:可靠,無大小限制,可是須要鏈接創建時間,差錯控制開銷大。網絡
UDP:UDP操做簡單,僅須要較少的監護,所以一般應用於局域網高可靠性的分散系統的Server/Client應用程序,例如視頻會議系統,並不須要徹底保證數據的絕對準確性,只須要保持連貫便可,這種狀況下,顯然UDP要更合理一些。經常使用在交互實時性高的系統上面,如網路遊戲,視頻會議。.net
UDP:不可靠,有大小限制,64KB如下,不須要創建鏈接,差錯控制開銷小。code
Server:視頻
package com.wangbiao.test; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; public class Server { // server public static void server() throws Exception { ServerSocket server = new ServerSocket(8888); String message = "hello everyone"; System.out.println("Server is up.waiting for connect"); Socket client = server.accept(); // OutputStream out=client.getOutputStream(); // byte[] b=new byte[1024]; // out.write(b); // System.out.println(new String(b,0,b.length)); PrintStream ps = new PrintStream(client.getOutputStream()); ps.println(message); ps.close(); client.close(); server.close(); } // server chat with client public static void server_chat() throws Exception { ServerSocket server = new ServerSocket(8888); Socket client = null; BufferedReader in = null; PrintStream out = null; boolean working = true; boolean flag = true; while (working) { System.out.println("Server is waiting for connect"); client = server.accept(); in = new BufferedReader(new InputStreamReader( client.getInputStream())); out = new PrintStream(client.getOutputStream()); while (flag) { String str = in.readLine(); if (null == str || "".equals(str)) { flag = false; } else { if ("bye".equals(str)) { flag = false; } else { System.out.println("Cient:" + str); out.println("Server:" + str); } } } } in.close(); out.close(); client.close(); server.close(); } // UDP public static void send_message() throws Exception { DatagramPacket dp=null; DatagramSocket ds=null; String content="hello UDP"; ds=new DatagramSocket(3000); dp=new DatagramPacket(content.getBytes(),content.length(),InetAddress.getByName("localhost"),9000); ds.send(dp); ds.close(); } public static void main(String[] args) throws Exception { // server(); //server_chat(); send_message(); } }
Client:server
package com.wangbiao.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.Socket; import java.net.UnknownHostException; public class Client { // client public static void client() throws Exception { Socket client = new Socket("localhost", 8888); String str = "test"; BufferedReader br = new BufferedReader(new InputStreamReader( client.getInputStream())); String content = br.readLine(); System.out.println(content); br.close(); client.close(); } // server chat with client public static void client_chat() throws Exception { Socket client = new Socket("localhost", 8888); BufferedReader input = new BufferedReader(new InputStreamReader( System.in)); BufferedReader in = new BufferedReader(new InputStreamReader( client.getInputStream())); PrintStream out = new PrintStream(client.getOutputStream()); boolean flag = true; while (flag) { String str = input.readLine(); out.println(str); if ("bye".equals(str)) { flag = false; } else { String mess=in.readLine(); System.out.println(mess); } } in.close(); out.close(); client.close(); } // UDP public static void receive_message() throws Exception { DatagramPacket dp=null; DatagramSocket ds=null; byte[] b=new byte[1024]; ds=new DatagramSocket(9000); dp=new DatagramPacket(b, 1024); ds.receive(dp); System.out.println("waiting for server message"); String content=dp.getAddress().getHostAddress()+"-"+new String(dp.getData()); System.out.println(content); ds.close(); } public static void main(String[] args) throws Exception { //client(); //client_chat(); receive_message(); } }