当前位置: 首页 > 科技新闻 >

「滴滴上车」聊聊java版TCP通信协议demo练习,走过

时间:2020-06-08 17:54来源:网络整理 浏览:
前言最近在向架构方向转,一直在查看分布式架构相关的知识点,本篇博客主要是对TCP通信协议的java代码练习,纯干货,没有课本知识:demo:
前言

最近在向架构方向转,一直在查看分布式架构相关的知识点,本篇博客主要是对TCP通信协议的java代码练习,纯干货,没有课本知识:

「滴滴上车」聊聊java版TCP通信协议demo练习,走过路过不要错过
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();

}

}

}

}

运行结果:

「滴滴上车」聊聊java版TCP通信协议demo练习,走过路过不要错过

小结:

我们通过简单的创建demo一个客户端 一个服务端,把数据传递过来了,我们这是使用Socket套接字进行练习,创建连接,监听端口号,进行数据传递。

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) {

//创建服务并且接收数据包

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);

}

}

运行结果:

「滴滴上车」聊聊java版TCP通信协议demo练习,走过路过不要错过

其实udp是可以双向通信,我们可以实现心跳机制,其实我们除了通信协议之外,还有他们的IO是比较有用的,

实现双向通话:

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();

}

}

}

运行结果:

「滴滴上车」聊聊java版TCP通信协议demo练习,走过路过不要错过


「滴滴上车」聊聊java版TCP通信协议demo练习,走过路过不要错过

双向通信的流程图:

「滴滴上车」聊聊java版TCP通信协议demo练习,走过路过不要错过


推荐内容