-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSServerThread.java
More file actions
71 lines (60 loc) · 2.07 KB
/
CSServerThread.java
File metadata and controls
71 lines (60 loc) · 2.07 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.*;
import java.net.*;
import java.lang.*;
/**
*
* @author sparrow
*/
public class CSServerThread {
/** default port number
*/
public final static int DEFAULT_PORT = 3456;//
/*
* number of clients
*/
public final static int qLen = 3;
public void CSServerThread(){};
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{
ServerSocket listenSocket = null;
OutputStreamWriter osw = null;
InputStreamReader isr = null;
BufferedReader br = null;
int portNum = DEFAULT_PORT;
int clientNo = 0;
if( args.length != 0)
{
try {
// count clients serviced
portNum = Integer.parseInt( args[ 0]);
// put some test here to allow for port number not in range
}
catch( NumberFormatException nfE) {
System.err.println( "Illegal port number: " + args[ 0]);
System.err.println( "\tUsing the default: " + DEFAULT_PORT);
}
}
try {
listenSocket = new ServerSocket(portNum,qLen);
}
catch( IOException ioE)
{
System.err.println("Could not bind to port: " + portNum);
System.err.println( "\tIs it already in use?");
System.err.println( "\tIs it a reserved port number?");
System.exit(1);
}
while( true) {
// loop forever accepting clients
Socket clientSocket = null;
clientSocket = listenSocket.accept();
++clientNo; // count clients serviced
new HandleClient( clientSocket, clientNo).start(); // a new thread
//new HandleClient( listenSocket.accept(), ++clientNo).start();
// try changing for isr rather than br
// end of finally
} // while forever waiting for a client connection
} // main
}