Java 使用 UDP 进行套接字编程

Java Socket Programming with UDP

我正在尝试使用服务器客户端发送一些数字和散列。我正在使用 UDP 协议来这样做。我按照预期使用数字或哈希创建数据包,并使用套接字发送它。客户端收到它,因此只打印接收到的值。但是当我在客户端收到数字和哈希值时,我得到的并不相同。可能的问题是什么?

我在这里提供我的服务器和客户端代码以及我得到的输出。

服务器代码:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import javax.xml.bind.DatatypeConverter;

public class main {

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {

        int n = 10;
        SecureRandom rand = new SecureRandom();
        ArrayList<String> clue = new ArrayList<String>();
        ArrayList<String> cluenum = new ArrayList<String>();
        MessageDigest digestnew = MessageDigest.getInstance("SHA-256");
        byte[] finalmsgblock = "End".getBytes();

        for(int i = 0; i < n; i++)
        {
            int num = rand.nextInt(100);
            String num1 = Integer.toString(num);
            byte[] num2 = digestnew.digest(num1.getBytes(StandardCharsets.UTF_8));
            String num3 = "Clue: " + DatatypeConverter.printHexBinary(num2);
            clue.add(i,num3);
            cluenum.add(i, num1);

        }
        System.out.println(clue);
        System.out.println(cluenum);

        // Create a Socket
        DatagramPacket packet, packet1;
        InetAddress address;
        DatagramSocket socket;
        socket = new DatagramSocket();
        address = InetAddress.getByName("127.0.0.1");

        for(int i = 0; i < n; i++)
        {
            byte[] newdata = clue.get(i).getBytes(StandardCharsets.UTF_8);
            packet = new DatagramPacket (newdata, newdata.length, address, 1502);
            socket.send(packet);
            System.out.println("Sent clue!");

            byte[] newdata1 = cluenum.get(i).getBytes(StandardCharsets.UTF_8);
            packet1 = new DatagramPacket (newdata1, newdata1.length, address, 1502);
            socket.send(packet1);
            System.out.println("Sent cluenum!");
        }

        DatagramPacket  packet11 = new DatagramPacket (finalmsgblock, finalmsgblock.length, address, 1502);
        socket.send(packet11);

    }

}

客户代码:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.ArrayList;
import javax.xml.bind.DatatypeConverter;

public class main {

    public static void main(String[] args) throws IOException {
        int n = 10;
        SecureRandom rand = new SecureRandom();
        ArrayList<String> clue = new ArrayList<String>();
        ArrayList<String> cluenum = new ArrayList<String>();

        // Create the socket
        int port = 1502;
        DatagramSocket socket;
        byte[] buf = new byte[10000];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        InetAddress address = InetAddress.getByName("127.0.0.1");
        socket = new DatagramSocket(port, address);
        int i1 = 0;
        int i2 = 0;

        while(true)
        {
            socket.receive (packet);
            byte[] data = new byte[packet.getLength()];
            System.arraycopy(packet.getData(), packet.getOffset(), data, 0, packet.getLength());
            String n1 = new String(data);
            if(new String(data).equals("End"))
            {
                break;
            }
            else
            {
                if(n1.startsWith("Clue: "))
                {
                    String n2 = (n1.replace("Clue: ", ""));
                    byte[] num = n2.getBytes(StandardCharsets.UTF_8);
                    String num1 = DatatypeConverter.printHexBinary(num);
                    clue.add(i1, num1);
                    i1 = i1 + 1;
                }
                else
                {
                    byte[] num = n1.getBytes(StandardCharsets.UTF_8);
                    String num1 = DatatypeConverter.printHexBinary(num);
                    cluenum.add(i2, num1);
                    i2 = i2 + 1;
                }
            }
        }

        System.out.println(clue);
        System.out.println(cluenum);
    }

}

我得到的输出如下:

服务器输出:

[Clue: BBB965AB0C80D6538CF2184BABAD2A564A010376712012BD07B0AF92DCD3097D, 
 Clue: 44C8031CB036A7350D8B9B8603AF662A4B9CDBD2F96E8D5DE5AF435C9C35DA69,
 Clue: 6E4001871C0CF27C7634EF1DC478408F642410FD3A444E2A88E301F5C4A35A4D, 
 Clue: A46E37632FA6CA51A13FE39A567B3C23B28C2F47D8AF6BE9BD63E030E214BA38,                 
 Clue: C837649CCE43F2729138E72CC315207057AC82599A59BE72765A477F22D14A54,    
 Clue: CD70BEA023F752A0564ABB6ED08D42C1440F2E33E29914E55E0BE1595E24F45A, 
 Clue: 7902699BE42C8A8E46FBBB4501726517E86B22C56A189F7625A6DA49081B2451, 
 Clue: 19581E27DE7CED00FF1CE50B2047E7A567C76B1CBAEBABE5EF03F7C3017BB5B7, 
 Clue: EF2D127DE37B942BAAD06145E54B0C619A1F22327B2EBBCFBEC78F5564AFE39D, 
 Clue: 1A6562590EF19D1045D06C4055742D38288E9E6DCD71CCDE5CEE80F1D5A774EB]
 [83, 84, 93, 82, 57, 89, 7, 9, 5, 50]

客户端输出: [42424239363541423043383044363533384346323138344241424144324135363441303130333736373132303132424430374230414639324443443330393744, 34344338303331434230333641373335304438423942383630334146363632413442394344424432463936453844354445354146343335433943333544413639, 36453430303138373143304346323743373633344546314443343738343038463634323431304644334134343445324138384533303146354334413335413444, 41343645333736333246413643413531413133464533394135363742334332334232384332463437443841463642453942443633453033304532313442413338, 43383337363439434345343346323732393133384537324343333135323037303537414338323539394135394245373237363541343737463232443134413534, 43443730424541303233463735324130353634414242364544303844343243313434304632453333453239393134453535453042453135393545323446343541, 37393032363939424534324338413845343646424242343530313732363531374538364232324335364131383946373632354136444134393038314232343531, 31393538314532374445374345443030464631434535304232303437453741353637433736423143424145424 142453545463033463743333031374242354237, 45463244313237444533374239343242414144303631343545353442304336313941314632323332374232454242434642454337384635353634414645333944, 31413635363235393045463139443130343544303643343035353734324433383238384539453644434437314343444535434545383046314435413737344542] [3833, 3834, 3933, 3832, 3537, 3839, 37, 39, 35, 3530]

我试过了,但无法格式化客户端输出

编辑: 我试过只发送号码,但还是不行。请让我知道可能出现的问题是什么?

您正在从服务器发送十六进制编码的数据。然后你的客户不应该用 DatatypeConverter.printHexBinary(num)

再次转换那个十六进制

而不是客户端做

   String n2 = (n1.replace("Clue: ", ""));
   byte[] num = n2.getBytes(StandardCharsets.UTF_8);
   String num1 = DatatypeConverter.printHexBinary(num);
   clue.add(i1, num1);

随心所欲

 String n2 = (n1.replace("Clue: ", ""));
 clue.add(i1, n2);

(并在您的客户的 else 子句中执行类似操作。)