home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-03 | 3.0 KB | 100 lines |
- package borland.samples.apps.chess.server;
-
- import java.net.*;
- import java.io.*;
-
- public class HelloThread extends Thread
- {
- ServerSocket serverSocket = null;
-
- HelloThread() {
- ChessServer.availablePort = new boolean[ChessServer.maxConnections];
- for (int i=0;i<ChessServer.maxConnections;i++)
- ChessServer.availablePort[i] = true ;
- //System.out.println("Hello");
- try {
- serverSocket = new ServerSocket(ChessServer.basePort - 1);
- System.out.println("Hello1");
- }
- catch (IOException e) {
- System.out.println("Could not listen on port: " + 4444 + ", " + e);
- System.exit(1);
- }
- }
-
- public void run() {
- //System.out.println("HelloThread: running");
- if (serverSocket == null)
- return;
- try {
- System.out.println("Address of the Server :" + InetAddress.getLocalHost().toString()) ;
- }
- catch (Exception e) {
- System.out.println("HelloThread: " + e);
- System.exit(1);
- }
- while (true) {
- System.out.println("HelloThread: Waiting for someone to talk to me: " );
- Socket clientSocket = null;
- try {
- clientSocket = serverSocket.accept();
- }
- catch (IOException e) {
- System.out.println("Accept failed: " + e);
- System.exit(1);
- }
-
- try {
- System.out.println("HelloThread: set up data streams");
-
- DataInputStream is = new DataInputStream(
- new BufferedInputStream(clientSocket.getInputStream()));
- PrintWriter os = new PrintWriter(clientSocket.getOutputStream());
- // new BufferedOutputStream(clientSocket.getOutputStream(), 1024), false);
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- String inputLine, outputLine;
- String name;
- //System.out.println("HelloThread: Someone to talk to! " );
-
- if ((inputLine = br.readLine()) != null) {
- System.out.println("Hello Thread: Received:" + inputLine);
- outputLine = "";
- //someone wants to talk ; tell them the number of their
- //private port;
- if (inputLine.startsWith("Hello")) {
- int portNumber = ChessServer.getPortNumber();
- new SendThread(portNumber).start();
- outputLine = String.valueOf(portNumber);
- System.out.println("Assign " + clientSocket.getInetAddress().toString() + " to port " + outputLine) ;
-
- }
- os.println(outputLine);
- os.flush();
- }
- os.close();
- is.close();
- clientSocket.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- catch (Exception e) {
- System.out.println("HelloThread:" + e);
- }
- }
- }
-
- protected void finalize() {
- if (serverSocket != null) {
- try {
- serverSocket.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- serverSocket = null;
- }
- }
- }
-