// How to use Sockets to create Servers in Java import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class client_rec { client_rec (int x, Socket s, DataOutputStream dos, DataInputStream di) { sock = s; clientnumber = x; dis = di; this.dos = dos; } int clientnumber; Socket sock; DataInputStream dis; DataOutputStream dos; } class client_monitor extends Thread { server serv; client_rec cr; client_monitor (server s, client_rec x) { serv = s; cr = x; start(); } public void run () { String x = new String(""); // output the current seed value stop: { while (true) { try { x = cr.dis.readUTF(); if (x.equalsIgnoreCase("stop")) { cr.dos.writeUTF("Client stopped"); break stop; } else { int seed = (int) ( Math.random() * 1000); cr.dos.writeUTF("Value server is thinking of: "); cr.dos.writeInt(seed); serv.display.append("Client: " + cr.clientnumber + " Request satisfied\n"); } } catch (IOException e) { break stop; } } } // close connection serv.display.append("Shutting down client: " + cr.clientnumber + "\n"); serv.remove_client(cr); try { cr.sock.close(); } catch (IOException e) { serv.display.append("Error shutting down socket: " + e.toString()); } stop(); cr = null; serv.display.append("Client: " + cr.clientnumber + " closed\n"); } } public class server extends JFrame { static int max_clients = 2; int client_count; client_rec clients[]; JTextArea display; public server() { super("Number Server"); display = new JTextArea(6, 25); display.setLineWrap(true); display.setEditable(false); display.setBackground(Color.yellow); JScrollPane jsp = new JScrollPane(display); jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(jsp, BorderLayout.CENTER); setSize(300, 200); setVisible(true); addWindowListener(new WindowAdapter () { public void windowClosing (WindowEvent e) { System.exit(0); } }); } public client_rec add_client (int c, Socket s, DataOutputStream dos, DataInputStream di) { if (client_count < max_clients) { client_rec x = new client_rec(c, s, dos, di); clients[client_count] = x; client_count = client_count + 1; return x; } return null; } public void remove_client (client_rec x) { for (int i = 0; i < client_count; i++) { if (clients[i] == x) { for (int j = i+1; j < client_count; j++) clients[j-1] = clients[j]; client_count = client_count - 1; return; } } } public void startServer() { ServerSocket s; Socket connection; DataOutputStream sos; DataInputStream sis; client_count = 0; try { // set up a socket listening to 5500 with client limit max_clients s = new ServerSocket(5500, max_clients); display.append("Server listening on port: 5500\n"); clients = new client_rec[max_clients]; while (true) { connection = s.accept(); display.append( "Connection: " + (client_count + 1) + " received from: " + connection.getInetAddress().getHostName() + "\n"); // create input and output streams for socket... sos = new DataOutputStream(connection.getOutputStream()); sis = new DataInputStream(connection.getInputStream()); client_rec cr = add_client(client_count + 1, connection, sos, sis); if (cr == null) { display.append("Client limit!! Connection refused...\n"); sos.writeUTF("Refused"); } else { display.append("Connection granted..." + "\n"); client_monitor cm = new client_monitor(this, cr); sos.writeUTF("Connected..."); } } } catch (IOException e) { e.printStackTrace(); } } public static void main (String args[]) { server s = new server(); s.startServer(); } }