home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
inprise
/
JSAMPLES.Z
/
HelloThread.java
< prev
next >
Wrap
Text File
|
1998-05-08
|
4KB
|
119 lines
/*
* Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
*
* This SOURCE CODE FILE, which has been provided by Borland as part
* of a Borland product for use ONLY by licensed users of the product,
* includes CONFIDENTIAL and PROPRIETARY information of Borland.
*
* USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
* OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
* THE PRODUCT.
*
* IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
* COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
* OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
* OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
* OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
* OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
* CODE FILE.
*/
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;
}
}
}