TCP/UDP与Socket
时间:2010-08-24 来源:myjiushizhe
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代码- package myjava.net.socket.tcp;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.InetAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- /**
- * Dependent on TCP protocol<br>
- * 1.MultiThread for server socket.<br>
- * 2.Create a new thread when accept a client socket.<br>
- *
- * @author jeck.xie 2009-3-25
- */
- public class SocketTCP extends Thread {
- private Socket s;
- public SocketTCP(Socket s) {
- this.s = s;
- }
- @Override
- public void run() {
- try {
- OutputStream os = s.getOutputStream();
- InputStream is = s.getInputStream();
- os.write("Hello,welcome you!".getBytes());
- byte[] buf = new byte[100];
- int len = is.read(buf);
- System.out.println(new String(buf, 0, len));
- os.close();
- is.close();
- s.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- if (args.length > 0)
- server();
- else
- client();
- }
- public static void server() {
- try {
- ServerSocket ss = new ServerSocket(6000);
- while (true) {
- Socket s = ss.accept();
- new SocketTCP(s).start();
- }
- // ss.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void client() {
- try {
- Socket s = new Socket(InetAddress.getLocalHost(), 6000);
- OutputStream os = s.getOutputStream();
- InputStream is = s.getInputStream();
- byte[] buf = new byte[100];
- int len = is.read(buf);
- System.out.println(new String(buf, 0, len));
- os.write("Hello,this is zhangsan!".getBytes());
- os.close();
- is.close();
- s.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
package myjava.net.socket.tcp; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * Dependent on TCP protocol<br> * 1.MultiThread for server socket.<br> * 2.Create a new thread when accept a client socket.<br> * * @author jeck.xie 2009-3-25 */ public class SocketTCP extends Thread { private Socket s; public SocketTCP(Socket s) { this.s = s; } @Override public void run() { try { OutputStream os = s.getOutputStream(); InputStream is = s.getInputStream(); os.write("Hello,welcome you!".getBytes()); byte[] buf = new byte[100]; int len = is.read(buf); System.out.println(new String(buf, 0, len)); os.close(); is.close(); s.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { if (args.length > 0) server(); else client(); } public static void server() { try { ServerSocket ss = new ServerSocket(6000); while (true) { Socket s = ss.accept(); new SocketTCP(s).start(); } // ss.close(); } catch (IOException e) { e.printStackTrace(); } } public static void client() { try { Socket s = new Socket(InetAddress.getLocalHost(), 6000); OutputStream os = s.getOutputStream(); InputStream is = s.getInputStream(); byte[] buf = new byte[100]; int len = is.read(buf); System.out.println(new String(buf, 0, len)); os.write("Hello,this is zhangsan!".getBytes()); os.close(); is.close(); s.close(); } catch (Exception e) { e.printStackTrace(); } } }
2.UDP实现
Java代码- package myjava.net.socket.udp;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- /**
- * Dependent on UDP protocol<br>
- *
- * @author jeck.xie 2009-3-25
- */
- public class SocketUDP {
- public static void main(String[] args) {
- if (args.length > 0)
- recv();
- else
- send();
- }
- public static void recv() {
- try {
- DatagramSocket ds = new DatagramSocket(6000);
- // accept data
- byte[] buf = new byte[100];
- DatagramPacket dp = new DatagramPacket(buf, 100);
- ds.receive(dp);
- System.out.println(new String(buf, 0, 100));
- // send data
- String str = "Welcome you!";
- DatagramPacket dpSend = new DatagramPacket(str.getBytes(), str
- .length(), dp.getAddress(), dp.getPort());
- ds.send(dpSend);
- ds.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void send() {
- try {
- DatagramSocket ds = new DatagramSocket();
- // send data
- String str = "Hello, this is zhangsan!";
- DatagramPacket dp = new DatagramPacket(str.getBytes(),
- str.length(), InetAddress.getLocalHost(), 6000);
- ds.send(dp);
- // accept data
- byte[] buf = new byte[100];
- DatagramPacket dbRecv = new DatagramPacket(buf, 100);
- ds.receive(dbRecv);
- System.out.println(new String(buf, 0, 100));
- ds.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
相关阅读 更多 +