Perform Connectionless Communication Between Producer and Consumer

See how to perform connectionless communication between the producer and the consumer.

DatagramSocket – Data producer

This class produces data and sends it to the specified IP address and port. It really does not bother if a real consumer is available.

DatagramSocket – Data consumer

This class consumes data from the port.

import java.net.*;  import java.io.*;public class DataProducer{       public static void main(String args[])   {      DataProducer dataProducer = new DataProducer();      dataProducer.proceed();   }      private void proceed()   {      try{         int port = 4567;         String ipAddress = "127.0.0.1" ;         DatagramSocket datagramSocket = new DatagramSocket();           String messageString = "Message from Producer";           InetAddress inetIpAddress = InetAddress.getByName(ipAddress);                     DatagramPacket datagramPacket = new DatagramPacket(messageString.getBytes(), messageString.length(), inetIpAddress, port);           datagramSocket.send(datagramPacket);           System.out.println("Message sent");           datagramSocket.close();        }catch(IOException ioe)      {         System.out.println("Exception: " + ioe.getMessage() );      }   }  }  /*

Expected output:

[[email protected]]# java DataProducerMessage sent*/import java.net.*;  import java.io.*;public class DataConsumer{       public static void main(String args[])   {      DataConsumer dataConsumer = new DataConsumer();      dataConsumer.proceed();   }      private void proceed()   {      try{         int port = 4567;         DatagramSocket datagramSocket = new DatagramSocket(port);           byte[] buf = new byte[1024];           DatagramPacket datagramPacket = new DatagramPacket(buf, 1024);           datagramSocket.receive(datagramPacket);           String receivedMessageString = new String(datagramPacket.getData(), 0, datagramPacket.getLength());           System.out.println(receivedMessageString);           datagramSocket.close();        }catch(IOException ioe)      {         System.out.println("Exception: " + ioe.getMessage() );      }   }  }  /*

Expected output:

[[email protected]]# java DataConsumerMessage from Producer*/

		
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS