简体   繁体   English

使用UDP的Java套接字编程

[英]Java Socket Programming with UDP

I am trying to send some number and hash using server client. 我正在尝试使用服务器客户端发送一些数字和哈希。 I am using the UDP protocol to do so. 我正在使用UDP协议来这样做。 I create a packet using wither the number or the hash, as I was supposed to, and sent it using the socket. 我按照预期的那样使用数字或哈希值创建了一个数据包,并使用套接字发送了它。 The client receives it and consequently, just prints the received value. 客户收到它,因此,只打印接收到的值。 But when I receive number and hash in the client, I don't get the same. 但是,当我在客户端收到数字和哈希值时,我不会得到相同的结果。 What can be the possible issue? 可能是什么问题?

I am giving my server and client code and the output I get here. 我正在提供服务器和客户端代码以及到达此处的输出。

Server Code: 服务器代码:

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);

    }

}

Client Code: 客户代码:

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);
    }

}

The output that I get is given below: 我得到的输出如下:

Server Output: 服务器输出:

[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]

Client Output: [42424239363541423043383044363533384346323138344241424144324135363441303130333736373132303132424430374230414639324443443330393744, 34344338303331434230333641373335304438423942383630334146363632413442394344424432463936453844354445354146343335433943333544413639, 36453430303138373143304346323743373633344546314443343738343038463634323431304644334134343445324138384533303146354334413335413444, 41343645333736333246413643413531413133464533394135363742334332334232384332463437443841463642453942443633453033304532313442413338, 43383337363439434345343346323732393133384537324343333135323037303537414338323539394135394245373237363541343737463232443134413534, 43443730424541303233463735324130353634414242364544303844343243313434304632453333453239393134453535453042453135393545323446343541, 37393032363939424534324338413845343646424242343530313732363531374538364232324335364131383946373632354136444134393038314232343531, 31393538314532374445374345443030464631434535304232303437453741353637433736 客户端输出:[42424239363541423043383044363533384346323138344241424144324135363441303130333736373132303132424430374230414639324443443330393744,34344338303331434230333641373335304438423942383630334146363632413442394344424432463936453844354445354146343335433943333544413639,36453430303138373143304346323743373633344546314443343738343038463634323431304644334134343445324138384533303146354334413335413444,41343645333736333246413643413531413133464533394135363742334332334232384332463437443841463642453942443633453033304532313442413338,43383337363439434345343346323732393133384537324343333135323037303537414338323539394135394245373237363541343737463232443134413534,43443730424541303233463735324130353634414242364544303844343243313434304632453333453239393134453535453042453135393545323446343541,37393032363939424534324338413845343646424242343530313732363531374538364232324335364131383946373632354136444134393038314232343531,31393538314532374445374345443030464631434535304232303437453741353637433736 423143424145424142453545463033463743333031374242354237, 45463244313237444533374239343242414144303631343545353442304336313941314632323332374232454242434642454337384635353634414645333944, 31413635363235393045463139443130343544303643343035353734324433383238384539453644434437314343444535434545383046314435413737344542] [3833, 3834, 3933, 3832, 3537, 3839, 37, 39, 35, 3530] 423143424145424142453543033033743743333031374242354237,45463244313237444533374239343242414144303631343545544342544336944314944323374374232454242454242434542544337384635353634414645333944,3141363536323539304546313946313044354430364,30353537343343343343 3834 3873374354 374374354

I tried, but couldn't format the client output 我尝试过,但无法格式化客户端输出

Edit: I have tried sending only the numbers alone and it still doesn't work. 编辑:我试图只发送数字,它仍然无法正常工作。 Please let me know what can be the possible issues? 请让我知道可能是什么问题?

You are sending hex encoded data from the server. 您正在从服务器发送十六进制编码的数据。 Then your client should not convert that hex again with DatatypeConverter.printHexBinary(num) 然后,您的客户端不应再次使用DatatypeConverter.printHexBinary(num)转换该十六进制

Instead of the client doing 代替客户做

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

Just do 做就是了

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

(And do it similarly in the else clause of your client.) (并且在客户端的else子句中也进行类似操作。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM