0
How to create a Socket in Java?
A Java Socket is, in my humble opinion, one of the most coolest features of Java thanks to their simplicity (yeah yeah yeah ... loosing fredom but, honestly, who cares about it in our times? :-P ).
So, first thing first, the Server Socket. This big guy waits for client socket connections, and when a client connects, it creates a socket for that specific client. Then, if you need it, you can reuse the Server Socket for waits for another client.
This reusable feature always bind a Server Socket with Thread creations: For each client socket that the Server Sockets creates, you can create a Thread for handle the specific client. So:
//Create the raw server socket
ServerSocket serverSocket = new ServerSocket(port);
//The client socket
Socket clientSocket;
//The handler for the client (is a class that implements the Runnable interface)
ClientHandler clientHandler;
//The Thread for the client
Thread thread;
while(true){
//Waits for a client ...
//The current thread will wait in this sentence until a clients connects
clientSocket = serverSocket.accept();
//When a client arrives, creates a handler
clientHandler = new ClientHandler(clientSocket);
//... and a Thread
thread = new Thread(clientHandler);
//Start the thread
thread.start();
}
Now we have a Server Socket and a "Threads creator" for multiple clients handling. Now, with a socket we can do, basicly, two operation: read and write. For Java, the socket is just another Stream, so we can handle all read and write operations in the same way that we handled it for files. Another good abstraction from Java that allows reuse knowledge and code! So, meanwhile in the code ...
//Get the socket input stream reader
BufferedReader reader =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//Get the socket output stream writer (with auto flush off)
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), false);
//The client message
String message;
//Send message to client
writer.println("Welcome");
writer.flush();
while(true){
//Get message from client
message = reader.readLine();
//Sent the echo response
writer.println("Echo response: "+message);
writer.flush();
}
That is it, at this point we have a Multi-Client server that can "read from"and "write to" a socket stream. Now, we can test it using telnet (remember, telnet open raw sockets) or you can implement a Java client like this:
//The raw socket that allow the client application interact with the server
Socket socket = null;
//Create a raw socket from scratch
socket = new Socket(host, port);
After this, you just only need handle the read/write operations like the server socket.
If you download this, you will see a complete socket example. If you want to know about it before download it, check the documentation
So, first thing first, the Server Socket. This big guy waits for client socket connections, and when a client connects, it creates a socket for that specific client. Then, if you need it, you can reuse the Server Socket for waits for another client.
This reusable feature always bind a Server Socket with Thread creations: For each client socket that the Server Sockets creates, you can create a Thread for handle the specific client. So:
//Create the raw server socket
ServerSocket serverSocket = new ServerSocket(port);
//The client socket
Socket clientSocket;
//The handler for the client (is a class that implements the Runnable interface)
ClientHandler clientHandler;
//The Thread for the client
Thread thread;
while(true){
//Waits for a client ...
//The current thread will wait in this sentence until a clients connects
clientSocket = serverSocket.accept();
//When a client arrives, creates a handler
clientHandler = new ClientHandler(clientSocket);
//... and a Thread
thread = new Thread(clientHandler);
//Start the thread
thread.start();
}
Now we have a Server Socket and a "Threads creator" for multiple clients handling. Now, with a socket we can do, basicly, two operation: read and write. For Java, the socket is just another Stream, so we can handle all read and write operations in the same way that we handled it for files. Another good abstraction from Java that allows reuse knowledge and code! So, meanwhile in the code ...
//Get the socket input stream reader
BufferedReader reader =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//Get the socket output stream writer (with auto flush off)
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), false);
//The client message
String message;
//Send message to client
writer.println("Welcome");
writer.flush();
while(true){
//Get message from client
message = reader.readLine();
//Sent the echo response
writer.println("Echo response: "+message);
writer.flush();
}
That is it, at this point we have a Multi-Client server that can "read from"and "write to" a socket stream. Now, we can test it using telnet (remember, telnet open raw sockets) or you can implement a Java client like this:
//The raw socket that allow the client application interact with the server
Socket socket = null;
//Create a raw socket from scratch
socket = new Socket(host, port);
After this, you just only need handle the read/write operations like the server socket.
If you download this, you will see a complete socket example. If you want to know about it before download it, check the documentation