java網絡編程總結java
TCP/IP協議,他把網絡分爲四層:應用層,傳輸層,網絡層,網絡接口層。編程
TCP在傳輸層,IP在網絡層。TCP協議是用於傳輸的,而IP是用於尋址的。服務器
對Java.net包,括重要的類有InetAddress類,ServerSocket類,Socket類,DatagramSocket類,DatagramPacket類,URL類,和URLConnection類。網絡
InetAddress類,是用來封裝計算機的IP地址和DNS的。併發
DatagramPacket是起到數據窗口做用的一個類,DatagramSocket是用於發送和接收DatagramPacket的機制。ide
TCP/IP套接了是最可靠的雙向流協議,經過三次握手來確認傳輸的可靠性,等待客戶端的服務器使用ServerSocket類,而要鏈接到服務器的客戶端則使用Socket類。性能
URL使用地址來幫助查找Internet上的文件,URLConnection類是一個通用類,提供用於進一步瞭解遠程資源的方法。this
利用套接字能夠傳輸對象,爲了解決性能產經常使用的辦法是經過套接字發送流。spa
具體的實例代碼以下:.net
文本文件傳輸
/*客戶端*/ public class FileClient { public static void main(String[] args) throws Exception, IOException { Socket s = new Socket("localhost" , 10001); BufferedReader bufr = new BufferedReader(new FileReader( "FileServer.java")); PrintWriter out = new PrintWriter(s.getOutputStream(),true); String line = null; while ((line = bufr.readLine()) != null) { out.println(line); } s.shutdownOutput(); // 關閉客戶端輸出流,至關於在流中加入結束標記 -1 BufferedReader bufIn = new BufferedReader(new InputStreamReader(s .getInputStream())); String serverResult = bufIn.readLine(); System. out.println(serverResult); s.close(); } }
//服務端 public class FileServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10001); Socket client = ss.accept(); BufferedReader bufIn = new BufferedReader(new InputStreamReader(client .getInputStream())); System. out.println("ip:" +client.getInetAddress().getHostAddress()); PrintWriter pw = new PrintWriter(new FileWriter("save.txt"), true); String line = null; while ((line = bufIn.readLine()) != null) { System. out.println(line); pw.println(line); } PrintWriter out = new PrintWriter(client.getOutputStream()); out.println( "上傳成功" ); out.close(); pw.close(); client.close(); ss.close(); } }
-------------------------------------------------
服務端併發接收圖片文件上傳
-------------------------------------------------
/* * 客戶端 * 功能:發送圖片文件 * */ public class ConcurrentPicClient { public static void main(String[] args) throws Exception { Socket s = new Socket("localhost" , 10001); InputStream in = s.getInputStream(); OutputStream out = s.getOutputStream(); //讀文件 FileInputStream is = new FileInputStream(new File("test.gif" )); byte[] buf = new byte[1024]; int len = 0; while ((len = is.read(buf)) != -1) { out.write(buf, 0, len); } s.shutdownOutput(); // 標記文件傳遞結束 len = in.read(buf); System. out.println(new String(buf, 0, len)); is.close(); s.close(); } } /* * 服務端 * 功能:併發接收圖片上傳 * 圖片文件使用Stream進行讀寫,不適用Reader/Writer * */ public class ConcurrentServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10001); while (true ) { new Thread(new ServerThread(ss.accept())).start(); } } } class ServerThread implements Runnable { private Socket s ; public ServerThread(Socket s) { this.s = s; } public void run() { int count = 1; String ip = s.getInetAddress().getHostAddress(); System. out.println(ip + "...connected" ); try { InputStream in = s.getInputStream(); OutputStream out = s.getOutputStream(); // 保證file不重名 File file = new File(ip + "(" + count + ").jpg"); while (file.exists()) { count++; file = new File(ip + "(" + count + ").jpg"); } // 寫文件 FileOutputStream fo = new FileOutputStream(file); byte[] buf = new byte[1024]; int len = 0; while ((len = in.read(buf)) != -1) { fo.write(buf, 0, len); } out.write( "上傳成功" .getBytes()); fo.close(); s.close(); } catch (Exception e) { } } }