Using sockets in Java

Socket server

This is example of most simple socket server. It opens port 4444 and listens for incoming connection. Server does nothing more than echo the input String. When 'exit' String is received, the connection is closed. If port 4444 is already in use, choose another port number.

Don’t send sensitive data (usernames, passwords, bank accounts...) over telnet, since data is not encrypted! Implement TelnetServer in your Java application and you can remotely call methods to be executed.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TelnetServer {

public static ServerSocket s = null;

public static Socket incoming = null;

public static void main(String[] args) {

while (true) {
try {

// establish server socket
s = new ServerSocket(4444);

// wait for incoming connection
incoming = s.accept();

InputStream inps = incoming.getInputStream();
OutputStream outs = incoming.getOutputStream();

Scanner in = new Scanner(inps);
PrintWriter out = new PrintWriter(outs, true);

out.println("Server running...");

boolean done = false;
while (!done && in.hasNextLine()) {

// read data from Socket
String line = in.nextLine();

if (line.trim().equalsIgnoreCase("exit")) {
done = true;
} else {
out.println("Echo: " + line);
}

}

} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

try {
incoming.close();
incoming = null;
} catch (IOException e) {
e.printStackTrace();
}

try {
s.close();
s = null;
} catch (IOException e) {
e.printStackTrace();
}

}

}

}

Connecting to SocketServer from Console

Open Console and type:

$ telnet localhost 4444

and you should get connected. If TelnetServer is running on another computer, type its hostname or IP address instead of 'localhost'. Type something and the server should respond with echo. Type 'exit' to disconnect from Server.

Connecting to TelnetServer from Java TelnetClient

Client which connects to TelnetServer running on localhost, on port 4444. First the client reads the welcome message which is sent from the server first. Then the client sends a string through PrintWriter object and the server responds with echo.

In while loops there is a condition that input stream is read until byte with value 10 is received. This byte is 'end-of-text' character.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

public class TelnetClient {

public static InputStream instream;
public static OutputStream outstream;

public static void main(String[] args) {

try {
SocketAddress sa = new InetSocketAddress("localhost", 4444);
Socket sock = new Socket();
sock.connect(sa, 10 * 1000);
instream = sock.getInputStream();
outstream = sock.getOutputStream();

int i = 0;
byte[] b = new byte[1];
PrintWriter out = new PrintWriter(outstream, true);

while ((i = instream.read()) != 10) {
b[0] = (byte) i;
System.out.print(new String(b));
}
System.out.println();

String s = "abcd";
out.println(s);
while ((i = instream.read()) != 10) {
b[0] = (byte) i;
System.out.print(new String(b));
}
System.out.println();

instream.close();
out.close();
outstream.close();
sock.close();

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

}