forked from patniemeyer/learningjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
49 lines (45 loc) · 1.4 KB
/
Copy pathServer.java
File metadata and controls
49 lines (45 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//file: Server.java
import java.net.*;
import java.io.*;
public class Server {
public static void main( String argv[] ) throws IOException {
ServerSocket ss = new ServerSocket( Integer.parseInt(argv[0]) );
while ( true )
new ServerConnection( ss.accept() ).start( );
}
} // end of class Server
class ServerConnection extends Thread {
Socket client;
ServerConnection ( Socket client ) throws SocketException {
this.client = client;
setPriority( NORM_PRIORITY - 1 );
}
public void run( ) {
try {
ObjectInputStream in =
new ObjectInputStream( client.getInputStream( ) );
ObjectOutputStream out =
new ObjectOutputStream( client.getOutputStream( ) );
while ( true ) {
out.writeObject( processRequest( in.readObject( ) ) );
out.flush( );
}
} catch ( EOFException e3 ) { // Normal EOF
try {
client.close( );
} catch ( IOException e ) { }
} catch ( IOException e ) {
System.out.println( "I/O error " + e ); // I/O error
} catch ( ClassNotFoundException e2 ) {
System.out.println( e2 ); // unknown type of request object
}
}
private Object processRequest( Object request ) {
if ( request instanceof DateRequest )
return new java.util.Date( );
else if ( request instanceof WorkRequest )
return ((WorkRequest)request).execute( );
else
return null;
}
}