Communication Between Java Applications (1) – Socket Programming

There are too many ways to send a message from one java application to another. Most known method is using a socket.

Let’s assume that we have a server which receive a message from a client and reverse that message text and then send it back.


Server.java

package com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	public static void main(String[] args) throws IOException {

		System.out.println("Server is starting...");

		ServerSocket serverSocket = null;
		Socket clientSocket = null;

		try {
			serverSocket = new ServerSocket(8081);
			System.out.println("Server is ready!");
		} catch (Exception e) {
			System.out.println("Initializing error. Try changing port number!" + e);
		}

		clientSocket = serverSocket.accept();

		//use sending message to client
		PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
		//use receiving message from client
		BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

		String message = null;

		while (true) {
			//send message
			out.println("Write a word to see some magic: ");

			//receive message
			message = in.readLine();
			System.out.println("<New message from client> " + message);

			//reverse and send it back
			out.println(new StringBuilder(message).reverse().toString());

			//close socket when receive "exit"
			if (message.equalsIgnoreCase("exit")) {
				System.out.println("Session closed!");
				out.close();
				in.close();
				clientSocket.close();
				serverSocket.close();
				break;
			}
		}

	}
}

Client.java

package com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {

	public static void main(String[] args) throws IOException {

		System.out.println("Client is starting...");

		Socket socket = null;

		try {
			//localhost because server running on my machine otherwise IP address of Server machine
			socket = new Socket("localhost", 8081);
		} catch (Exception e) {
			System.out.println("Initializing error. Make sure that server is alive!\n" + e);
		}

		//use sending message to server
		PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

		//use receiving message from server
		BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

		while (true) {
			//receive message
			System.out.println("<New message from server> " + in.readLine());

			//get text from user
			BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
			String message = data.readLine();

			//send message
			out.println(message);

			//receive response
			System.out.println("<Response from server> " + in.readLine());

			//close socket when receive exit
			if (message.equalsIgnoreCase("exit")) {
				out.close();
				in.close();
				data.close();
				socket.close();
				break;
			}

		}

	}

}

Run your Server first as java application and you will see:
socket1

Then run Client also:
socket2

Check Server again:
socket3

Any criticism is welcome!

Resources:
http://www.tutorialspoint.com/java/java_networking.htm
http://www.javaworld.com/article/2077322/core-java/core-java-sockets-programming-in-java-a-tutorial.html
https://systembash.com/a-simple-java-tcp-server-and-tcp-client/
https://systembash.com/a-simple-java-udp-server-and-udp-client/
http://cs.lmu.edu/~ray/notes/javanetexamples/

Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: