Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok so I've noticed you have been asking a lot of questions about networking recently and I figured maybe you need some help with the basics. This is an extremely simple server:</p> <pre><code>package com; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class SimpleServer implements Runnable { public SimpleServer() { started = false; serverSocket = null; } public void start() { if(!started) { started = true; try { serverSocket = new ServerSocket(PORT); running = true; serverThread = new Thread(this); serverThread.start(); System.out.println("Server started!\n"); }catch(Exception e) { e.printStackTrace(); System.exit(0); } } } public void stop() { running = false; started = false; if(serverThread != null) serverThread.interrupt(); serverThread = null; } public void run() { try { while(running) { try { Socket client = serverSocket.accept(); System.out.println("Client Accepted!"); ClientHandler handler = new ClientHandler(client); handler.sendMessage("Hello, SimpleClient!"); System.out.println("Sendeing client a message..."); }catch(Exception e){e.printStackTrace();} } }catch(Exception e){e.printStackTrace();} } private boolean started; private boolean running; private ServerSocket serverSocket; private Thread serverThread; private static final int PORT = 8081; public static void main(String args[]) { SimpleServer server = new SimpleServer(); server.start(); } public class ClientHandler implements Runnable { public ClientHandler(Socket socket) { this.socket = socket; try { writer = new PrintWriter(socket.getOutputStream()); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); running = true; runningThread = new Thread(this); runningThread.start(); }catch(Exception e){e.printStackTrace(); disconnect();} } public void disconnect() { running = false; if(runningThread != null) runningThread.interrupt(); runningThread = null; try { reader.close(); }catch(Exception e){} reader = null; try { writer.close(); }catch(Exception e){} writer = null; try { socket.close(); }catch(Exception e){} socket = null; } public void sendMessage(String message) { if(running) { writer.println(message); writer.flush(); } } public void run() { try { String message = ""; while((message = reader.readLine()) != null &amp;&amp; running) { System.out.println("Message Recieved: " + message); } }catch(Exception e){e.printStackTrace(); disconnect();} } private Socket socket; private PrintWriter writer; private BufferedReader reader; private Thread runningThread; private boolean running; } } </code></pre> <p>and now here is the client:</p> <pre><code>package com; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class SimpleClient implements Runnable { public SimpleClient() { try { socket = new Socket("127.0.0.1", PORT); writer = new PrintWriter(socket.getOutputStream()); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); running = true; runningThread = new Thread(this); runningThread.start(); }catch(Exception e){e.printStackTrace(); disconnect();} } public void disconnect() { running = false; if(runningThread != null) runningThread.interrupt(); runningThread = null; try { reader.close(); }catch(Exception e){} reader = null; try { writer.close(); }catch(Exception e){} writer = null; try { socket.close(); }catch(Exception e){} socket = null; } public void sendMessage(String message) { if(running) { writer.println(message); writer.flush(); } } public void run() { try { String message = ""; while((message = reader.readLine()) != null &amp;&amp; running) { System.out.println("Message Recieved: " + message); System.out.println("Sending a response!"); sendMessage("Hello, SimpleServer!"); } }catch(Exception e){e.printStackTrace(); disconnect();} } private Socket socket; private PrintWriter writer; private BufferedReader reader; private Thread runningThread; private boolean running; private static final int PORT = 8081; public static void main(String args[]) { new SimpleClient(); } } </code></pre> <p>In order to make a successful server in Java you need to implement a lot of Object Orientation. The server would be too complex if it had to deal with not only listening for clients but also sending and receiving messages to and from those clients. This is why I usually use a <code>ClientHandler</code> class. This class handles all of the communication and is very easy to implement. The client and the server should never be in the same file because they are meant to be run separately.</p> <p>This also might interest you if you aren't sure exactly how server and clients should work:</p> <ul> <li><a href="http://en.wikipedia.org/wiki/Client%E2%80%93server_model">Client - Server Model</a></li> </ul> <p>I hope this helped!</p> <p>John</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload