 <p>Here is a super simple I made just now with some comments of what is going on. The client connects to the server can can type messages which the server will print out. This is not a chat program since the server receives messages, and the client send them. But hopefully you will understand better it better :)</p> <p>Server:</p> <pre><code> import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Main { public static DataInputStream in; public static DataOutputStream out; public static void main(String[] args) throws IOException { int port = 4444; ServerSocket server = null; Socket clientSocket = null; try { //start listening on port server = new ServerSocket(port); System.out.println("Listening on port: " + port); //Accept client clientSocket = server.accept(); System.out.println("client Connected!"); //initialize streams so we can send message in = new DataInputStream(clientSocket.getInputStream()); out = new DataOutputStream(clientSocket.getOutputStream()); String message = null; while (true) { // as soon as a message is being received, print it out! message = in.readUTF(); System.out.println(message); } } catch (IOException e){ e.printStackTrace(); if (!server.isClosed()){server.close();} if (!clientSocket.isClosed()){clientSocket.close();} } } } </code></pre> <p>Client:</p> <pre><code> import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { //declare a scanner so we can write a message Scanner keyboard = new Scanner(System.in); // localhost ip String ip = "127.0.0.1"; int port = 4444; Socket socket = null; try { //connect socket = new Socket(ip, port); //initialize streams DataInputStream in = new DataInputStream(socket.getInputStream()); DataOutputStream out = new DataOutputStream(socket.getOutputStream()); while (true){ System.out.print("\nMessage to server: "); //Write a message :) String message = keyboard.nextLine(); //Send it to the server which will just print it out out.writeUTF(message); } } catch (IOException e){ e.printStackTrace(); if (!socket.isClosed()){socket.close();} } } } </code></pre>