简体   繁体   中英

How to send an array of String over UDP datagram in java?

I have written a program in java to retrieve data from a database and store it inside an array of String named as Records . the program sends String msg to the server which contain test client ..Could you please tell me how to send String [] Records instead of String msg ?..

I appreciate your help Thank you

here is the program:

import java.io.*;
import java.net.*;
import java.sql.*;

public class Client {

public static void main(String[] args) {
String [] Records= new String[5];
 int x = 1;
 while (true){
        try {

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:DDS_DSN");
            Statement st = con.createStatement();



            PreparedStatement ps = con.prepareStatement(
                    "SELECT * FROM request_dns " +
                    "WHERE id = ?");
            ps.setInt(1, x);
ResultSet rs = ps.executeQuery();


            while ( rs.next() ) {
                Records[0]= rs.getString("id");
                Records[1]= rs.getString("source_ip");
                Records[2]= rs.getString("source_port");
                Records[3]= rs.getString("destination_ip");
                Records[4]= rs.getString("destination_port");

                System.out.println(Records[0]);
                System.out.println(Records[1]);
                System.out.println(Records[2]);
                System.out.println(Records[3]);
                System.out.println(Records[4]);
            }
            con.close();
        } catch (Exception e) {
            System.out.println(e);

        }


        DatagramSocket skt;

        try{

        skt =new DatagramSocket();
        String msg ="test client"; ///////////////// sent messege
        byte [] b = msg.getBytes();
        InetAddress host=InetAddress.getByName("localhost"); ///////// inserting the destination IP address

        int serverSocket =6700; ///////////////////port number

        ////////////////////////////////packet sender
        DatagramPacket request = new DatagramPacket (b,b.length,host,serverSocket);
        skt.send (request);

        /////////////////////packet receiver
       byte [] buffer = new byte [1000];
        DatagramPacket reply = new DatagramPacket (buffer,buffer.length);
        skt.receive(reply);
        System.out.println("client received \t"+new String(reply.getData()) );

        skt.close();




        }

        catch(Exception ex){

        }

x++;
}
    }

}

There is many, many way do this but perhaps the simplest is to turn the multiple Strings into one String and back again. That way the rest of your code would be as it is now.

String[] array = "a,b,c,d,e".split(",");
// to join multiple strings into one.
String s = Stream.of(array).collect(Collectors.joining(","));
// to split multiple strings from on.
String[] array2 = s.split(",");

You might find a better delimiter such as "|" or "\￿" depending on the encoding you use.

Note: UDP is a lossy protocol which means there is no guarantee a packet will be received and the sender has no way of know this has happened. You can add a protocol to make the transmission loss less however it is usually simpler to use an existing library which does this or TCP.

I don't know of a way to send String[]. If you need to send, string after string in the same datagram (string[0] + string[1] + ...) you will need some protocol/convention to have a separator or marker between strings. It could be a special character you know you never use in the strings or you could use TLV (Type, Length, Value) format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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