Java教程

Java自学-网络编程(4)

本文主要是介绍Java自学-网络编程(4),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Java自学-网络编程(4)

1、UDP消息发送

发短信:不用连接,需要知道对方的地址。

发送端

package lesson03;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/7 12:59
 * FileName: UdpCilentDemo01
 * Description:
 */
//不需要连接服务器。
public class UdpCilentDemo01 {
    public static void main(String[] args) throws Exception {
        //1、建立一个Socket。
        DatagramSocket socket = new DatagramSocket();

        //2、建立包。
        String msg="蔡伟,你好!";

        //发送给谁
        InetAddress localhost = InetAddress.getByName("localhost");
        int port=9090;
        //数据,数据的长度,要发送给谁
        DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);

        //3、发送包。
        socket.send(packet);

        //4、 关闭流。
        socket.close();
    }
}

接收端

package lesson03;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/7 13:09
 * FileName: UdpServerDemo01
 * Description:
 */
//还是要等待客户端的连接。
public class UdpServerDemo01 {
    public static void main(String[] args) throws Exception {
        //开放端口。
        DatagramSocket socket = new DatagramSocket(9090);
        //接收数据包
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);//接收

        socket.receive(packet);//阻塞接收。

        System.out.println(packet.getAddress().getHostAddress());
        System.out.println(new String(packet.getData(),0,packet.getLength()));


        //关闭连接
        socket.close();
    }
}

2、UDP聊天实现

循环发送消息

package chat;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/7 13:24
 * FileName: UdpSenderDemo01
 * Description:
 */
public class UdpSenderDemo01 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(8888);

        //准备数据。控制台读取,System.in
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));


        while(true){
            String data = reader.readLine();
            byte[] datas = data.getBytes();

            DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",6666));
            socket.send(packet);

            if(datas.equals("bye")){
                break;
            }
        }

        socket.close();
    }
}

循环接收消息

package chat;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/7 13:25
 * FileName: UdpReceiveDemo01
 * Description:
 */
public class UdpReceiveDemo01 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(6666);

        while(true){
            //准备接收包裹
            byte[] container = new byte[1024];
            DatagramPacket packet = new DatagramPacket(container,0,container.length);

            socket.receive(packet);//阻塞式接收包裹。

            //断开连接
            byte[] data = packet.getData();
            String receiveData = new String(data, 0, data.length);
            receiveData=receiveData.trim();
            System.out.println(receiveData);

            if(receiveData.equals("bye")){
                break;
            }
        }

        socket.close();
    }
}

3、UDP多线程在线咨询

在线咨询:两个人都可以是发送方,也都可以是接收方。

4、URL

image-20210807143756734

这篇关于Java自学-网络编程(4)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!