Source code: org/zazof/jtegServer/ServerConnection.java
1 package org.zazof.jtegServer;
2
3 import java.net.*;
4 import java.io.*;
5
6 /**
7 * This class listens for incoming connections from clients
8 */
9
10 public class ServerConnection extends ServerSocket implements Runnable{
11
12 /**
13 * creates a server which listens on a specific port
14 * @exception IOException if the port is already used by another application
15 */
16 public ServerConnection(int port, ConnectionController controller) throws IOException{
17 super(port);
18 $port = port;
19 $controller = controller;
20 $thread = new Thread(this);
21 $thread.start();
22 if ($debug) System.out.println("JTEG server listens at port " + port);
23 }
24
25 public void run(){
26 Socket clientSocket;
27 try{
28 while(true){
29 clientSocket = accept();
30 $controller.createNewConnection(clientSocket);
31 }
32 }
33 catch (IOException exc){
34 try{
35 close();
36 $controller.removeServerConnection();
37 }
38 catch (IOException ioExc){
39 $controller.removeServerConnection();
40 }
41 }
42 }
43
44 private int $port;
45 private ConnectionController $controller;
46 private Thread $thread;
47 private static final boolean $debug = true;
48 }