最近在向架構(gòu)方向轉(zhuǎn),一直在查看分布式架構(gòu)相關(guān)的知識(shí)點(diǎn),本篇博客主要是對(duì)TCP通信協(xié)議的java代碼練習(xí),純干貨,沒有課本知識(shí):
demo:Socket
package com.example.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @ClassName : ServerSocketDemo
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-07 19:51
*/
public class ServerSocketDemo {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket= null;
BufferedReader bufferedReader = null ;
try {
serverSocket = new ServerSocket(8099);
//接收客戶端的鏈接
Socket socket = serverSocket.accept();
//獲得輸入流
bufferedReader = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);
System.out.println(bufferedReader.readLine());
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bufferedReader !=null){
bufferedReader.close();
}
if(serverSocket !=null){
serverSocket.close();
}
}
}
}
package com.example.demo;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
/**
* @ClassName : ClientSocketDemo
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-07 20:03
*/
public class ClientSocketDemo {
public static void main(String[] args) throws IOException {
Socket socket=null;
try {
socket = new Socket("127.0.0.1",8099);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),true);
printWriter.println("O(∩_∩)O哈哈~");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(socket !=null){
socket.close();
}
}
}
}
運(yùn)行結(jié)果:
小結(jié):
我們通過簡(jiǎn)單的創(chuàng)建demo一個(gè)客戶端 一個(gè)服務(wù)端,把數(shù)據(jù)傳遞過來了,我們這是使用Socket套接字進(jìn)行練習(xí),創(chuàng)建連接,監(jiān)聽端口號(hào),進(jìn)行數(shù)據(jù)傳遞。
UDP:
package com.example.demo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* @ClassName : UdpServerDemo
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-07 20:17
*/
public class UdpServerDemo {
public static void main(String[] args) {
//創(chuàng)建服務(wù)并且接收數(shù)據(jù)包
try {
DatagramSocket datagramSocket = new DatagramSocket(8088);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(
receiveData,receiveData.length
);
datagramSocket.receive(receivePacket);
System.out.println(new String(receiveData,0,receivePacket.getLength()));
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.example.demo;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.net.*;
/**
* @ClassName : UdpClientDemo
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-07 20:28
*/
public class UdpClientDemo {
public static void main(String[] args) throws IOException {
InetAddress address = InetAddress.getByName("localhost");
byte[] sendData = "hello HaHa".getBytes();
DatagramPacket senPacket =new DatagramPacket(
sendData,sendData.length,address,8088
);
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.send(senPacket);
}
}
運(yùn)行結(jié)果:
其實(shí)udp是可以雙向通信,我們可以實(shí)現(xiàn)心跳機(jī)制,其實(shí)我們除了通信協(xié)議之外,還有他們的IO是比較有用的,
實(shí)現(xiàn)雙向通話:
package com.example.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @ClassName : ServerDemo1
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-07 20:43
*/
public class ServerDemo1 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(8088);
Socket socket = serverSocket.accept();
BufferedReader is = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);
PrintWriter os = new PrintWriter(socket.getOutputStream());
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
//得到客戶端的信息
System.out.println("client:"+is.readLine());
String line =sin.readLine();
while(!line.equals("bye")){
os.println(line);
os.flush();
System.out.println("Server:"+line);
System.out.println("client:"+is.readLine());
}
os.close();
is.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.example.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @ClassName : ServerDemo1
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-07 20:43
*/
public class ClientDemo1 {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1",8088);
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
PrintWriter os = new PrintWriter(socket.getOutputStream());
BufferedReader is = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);
String line =sin.readLine();
while (!line.equals("bye")){
os.println(line);
os.flush();
System.out.println("client"+line);
System.out.println("server"+is.readLine());
line = sin.readLine();
};
} catch (IOException e) {
e.printStackTrace();
}
}
}
運(yùn)行結(jié)果:
雙向通信的流程圖: