Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Maybe this example Applet will get you started.</p> <pre><code>import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.net.ServerSocket; import java.net.Socket; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JTextArea; import javax.swing.SwingUtilities; public class Example extends JApplet { private JButton _jButtonStartGameServer; private boolean _isStarted = false; private GameServer _gameServer = null; private JTextArea _jTextArea; private Thread _gameServerThread = null; private boolean _initialized = false; public Example() { System.out.println("ctor..."); } private void initComponents() { if (_initialized) { return; } _jButtonStartGameServer = new JButton("Start Game Server"); _jButtonStartGameServer.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!_isStarted) { startupGameServer(); } else { shutdownGameServer(); } } }); setLayout(new BorderLayout(0, 0)); _jTextArea = new JTextArea(); add(_jTextArea); add(_jButtonStartGameServer, BorderLayout.NORTH); _initialized = true; } public void init() { System.out.println("init... " + Thread.currentThread()); try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { initComponents(); } }); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); Thread.currentThread().interrupt(); } } public void start() { System.out.println("start... " + Thread.currentThread()); SwingUtilities.invokeLater(new Runnable() { public void run() { setSize(640/2, 480/2); } }); } public void stop() { System.out.println("stop... " + Thread.currentThread()); shutdownGameServer(); } public void destroy() { System.out.println("destroy " + Thread.currentThread()); shutdownGameServer(); } void displayException(Throwable t) { log("Got exception: " + t); } void processMessageFromOtherPlayer(String msg) { log("Received message from other player: " + msg); } void gameServerThreadExiting() { shutdownGameServer(); } public void startupGameServer() { _gameServer = new GameServer(); _gameServerThread = new Thread(_gameServer); _gameServerThread.start(); _isStarted = true; _jButtonStartGameServer.setText("Stop Game Server"); } public void shutdownGameServer() { GameServer gs = _gameServer; if (gs != null) { gs.cancel(); } Thread t = _gameServerThread; try { if (t != null) { t.join(); } } catch (InterruptedException ie) { Thread.currentThread().interrupt(); } _gameServer = null; _jButtonStartGameServer.setText("Start Game Server"); _isStarted = false; } void log(final String msg) { System.out.println("Log (" + Thread.currentThread() + "): " + msg); Runnable r = new Runnable() { public void run() { StringBuilder sb = new StringBuilder(_jTextArea.getText()); sb.append(msg); sb.append("\n"); _jTextArea.setText(sb.toString()); } }; if (SwingUtilities.isEventDispatchThread()) { r.run(); } else { SwingUtilities.invokeLater(r); } } class GameServer implements Runnable { volatile ServerSocket _ss = null; volatile Socket _s = null; volatile InputStream _sis = null; volatile boolean _cancel = false; public void run() { try { _ss = new ServerSocket(9999); log("Waiting for other player, listening on port: " + _ss.getLocalPort()); _s = _ss.accept(); log("Connection accepted from: " + _s.getRemoteSocketAddress()); _sis = _s.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(_sis, "UTF-8")); while (true) { if (_cancel) { break; } final String msg = br.readLine(); if (msg == null) { // connection closed / lost break; } SwingUtilities.invokeLater(new Runnable() { public void run() { processMessageFromOtherPlayer(msg); } }); } } catch (final Throwable t) { // don't show the user the exception if the user is canceling if (!_cancel) { SwingUtilities.invokeLater(new Runnable() { public void run() { displayException(t); } }); } else { // even if the user is canceling, we might be interested in the exception, though t.printStackTrace(); } } log("Game server thread exiting..."); SwingUtilities.invokeLater(new Runnable() { public void run() { gameServerThreadExiting(); } }); } public void cancel() { _cancel = true; ServerSocket ss = _ss; if (ss != null) { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } Socket s = _s; if (s != null) { try { s.close(); } catch (IOException e) { e.printStackTrace(); } } InputStream sis = _sis; if (sis != null) { try { sis.close(); } catch (IOException e) { e.printStackTrace(); } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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