Chinaunix首页 | 论坛 | 博客
  • 博客访问: 459739
  • 博文数量: 155
  • 博客积分: 2954
  • 博客等级: 少校
  • 技术积分: 1000
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-12 22:00
文章分类

全部博文(155)

文章存档

2014年(2)

2013年(5)

2012年(10)

2011年(33)

2010年(105)

我的朋友

分类: Java

2010-08-24 14:11:24

OSI七层与TCP/IP4层

TCP/IP4层 OSI七层 协议
应用层 应用层 Telnet、FTP、HTTP、DNS、SMTP、POP3
表示层
会话层
传输层 传输层 TCP、UDP
网络层 网络层 IP、ICMP、IGMP
网络接口层  
数据链路层  
物理层

 

 

 

 

 

 

 

 

 

 

 

 

 

TCP与UDP比较

TCP UDP
是否连接 面向连接的 面向非连接的
传输可靠性 可靠的 不可靠的
应用场合

1、数据完整性要求高的传输

2、传输大量的数据(大小没有限制)

如:安装文件下载等

1、实时性要求高,丢失部分数据也无关紧要的传输

2、少量数据(有限制,每次不超过64K)

如:视频会议等

速度

 

 

 

 

 

 

 

 

 

 

Socket与TCP、UDP

1、TCP实现

Java代码 复制代码
  1. package myjava.net.socket.tcp;   
  2.   
  3. import java.io.IOException;   
  4. import java.io.InputStream;   
  5. import java.io.OutputStream;   
  6. import java.net.InetAddress;   
  7. import java.net.ServerSocket;   
  8. import java.net.Socket;   
  9.   
  10. /**  
  11.  * Dependent on TCP protocol
     
  12.  * 1.MultiThread for server socket.
     
  13.  * 2.Create a new thread when accept a client socket.
     
  14.  *   
  15.  * @author jeck.xie 2009-3-25  
  16.  */  
  17. public class SocketTCP extends Thread {   
  18.     private Socket s;   
  19.   
  20.     public SocketTCP(Socket s) {   
  21.         this.s = s;   
  22.     }   
  23.   
  24.     @Override  
  25.     public void run() {   
  26.         try {   
  27.             OutputStream os = s.getOutputStream();   
  28.             InputStream is = s.getInputStream();   
  29.             os.write("Hello,welcome you!".getBytes());   
  30.             byte[] buf = new byte[100];   
  31.             int len = is.read(buf);   
  32.             System.out.println(new String(buf, 0, len));   
  33.             os.close();   
  34.             is.close();   
  35.             s.close();   
  36.         } catch (Exception e) {   
  37.             e.printStackTrace();   
  38.         }   
  39.     }   
  40.   
  41.     public static void main(String[] args) {   
  42.         if (args.length > 0)   
  43.             server();   
  44.         else  
  45.             client();   
  46.     }   
  47.   
  48.     public static void server() {   
  49.         try {   
  50.             ServerSocket ss = new ServerSocket(6000);   
  51.             while (true) {   
  52.                 Socket s = ss.accept();   
  53.                 new SocketTCP(s).start();   
  54.             }   
  55.   
  56.             // ss.close();   
  57.         } catch (IOException e) {   
  58.             e.printStackTrace();   
  59.         }   
  60.     }   
  61.   
  62.     public static void client() {   
  63.         try {   
  64.             Socket s = new Socket(InetAddress.getLocalHost(), 6000);   
  65.             OutputStream os = s.getOutputStream();   
  66.             InputStream is = s.getInputStream();   
  67.             byte[] buf = new byte[100];   
  68.             int len = is.read(buf);   
  69.             System.out.println(new String(buf, 0, len));   
  70.             os.write("Hello,this is zhangsan!".getBytes());   
  71.             os.close();   
  72.             is.close();   
  73.             s.close();   
  74.         } catch (Exception e) {   
  75.             e.printStackTrace();   
  76.         }   
  77.     }   
  78. }  

 2.UDP实现

Java代码 复制代码
  1. package myjava.net.socket.udp;   
  2.   
  3. import java.net.DatagramPacket;   
  4. import java.net.DatagramSocket;   
  5. import java.net.InetAddress;   
  6.   
  7. /**  
  8.  * Dependent on UDP protocol
     
  9.  *   
  10.  * @author jeck.xie 2009-3-25  
  11.  */  
  12. public class SocketUDP {   
  13.   
  14.     public static void main(String[] args) {   
  15.         if (args.length > 0)   
  16.             recv();   
  17.         else  
  18.             send();   
  19.     }   
  20.   
  21.     public static void recv() {   
  22.         try {   
  23.             DatagramSocket ds = new DatagramSocket(6000);   
  24.   
  25.             // accept data   
  26.             byte[] buf = new byte[100];   
  27.             DatagramPacket dp = new DatagramPacket(buf, 100);   
  28.             ds.receive(dp);   
  29.             System.out.println(new String(buf, 0100));   
  30.   
  31.             // send data   
  32.             String str = "Welcome you!";   
  33.             DatagramPacket dpSend = new DatagramPacket(str.getBytes(), str   
  34.                     .length(), dp.getAddress(), dp.getPort());   
  35.             ds.send(dpSend);   
  36.   
  37.             ds.close();   
  38.         } catch (Exception e) {   
  39.             e.printStackTrace();   
  40.         }   
  41.     }   
  42.   
  43.     public static void send() {   
  44.         try {   
  45.             DatagramSocket ds = new DatagramSocket();   
  46.   
  47.             // send data   
  48.             String str = "Hello, this is zhangsan!";   
  49.             DatagramPacket dp = new DatagramPacket(str.getBytes(),   
  50.                     str.length(), InetAddress.getLocalHost(), 6000);   
  51.             ds.send(dp);   
  52.   
  53.             // accept data   
  54.             byte[] buf = new byte[100];   
  55.             DatagramPacket dbRecv = new DatagramPacket(buf, 100);   
  56.             ds.receive(dbRecv);   
  57.             System.out.println(new String(buf, 0100));   
  58.   
  59.             ds.close();   
  60.         } catch (Exception e) {   
  61.             e.printStackTrace();   
  62.         }   
  63.     }   
  64.   
  65. }  
阅读(967) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~