TCP&&UDP实战

TCP

TCP:客户端 

package com.alibaba.interview;

import java.io.PrintWriter;
import java.net.Socket;

/**
 * @author: WSC
 * @Create 2020/9/2 14:50
 */
public class TcpClient {

    public static void main(String[] args) throws Exception {
        String msg = "7e 7e 01 00 18 01 01 00 00 00 00 00 00 00 66 f7 80 a2 73 01 00 00 ff ff 04 00 37 00 00 00 46 82 33 41 c7 4f 28 40 4e dd 53 40 fa 2c ae 43 37 47 88 3c 00 00 b4 42 00 00 80 bf 00 00 80 bf f3 d7 b6 c6 6e 92 5a 40 56 e2 7e f9 ac 85 3d 40 ff ff 04 00 35 00 00 00 46 d2 2e 41 36 ee 40 40 19 61 4e 40 35 92 ab 43 d1 45 6e 3f 82 04 1f 43 00 00 80 bf 00 00 80 bf 6e df 41 44 77 92 5a 40 dd 20 e0 ac 7c 85 3d 40 ff ff 04 00 38 00 00 00 d0 09 d0 40 6a 23 0e 40 31 1a 2f 40 6f 14 ae 43 8e d7 87 40 4c 56 32 43 00 00 80 bf 00 00 80 bf a9 fa a2 c2 72 92 5a 40 94 9d c9 d1 a3 85 3d 40 ff ff 03 00 36 00 00 00 15 fa 8f 40 b8 a8 f4 3f ad 30 e6 3f 52 28 9b 43 8d 30 83 40 fa 77 09 43 00 00 80 bf 00 00 80 bf 1e 66 63 34 7c 92 5a 40 70 65 99 cd 8a 85 3d 40 ff ff 04 00 39 00 00 00 0f 0a 05 41 de 21 2e 40 e9 5e 46 40 b4 68 9b 43 bf 48 e5 3f b3 d3 8c 43 00 00 80 bf 00 00 80 bf d6 3f 38 92 7e 92 5a 40 f5 a5 e5 91 86 85 3d 40 72 fe 7e 7d";
        //创建一个Socket,跟本机的8888端口进行连接
        Socket socket = new Socket("127.0.0.1", 8888);
        //使用Socket创建一个PrintWriter进行写数据
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
        //发送数据
        printWriter.println(msg);
        //刷新一下,使得服务立马可以收到请求信息
        printWriter.flush();
        //printWriter.println(msg);

        //关闭资源
        printWriter.close();
        socket.close();
    }

}

TCP服务端

package com.alibaba.interview;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;

/**
 * @author: WSC
 * @Create 2020/9/2 14:49
 */
public class TcpServer {

    public static void main(String[] args) throws Exception {
        //创建一个ServerSocket监听一个端:8888
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("TCP服务器已经启动,端口是8888");
        //无限循环
        while (true) {
            //等待客户端的请求,如果有请求分配一个Socket
            Socket socket = serverSocket.accept();
            //根据标准输入构造一个BufferedReader对象
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String buffer = null;
            //循环读取输入的每一行数据
            while ((buffer = reader.readLine()) != null && !buffer.equals("")) {
                System.out.println(Arrays.toString(buffer.split(" ")));
                String replace = buffer.replace(" ", "");
                System.out.println(replace);
                System.out.println(replace.substring(0, 4));
                System.out.println(replace.substring(4, 6));
                System.out.println(replace.substring(6, 8));
                System.out.println(replace.substring(8, 12));
                System.out.println(replace.substring(12, 28));
                System.out.println(replace.substring(28, 44));
                String temp = replace.substring(44, replace.length() - 8);
                int index = 0;
                while ((index = temp.lastIndexOf("ffff")) != -1) {
                    System.out.println(temp.substring(index));
                    System.out.println(temp.substring(index).length());
                    System.out.println(temp.substring(index).substring(0, 2));
                    System.out.println(temp.substring(index).substring(2, 4));
                    System.out.println(temp.substring(index).substring(4, 6));
                    System.out.println(temp.substring(index).substring(8, 16));
                    System.out.println(temp.substring(index).substring(16, 24));
                    System.out.println(temp.substring(index).substring(24, 32));
                    System.out.println(temp.substring(index).substring(32, 40));
                    System.out.println(temp.substring(index).substring(40, 48));
                    System.out.println(temp.substring(index).substring(56, 64));
                    System.out.println(temp.substring(index).substring(64, 72));
                    System.out.println(temp.substring(index).substring(72, 80));
                    System.out.println(temp.substring(index).substring(80, 96));
                    System.out.println(temp.substring(index).substring(96, 112));
                    temp = temp.substring(0, index);
                }
                System.out.println(replace.substring(replace.length() - 8, replace.length() - 4));
                System.out.println(replace.substring(replace.length() - 4, replace.length()));
            }
            //通过Socket对象得到输出流,构造BufferedWrite对象
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            //模拟了http的请求头信息
            writer.write("HTTP/1.1 200 OK \r\n Content-Type:text/html \r\n charset=UTF-8\r\n\r\n ");
            //写一些html的体
            writer.write("http请求

这是一个HTTP请求!

"); //刷新输出流,使得数据立马发送 writer.flush(); //关闭 reader.close(); writer.close(); socket.close(); } } }

UDP

UDP发送端

package com.alibaba.interview;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author: WSC
 * @Create 2020/9/2 17:45
 */
public class UDPSendDemo {

    public static void main(String[] args) throws Exception {
        //创建一个DatagramSocket实例
        DatagramSocket datagramSocket = new DatagramSocket();
        //使用键盘输入构建一个BufferedReader
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            //转成byte
            byte[] bytes = line.getBytes();
            //创建一个用于发送的DatagramPacket对象
            DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("127.0.0.1"), 10005);
            //发送数据包
            datagramSocket.send(datagramPacket);
            if ("88".equals(line)) {//当输入88时结束发送
                break;
            }
        }
        System.out.println("UDP发送端已发送结束。。。。。。。。。");
        //关闭
        datagramSocket.close();
    }

}

UDP接收端

package com.alibaba.interview;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author: WSC
 * @Create 2020/9/2 17:45
 */
public class UDPReciveDemo {

    public static void main(String[] args) throws Exception {
        //创建一个DatagramSocket实例,并且把实例绑定到本机的地址,端口10005
        DatagramSocket datagramSocket = new DatagramSocket(10005);
        byte bytes[] = new byte[1024];
        //以一个空数组来创建 DatagramPacket,这个对象作用是接收DatagramSocket中的数据
        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
        System.out.println("UDP接收端已启动。。。。。。。。。");
        while (true) {//无限循环是必须要的,因为不知道数据何时来
            //接收到的数据包
            datagramSocket.receive(datagramPacket);
            //获取接收的数据
            byte[] data = datagramPacket.getData();
            //把数组转成字符
            String str = new String(data, 0, datagramPacket.getLength());
            //如果数据包中是88的信息,则跳出并且关闭
            if ("88".equals(str)) {
                break;
            }
            //打印数据
            System.out.println("接收到的数据为:" + str);
        }
        //关闭
        datagramSocket.close();
    }

}

注意tcp必须先启动服务端,再启动客户端;而udp启动顺序没有先后顺序。

你可能感兴趣的:(JavaWeb,socket,udp,tcp)