【tomcat】02 Web服务器机制 之 套接字通信 代码实现

一、单播

package com.example.tomcat.http;

import lombok.extern.slf4j.Slf4j;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * @author haoxiansheng
 */
@Slf4j
public class SocketClientTest {
    public static void main(String[] args) {
        Socket socket = null;
        try {
            // 1、绑定连接
            socket = new Socket("localhost", 8888);
            // 2、创建输入输出流
            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());
            // 3、发送数据 获得响应
            outputStream.writeUTF("我是客户端,我想要获取XXX");
            log.info("input=>{}", inputStream.readUTF());
            // 4、关闭连接
            socket.close();
        } catch (IOException e) {
            log.info("e=>{}", e.getMessage());
        }

    }
}


package com.example.tomcat.http;

import lombok.extern.slf4j.Slf4j;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author haoxiansheng
 */
@Slf4j
public class SocketServerTest {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            // 1、绑定端口,阻塞等待连接
            serverSocket = new ServerSocket(8888);
            Socket socket = serverSocket.accept();

            // 2、创建输入输出流
            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());

            log.info("服务器收到客户端的连接请求=>{} ", inputStream.readUTF());
            // 3、向客户端发送响应
            outputStream.writeUTF("接收连接请求,连接成功");

            // 4、完成响应关闭连接
            socket.close();
            serverSocket.close();
        } catch (IOException e) {
            log.error("e=>{}", e.getMessage());
        }
    }
}


二、组播

package com.example.tomcat.http;

import lombok.extern.slf4j.Slf4j;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;


/**
 * @author haoxiansheng
 */
@Slf4j
public class Node1 {
    private static int port = 8000;

    private static String address = "228.0.0.4";

    public static void main(String[] args) {
        try {
            InetAddress group = InetAddress.getByName(address);
            MulticastSocket multicastSocket = new MulticastSocket(port);

            // 申请加入组中
            multicastSocket.joinGroup(group);

            while (true) {
                String msg = "hello ! I am node1";
                byte[] buffer = msg.getBytes();

                // 发送消息
                DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, port);
                multicastSocket.send(packet);
                Thread.sleep(1000);
            }
            // 关闭连接
            // multicastSocket.close(); 实际操作完应关闭

        } catch (Exception e) {
            log.error("e=>{}", e.getMessage());
        }
    }
}


package com.example.tomcat.http;

import lombok.extern.slf4j.Slf4j;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

/**
 * @author haoxiansheng
 */
@Slf4j
public class Node2 {
    private static int port = 8000;

    private static String address = "228.0.0.4";

    public static void main(String[] args) {
        try {
            InetAddress group = InetAddress.getByName(address);
            MulticastSocket multicastSocket = new MulticastSocket(port);

            // 申请加入组中
            multicastSocket.joinGroup(group);
            byte[] buffer = new byte[1024];

            while (true) {

                // 接收消息
                DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, port);
                multicastSocket.receive(packet);
                String msg = new String(packet.getData(), 0, packet.getLength());
                log.info("receive from=>{}", msg);
            }
            // 关闭连接
            // multicastSocket.close(); 实际操作完应关闭

        } catch (Exception e) {
            log.error("e=>{}", e.getMessage());
        }
    }
}



三、广播


package com.example.tomcat.http;

import lombok.extern.slf4j.Slf4j;

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

/**
 * @author haoxiansheng
 */
@Slf4j
public class BroadCastReceiver {
    public static void main(String[] args) {
        try{
            DatagramSocket datagramSocket = new DatagramSocket(8888);
            byte[] buf = new byte[5];
            DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
            datagramSocket.receive(datagramPacket);
            log.info("=====>msg {}", new String(buf));
        } catch (Exception e) {
            log.error("e=>{}", e.getMessage());
        }
    }
}

package com.example.tomcat.http;

import lombok.extern.slf4j.Slf4j;

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


/**
 * @author haoxiansheng
 */
@Slf4j
public class BroadCastSender {
    public static void main(String[] args) {
        try {
            InetAddress ip = InetAddress.getByName("192.168.0.255");
            DatagramSocket datagramSocket = new DatagramSocket();

            String msg = "hello !!!";
            DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(), msg.getBytes().length, ip, 8888);
            datagramSocket.send(datagramPacket);

            datagramSocket.close();
        } catch (Exception e) {
            log.error("e=>{}", e.getMessage());
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值