Java Sockets

You don’t often hear too much spoken about sockets, especially if you’re new to the programming world. However, no matter how advanced you are in programming languages, sockets are a handy tool to know and learn. If you’re a part of the RSPS community and have actively programmed on a server before, you’ve used sockets! Well maybe you haven’t messed around with them, but the very basics of a private server relation is the socket network between the server and client. Yes, there’s different variations between different sources, but in the end, it’s all the same concept. This tutorial on Sockets will take you through the basic functionality and methods required to use them. Please note that the concepts in this tutorial are slightly more challenging to understand than most other tutorials located on this website.

Why use Sockets?

Sockets are a great way to ensure a stable and constant flow connection between the two points your communicating with. Whether it be runescape or your own custom program, they’re a great way to relay information smoothly and safely. Besides that, the possibilities using sockets are endless. You can even connect to a Java socket server using PHP from a webpage! Sounds cool doesn’t it? This makes it a cinch to create chat programs, multiplayer games, database extractions, or anything!

How does this relate to the RSPS community?

Well, as said above there’s many features one could use sockets for. Just the other day, I used a PHP to Java socket to create a website interface that allows you to view another person’s in-game bank. It queries the player’s bank in around 150ms (wow that’s pretty decent). Sockets can also be used for modifying how your server/client operates, since Runescape and private servers basically runs on them. The possibilities are endless if you have the knowledge. And here’s some more knowledge for you!

How do I use/implement a socket?

Java Sockets

A socket requires there to be a server and a client. Each end either sends or listens for responses from one another. Here’s an example for you to think about to better imagine a socket:

Suppose you have a server running a java application 24/7. This application listens for connections from clients (which could be anywhere in the world). The clients connect only to your server. The client socket application sends a message to the server containing a number perhaps. The server then accepts the message that the client sent and replies. Suppose in this case, your server takes the number received and sends back to the client a different number. Just like Runescape, there’s only 1 server and an unlimited number of clients. The purpose of a server is to make it so that your clients (users) won’t have contact to the sensitive validation & data you might be trying to keep secure.

Let’s get started:

Socket Server:

package org.clayton;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    
    public static void main(String args[]) {
        try {
            ServerSocket listener = new ServerSocket(9090); //parameter for ServerSocket is the port #.
            while (true) {
                Socket socket = listener.accept(); /*will stay on this line until a connection is received.
                The while loop will NOT continue until a socket is accepted here.*/
                System.out.println("Connection Accepted!");
                DataInputStream in = new DataInputStream(socket.getInputStream());
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                
                System.out.println(in.readUTF()); //reads and outputs message from client
                out.writeUTF("We got your message!"); //sends a message to the client
                
                in.close();
                out.close(); //close all streams and resources
                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    

}

The above code is an example of a socket server that I’ve written. It’s well commented, so be sure to read through the code & the comments before continuing this paragraph. It’s important to remember that the while loop will continue to search for incoming connections until the program is exited or there is a run-time error. Also remember that (as said in comments), the line that accepts a new socket will stay on the line (process will not continue on through the loop) until a socket is found & connected.

Socket Client:

package org.clayton;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

public class Client {
    
    public static void main(String args[]) {
        try {
            Socket socket = new Socket("127.0.0.1", 9090); //connect using server IP and port #
            DataInputStream in = new DataInputStream(socket.getInputStream());
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            
            out.writeUTF("Hey server, please send me back a message."); //sends a message to the server
            System.out.println(in.readUTF()); //gets message from server
            
            in.close();
            out.close(); //close all streams and resources
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

}

Above is the client. Again, please read through the code & comments before proceeding. A socket connection is attempted using an IP address and a port number (the same port number as you used on your server). We then send a message to the server and receive one back. At the end, don’t forget to close all resources being used. The DataOutputStream and DataInputStream classes have a number of methods in which you can send/receive data. In the above example, writeUTF() and readUTF() are for writing strings only. There’s equivalent methods for most data types in the classes above.

Some Rules about sending and receiving data:

  • If the client sends a string, integer, string in that order, the data must be read on the server side in the same order.
  • Make sure you close out all resources when you’re done using them.

Some good resources: