Petr Faltus development

Java network communication over UDP socket

Petr Faltus development

Example source code in Java how to communicate in IPv4 over network UDP socket. The sender and the receiver.

net_udp_receiver.java

GitHub repository: network-socket-communication-source-codes

git clone https://github.com/petrfaltus/network-socket-communication-source-codes.git

Directories to net_udp_receiver.java in the repository: java/cz/petrfaltus/net


package cz.petrfaltus.net;

import java.io.IOException;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import static java.lang.System.out;

public class net_udp_receiver {
    private static final int RECEIVER_PORT = 10000;
    private static final int BUFFER_SIZE = 4096;

    public static void main(String[] args) {
        out.println("UDP IPv4 datagram socket receiver");

        try {
            DatagramSocket socket = new DatagramSocket(RECEIVER_PORT);
            out.println("- socket created and bound on port " + RECEIVER_PORT);

            byte[] buffer = new byte[BUFFER_SIZE];

            boolean stop = false;
            while (stop == false) {
                DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

                socket.receive(packet);
                String msg = new String(packet.getData(), 0, packet.getLength());
                InetAddress peer = packet.getAddress();
                out.println("- message " + msg.length() + "B of " + packet.getLength() + "B received from " + peer.getHostAddress() + ":" + packet.getPort() + " on port " + socket.getLocalPort());
                out.println("|" + msg + "|");

                if (msg.equals("stop")) {
                    // received message "stop" to stop the receiver
                    stop = true;
                }
            }

            socket.close();
            out.println("- socket closed");
        } catch (IOException e) {
            out.println("- " + e);
        }
    }
}

net_udp_sender.java

Directories to net_udp_sender.java in the repository: java/cz/petrfaltus/net


package cz.petrfaltus.net;

import java.io.IOException;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Date;

import static java.lang.System.out;

public class net_udp_sender {
    private static final String RECEIVER_ADDRESS = "127.0.0.1";
    private static final int RECEIVER_PORT = 10000;

    public static void main(String[] args) {
        out.println("UDP IPv4 datagram socket sender");

        try {
            DatagramSocket socket = new DatagramSocket();
            out.println("- socket created");

            String msg;
            if (args.length > 0) {
                msg = args[0]; // message is the first parameter, for example "stop" to stop the receiver
            } else {
                Date now = new Date();
                DateFormat date_format = new SimpleDateFormat("d.M.yyyy 'in' H:mm:ss");

                msg = "This is Java message sent at " + date_format.format(now);
            }
            out.println("|" + msg + "|");

            byte[] buffer = msg.getBytes();
            InetAddress receiver_address = InetAddress.getByName(RECEIVER_ADDRESS);
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length, receiver_address, RECEIVER_PORT);

            InetAddress peer = packet.getAddress();
            out.println("- for " + peer.getHostAddress() + ":" + packet.getPort() + " bound on port " + socket.getLocalPort());

            socket.send(packet);
            out.println("- message " + buffer.length + "B of " + msg.length() + "B sent to " + RECEIVER_ADDRESS + ":" + RECEIVER_PORT);

            socket.close();
            out.println("- socket closed");
        } catch (IOException e) {
            out.println("- " + e);
        }
    }
}

  Previous example All examples Following example  

  

🤝 Your IP address is 3.238.98.39


💻💽💾 Useful examples and tools for home and professional programming.


© Petr Faltus - 2021