首页 > 编程语言 >基于UDP的网络编程

基于UDP的网络编程

时间:2024-09-21 17:21:35浏览次数:9  
标签:DatagramSocket UDP DatagramPacket java String 编程 基于 import new

基于UDP的网络编程

@[toc]

使用基于UDP的网络编程方法,完成远程计算等差数列的前n项和功能

(1)客户端将一等差数列的首项a1,公差d和项数n发送给服务器;

(2)服务器端接收到数据后对接收到的数据进行解析,将前n项和的计算结果发送给客户端;

(3)客户端收到后输出到控制台。


UDPSender

package MoocPart10_02_01;

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

/**
 * @author Laccoliths
 */
public class UDPSender {
    public static void main(String[] args) throws IOException {
        // 创建DatagramSocket
        DatagramSocket socket = new DatagramSocket();

        // 创建数据报包
        // 发送首项、公差和项数
        String sendContent = "1,2,3";
        System.out.println("输入内容:" + sendContent);
        // 明确接收方的详细信息,IP和Port
        DatagramPacket packet = new DatagramPacket(sendContent.getBytes(), sendContent.getBytes().length, InetAddress.getLocalHost(),8888);
        // 发送报包
        socket.send(packet);

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

UDPGetter

package MoocPart10_02_01;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author Laccoliths
 */
public class UDPGetter {
    public static void main(String[] args) throws IOException {
        // 创建DatagramSocket
        DatagramSocket socket = new DatagramSocket(8888);

        // 创建数据包
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        socket.receive(packet);

        // 处理数据
        String line = new String(buffer);
        // 分割字符串
        String[] input = line.split(",");
        int a1 = Integer.parseInt(input[0]);
        int d = Integer.parseInt(input[1]);
        int n = Integer.parseInt(input[2].trim());
        double result = n*a1+n*(n-1)/d;
        System.out.println("首项为:"+a1+",公差为:"+d+",项数为:"+n+",的前n项和为:" +result);

        // 关闭
        socket.close();
    }

}

使用基于UDP的网络编程方法,完成远程小写转换和校验功能

(1)客户端将一等差数列的首项a1,公差d和项数n发送给服务器;

(2)服务器端接收到数据后对接收到的数据进行解析,将前n项和的计算结果发送给客户端;

(3)客户端收到后输出到控制台。


UDPSender

package MoocPart10_02_02;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * 客户端
 * @author Laccoliths
 */
public class UDPSender {
    public static void main(String[] args) throws IOException {
        // 创建DatagramSocket
        DatagramSocket socket = new DatagramSocket();

        System.out.print("请输入一段字符串:");

        // 创建数据报包
        // 接收客户键盘输入的字符串
        String sendContent = new Scanner(System.in).next();
        // 明确接收方的详细信息,IP和Port
        DatagramPacket packet = new DatagramPacket(sendContent.getBytes(), sendContent.getBytes().length, InetAddress.getLocalHost(),8888);
        // 发送包
        socket.send(packet);

        System.out.println("输入的内容为:" + sendContent);

        // 接收包
        byte[] buffer = new byte[1024];
        DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
        socket.receive(receivePacket);

        //5.处理
        String str = new String(buffer);
        System.out.println("处理后的内容:"+str);


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

UDPGetter

package MoocPart10_02_02;

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.nio.charset.StandardCharsets;

/**
 * @author Laccoliths
 */
public class UDPGetter {
    public static void main(String[] args) throws IOException {
        // 创建DatagramSocket
        DatagramSocket socket = new DatagramSocket(8888);

        // 创建数据包
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        socket.receive(packet);

        // 处理数据
        String line = new String(buffer);
        String lower = line.toLowerCase();
        System.out.print("小写处理后的内容:" + lower);

        boolean isOK = false;

        // 校验是否是小写
        if (checkString(lower)) {
            isOK = true;
            System.out.println("处理成功!");
            toTxt(lower);
        }else {
            System.out.println("处理失败!");
        }
        if (isOK){
            // 回复的内容包
            DatagramPacket replyPacket = new DatagramPacket(lower.getBytes(), lower.getBytes().length, packet.getAddress(), packet.getPort());
            socket.send(replyPacket);
        }


        // 关闭
        socket.close();
    }


    /**
     * 检验是否是小写,全小写返回true,否则返回false
     * @param str
     */
    private static boolean checkString(String str) {
        char ch;

        boolean capitalFlag = false;

        boolean lowerCaseFlag = false;

        boolean numberFlag = false;

        for(int i=0;i < str.length();i++) {
            ch = str.charAt(i);

            if( Character.isDigit(ch)) {
                numberFlag = true;

            }

            else if (Character.isUpperCase(ch)) {
                capitalFlag = true;

            } else if (Character.isLowerCase(ch)) {
                lowerCaseFlag = true;

            }

            if(numberFlag && capitalFlag && lowerCaseFlag) {
                return false;
            }

        }

        return true;

    }

    /**
     * 将字符串写入D:/character.txt文件中
     * @param str
     * @throws IOException
     */
    public static void toTxt(String str) throws IOException {
        FileWriter fileWriter = new FileWriter("D://character.txt");
        fileWriter.write(str.trim());
        fileWriter.flush();
        fileWriter.close();

    }
}

标签:DatagramSocket,UDP,DatagramPacket,java,String,编程,基于,import,new
From: https://blog.51cto.com/Laccoliths/12074987

相关文章