與OSI參考模型不一樣,TCP/IP協議棧共有4層,其中網絡接口層對應OSI中的物理層和數據鏈路層,應用層對應OSI中的應用層、表示層和會話層。html
在網絡接口層的主要協議有:ARP、RARP等。ARP協議主要功能是根據IP地址獲取物理地址,RARP協議則反之。java
網絡層的主要協議有:IP、路由協議(RIP、OSPF、BGP等)。IP協議爲網絡上的每臺主機編號,在此基礎上纔有路由協議,所以路由協議是基於IP協議的。網絡
傳輸層的主要協議有:TCP、UDP。傳輸層有端口號的概念,端口號是指TCP或UDP協議能根據端口號找到接受數據包的進程。(也就是說一個TCPServer和一個UDPServer能夠綁定同一個端口,詳細解釋參看第2節)socket
應用層協議主要有:HTTP、FTP、SMTP等。post
協議號:協議號是存在於IP數據報的首部的20字節的固定部分,佔有8bit。該字段是指出此數據報所攜帶的是數據是使用何種協議,以便目的主機的IP層知道將數據部分上交給哪一個傳輸層協議(TCP/UDP等)。測試
端口號:端口號存在於TCP/UDP報文的首部。spa
目的主機收到數據報後,IP協議會將解析到協議號,並據此將數據上送給相應的傳輸層協議;傳輸層協議收到數據並解析,得到端口號,並據此將數據上送給相應的接受數據報的進程。.net
從上述過程可知,端口號是按照傳輸層協議號劃分了「命名空間」的,所以,同一主機能夠同時運行綁定同一端口號的TCP Socket(java中對應ServerSocket、Socket)和UDP Socket(java中對應DatagramSocket)而不發生衝突。code
測試使用的代碼:server
TCPClient.java
1 package socket; 2 3 import java.io.*; 4 import java.net.*; 5 import java.util.Date; 6 7 public class TCPClient { 8 9 /** 10 * @param args 11 */ 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 try { 15 System.out.println(new Date()); 16 InetAddress remoteAddress = InetAddress.getByName("22.11.143.60"); 17 // InetAddress localAddress = InetAddress.getByName("127.0.0.1"); 18 // Socket socket = new Socket(remoteAddress, 1287, localAddress, 1288); 19 // Socket socket = new Socket(remoteAddress, 1287); 20 Socket socket = new Socket (); 21 socket.connect(new InetSocketAddress(remoteAddress,1287), 1000); 22 socket.setSoTimeout(110000); 23 System.out.println(new Date()); 24 PrintWriter os = new PrintWriter(socket.getOutputStream()); 25 BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())); 26 String msg = "hello"; 27 os.println(msg); 28 os.flush(); 29 System.out.println("Client: " + msg); 30 // Thread.sleep(1000); 31 System.out.println("Server: " + is.readLine()); 32 System.out.println(new Date()); 33 Thread.sleep(1000000); 34 System.out.println("end!"); 35 os.close(); 36 is.close(); 37 socket.close(); 38 } catch (UnknownHostException e) { 39 // TODO Auto-generated catch block 40 System.out.println(new Date()); 41 e.printStackTrace(); 42 } catch (IOException e) { 43 // TODO Auto-generated catch block 44 System.out.println(new Date()); 45 e.printStackTrace(); 46 } 47 catch (InterruptedException e) { 48 // TODO Auto-generated catch block 49 System.out.println(new Date()); 50 e.printStackTrace(); 51 } 52 53 } 54 55 }
TCPServer.java
1 package socket; 2 3 import java.io.*; 4 import java.net.*; 5 6 public class TCPServer { 7 8 /** 9 * @param args 10 */ 11 public static void main(String[] args) { 12 // TODO Auto-generated method stub 13 try { 14 ServerSocket serverSocket = new ServerSocket(1287,2); 15 Socket socket = serverSocket.accept(); 16 17 PrintWriter os = new PrintWriter(socket.getOutputStream()); 18 BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())); 19 while (true){ 20 System.out.println("Client: " + is.readLine()); 21 String msg = "hi"; 22 os.println(msg); 23 os.flush(); 24 System.out.println("Server: " + msg); 25 } 26 // os.close(); 27 // is.close(); 28 // socket.close(); 29 // serverSocket.close(); 30 } catch (IOException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } 34 // catch (InterruptedException e) { 35 // // TODO Auto-generated catch block 36 // e.printStackTrace(); 37 // } 38 39 } 40 41 }
UDPClient.java
1 package socket; 2 3 import java.io.IOException; 4 import java.net.DatagramPacket; 5 import java.net.DatagramSocket; 6 import java.net.InetAddress; 7 import java.net.SocketException; 8 import java.net.UnknownHostException; 9 10 public class UDPClient { 11 12 public static void main(String[] args) { 13 try { 14 DatagramSocket ds = new DatagramSocket(); 15 String str = "hello"; 16 DatagramPacket dp = new DatagramPacket(str.getBytes(),str.length(),InetAddress.getByName("22.11.143.60"),1287); 17 ds.send(dp); 18 19 ds.close(); 20 21 } catch (SocketException e) { 22 e.printStackTrace(); 23 } catch (UnknownHostException e) { 24 e.printStackTrace(); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 29 } 30 31 }
UDPServer.java
1 package socket; 2 3 import java.io.IOException; 4 import java.net.DatagramPacket; 5 import java.net.DatagramSocket; 6 import java.net.SocketException; 7 8 public class UDPServer { 9 10 public static void main(String[] args) { 11 12 try { 13 DatagramSocket ds = new DatagramSocket(1287); 14 byte[] buf=new byte[100]; 15 DatagramPacket dp=new DatagramPacket(buf,100); 16 ds.receive(dp);// block here 17 System.out.println(new String(dp.getData())); 18 19 ds.close(); 20 21 } catch (SocketException e) { 22 e.printStackTrace(); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 27 28 } 29 30 }
http://www.cnblogs.com/kqdongnanf/p/4773539.html