Can you provide an example of a byte buffer transferred between two java classes via UDP datagram?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Hows’ this ?
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; public class Server { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(new InetSocketAddress(5000)); byte[] message = new byte[512]; DatagramPacket packet = new DatagramPacket(message, message.length); socket.receive(packet); System.out.println(new String(packet.getData(), packet.getOffset(), packet.getLength())); } }import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; public class Client { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(); socket.connect(new InetSocketAddress(5000)); byte[] message = 'Oh Hai!'.getBytes(); DatagramPacket packet = new DatagramPacket(message, message.length); socket.send(packet); } }