简体   繁体   中英

Communication between Java programs with DatagramSocket

I'm trying to communicate two java programs, no problem with this. Program A send a message with the word "token" and Program B receive the message and print it. The problem I have is I would like to send before length of message to Program B to create the array with the accurate length but i don't know how can i do it.

public class ProgramA {
public static void main(String[] args) {
    try {
        DatagramSocket datagramSocket = new DatagramSocket();
        String message = "token";
        InetAddress addr = InetAddress.getByName("localhost");
        DatagramPacket datagram = new DatagramPacket(message.getBytes(),message.getBytes().length, addr, 5556);
        datagramSocket.send(datagram);
        datagramSocket.close();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

public class ProgramB {
public static void main(String[] args) {
    try {
        InetSocketAddress addr = new InetSocketAddress("localhost", 5556);
        DatagramSocket datagramSocket = new DatagramSocket(addr);
        byte[] message = new byte[5];
        DatagramPacket datagram = new DatagramPacket(message, message.length);
        datagramSocket.receive(datagram);
        datagramSocket.close();
        for(int i = 0; i < message.length; i++) {
            System.out.print((char)message[i]);
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

If you know that the length of the message is maximum X bytes, you could use something like this:

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

public class ProgramA {
    public static void main(String[] args) {
        try {
            System.out.println("Started");
            DatagramSocket datagramSocket = new DatagramSocket();
            String message = "tokenasdfasdfasdfasdf";
            InetAddress addr = InetAddress.getByName("localhost");
            DatagramPacket datagramLength = new DatagramPacket(new byte[]{(byte)message.length()}, 1, addr, 5556);
            DatagramPacket datagram = new DatagramPacket(message.getBytes(),message.getBytes().length, addr, 5556);
            datagramSocket.send(datagramLength);
            datagramSocket.send(datagram);
            datagramSocket.close();
            System.out.println("Done");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

package experiment;

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

public class ProgramB {
    public static void main(String[] args) {
        try {
            InetSocketAddress addr = new InetSocketAddress("localhost",     5556);
            DatagramSocket datagramSocket = new DatagramSocket(addr);
            byte[] messageLength = new byte[1];
            DatagramPacket datagramLength = new DatagramPacket(messageLength, 1);
            datagramSocket.receive(datagramLength);
            byte[] message = new byte[(int)messageLength[0]];
            DatagramPacket datagram = new DatagramPacket(message, message.length);
            datagramSocket.receive(datagram);
            datagramSocket.close();
            for(int i = 0; i < message.length; i++) {
                System.out.print((char)message[i]);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

This works assumed the length of the message does not exceed 127; otherwise your datagramPacket with the lenght of the message has to contain more than one byte

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