Petr Faltus development

Java network communication over TCP socket

Petr Faltus development

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

net_tcp_receiver.java

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

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

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


package cz.petrfaltus.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import static java.lang.System.out;

public class net_tcp_receiver {
    private static final int RECEIVER_PORT = 10000;
    private static final int RECEIVED_MESSAGES_MAX = 10;

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

        try {
            ServerSocket socket = new ServerSocket(RECEIVER_PORT, RECEIVED_MESSAGES_MAX);
            out.println("- socket created, bound on port " + RECEIVER_PORT + " and is listening for max " + RECEIVED_MESSAGES_MAX + " messages");

            boolean stop = false;
            while (stop == false) {
                Socket msgsock = socket.accept();
                out.println("- socket accepted request");

                InetAddress peer = msgsock.getInetAddress();
                InetAddress local = msgsock.getLocalAddress();
                out.println("- peer connect from " + peer.getHostAddress() + ":" + msgsock.getPort() + " on " + local.getHostAddress() + ":" + msgsock.getLocalPort());

                InputStreamReader isr = new InputStreamReader(msgsock.getInputStream());
                BufferedReader br = new BufferedReader(isr);

                String msg = br.readLine();
                out.println("- message " + msg.length() + "B received");
                out.println("|" + msg + "|");

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

                msgsock.close();
                out.println("- socket closed request");
            }

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

net_tcp_sender.java

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


package cz.petrfaltus.net;

import java.io.IOException;
import java.io.PrintStream;

import java.net.InetAddress;
import java.net.Socket;

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

import java.util.Date;

import static java.lang.System.out;

public class net_tcp_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("TCP IPv4 stream socket sender");

        try {
            Socket socket = new Socket(RECEIVER_ADDRESS, RECEIVER_PORT);
            out.println("- socket created for sending to " + RECEIVER_ADDRESS + ":" + RECEIVER_PORT);

            InetAddress peer = socket.getInetAddress();
            InetAddress local = socket.getLocalAddress();
            out.println("- for " + peer.getHostAddress() + ":" + socket.getPort() + " bound on " + local.getHostAddress() + ":" + socket.getLocalPort());

            PrintStream output_stream = new PrintStream(socket.getOutputStream());

            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 + "|");

            output_stream.print(msg);
            out.println("- message " + msg.length() + "B sent");

            output_stream.close();
            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