home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet News 1999 February
/
InternetNews_1999_02.iso
/
pc
/
Professionale
/
codici9810
/
SampleClient.java
next >
Wrap
Text File
|
1998-01-25
|
5KB
|
184 lines
/*
** Programma: SampleClient
** Versione: 1.0.0
** Autore: Dario de Judicibus
** Indirizzo: dejudicibus@geocities.com
** Creato il: January 3rd, 1998
** Modificato il: January 4th, 1998
** Linguaggio: Java (jdk 1.1)
** -------------------------------------------------------------
** (c) 1998 - All rights reserved
** -------------------------------------------------------------
** I make no representations or warranties about the suitability of
** the software, either express or implied, including but not limited
** to the implied warranties of merchantability, fitness for a
** particular purpose, or non-infringement. I shall not be liable for
** any damages suffered by licensee as a result of using, modifying or
** distributing this software or its derivatives.
*/
/**
* @author Dario de Judicibus
* @version 1.0.0
* @see jdk 1.1
*/
import java.io.* ;
import java.net.* ;
import java.util.Date ;
import java.text.SimpleDateFormat ;
class SampleClient
{
// CLASS VARIABLES
private static final int cvPORT = 2000 ;
private static final String cvDATEFORMAT = "EEE dd MMM yyyy HH:mm:ss z" ;
// INSTANCE VARIABLES
private String ivHostName ;
private String ivServerAddress ;
private Socket ivClientSocket ;
private BufferedReader ivFromServerStream ;
private PrintWriter ivToServerStream ;
private SimpleDateFormat ivFormatter ;
public static void main( String[] args )
{
String givenAddress = null ;
if ( args.length == 1 )
{
givenAddress = args[ 0 ] ;
}
SampleClient sampleClient = new SampleClient( givenAddress ) ;
sampleClient.run( ) ;
}
private SampleClient( String serverAddress )
{
ivServerAddress = serverAddress ;
ivFormatter = new SimpleDateFormat( cvDATEFORMAT ) ;
System.out.println( "[" + ivFormatter.format( new Date( ) ) + "] " + "Starting client." ) ;
}
private void run( )
{
// Try to create a client socket first.
// If fails, just abend the client.
try
{
createClientSocket( ) ;
}
catch ( UnknownHostException exc )
{
System.out.println( "[ERROR] Unknown host: " + exc.getMessage( ) ) ;
System.exit( 1 ) ; // Terminate server
}
catch ( IOException exc )
{
System.out.println( "[ERROR] IO: " + exc.getMessage( ) ) ;
System.exit( 1 ) ; // Terminate server
}
// Manage interactions with server.
// If fails, print the stack trace and abend the client.
try
{
// Prepare for data exchange
prepareForExchange( ) ;
int c ;
String inputLine ;
StringBuffer outputLine = new StringBuffer( 50 ) ;
// Process input from server until closing
while ( ( inputLine = ivFromServerStream.readLine( ) ) != null )
{
System.out.println( "[SERVER] " + inputLine ) ;
if ( inputLine.equals( "OK Closing server." ) )
break ;
while ( ( c = System.in.read( ) ) != '\n' )
{
outputLine.append( (char)c ) ;
}
System.out.println("[CLIENT] " + outputLine ) ;
ivToServerStream.println( outputLine.toString( ) ) ;
outputLine.setLength( 0 ) ;
}
// Terminate session and close connection
terminateConnection( ) ;
}
catch ( UnknownHostException exc )
{
System.out.println( "[ERROR] Unknown host: " + exc.getMessage( ) ) ;
System.exit( 1 ) ; // Terminate server
}
catch ( IOException exc )
{
System.out.println( "[ERROR] IO: " + exc.getMessage( ) ) ;
System.exit( 1 ) ; // Terminate server
}
}
private void createClientSocket( ) throws UnknownHostException,IOException
{
InetAddress serverAddress ;
try
{
serverAddress = InetAddress.getByName( ivServerAddress ) ;
}
catch ( UnknownHostException exc )
{
serverAddress = InetAddress.getByName( null ) ;
}
ivHostName = serverAddress.getHostName( ) ;
ivClientSocket = new Socket( ivHostName, cvPORT ) ;
System.out.println( "[" + ivFormatter.format( new Date( ) ) + "] "
+ "Connecting to server " + ivHostName + " on port " + cvPORT ) ;
return ;
}
private void prepareForExchange( ) throws IOException
{
// Create the input and output streams
// Both streams are buffered
ivFromServerStream = new BufferedReader(
new InputStreamReader( ivClientSocket.getInputStream( ) ) ) ;
ivToServerStream = new PrintWriter( ivClientSocket.getOutputStream( ), true ) ;
System.out.println( "[" + ivFormatter.format( new Date( ) ) + "] " + "Client ready" ) ;
return ;
}
private void terminateConnection( ) throws IOException
{
ivToServerStream.close( ) ;
ivFromServerStream.close( ) ;
ivClientSocket.close( ) ;
System.out.println( "[" + ivFormatter.format( new Date( ) ) + "] "
+ "Closing connection from " + ivHostName ) ;
// Reset all instance variables
ivToServerStream = null ;
ivFromServerStream = null ;
ivClientSocket = null ;
return ;
}
}