1. 程式人生 > 其它 >JAVA 通訊協議

JAVA 通訊協議

JAVA的通訊協議

TCP/IP協議簇

TCP:使用者傳輸協議

 (類似打電話;

  連線穩定;

  三次握手{A:你瞅啥?B:瞅你咋地?A:幹一場!};

  四次握手{A:我要走了。B:你真的要走了?B:你真的真的要走了?A:我真的要走了。};

  客戶端和服務端連線;

  傳輸完成釋放連線,效率低)

UDP:使用者資料報協議

 (類似發簡訊;

  不連線,不穩定;

  客戶端和服務端沒有明確解析;

  不管有沒有準備好都可以傳送;)

IP:網路互連協議

客戶端:

package 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; import java.net.UnknownHostException; public class TcpClientDemo01 { public static void main(String[] args) { InetAddress serverIP = null; Socket socket = null; OutputStream outputStream
= null; try { InetAddress.getByName("127.0.0.1"); serverIP =InetAddress.getLocalHost(); int port = 9999; socket = new Socket(serverIP, port); outputStream = socket.getOutputStream(); outputStream.write("你好12345678".getBytes()); }
catch (Exception e) { e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

服務端:

package TCP;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDemo01 {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream =null;
        try {
            serverSocket = new ServerSocket(9999);
            socket = serverSocket.accept();
            inputStream = socket.getInputStream();
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, len);
            }
            System.out.println(byteArrayOutputStream.toString()); } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(byteArrayOutputStream!=null){
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}