📄 threadedserver.java
字号:
import java.io.*;
import java.net.*;
import java.util.*;
public class ThreadedServer {
public ThreadedServer() throws Exception {
//the server socket
ServerSocket serSk = new ServerSocket(8000);
//the counter of clients
int counter = 1;
while (true) {
Socket connectionSocket = serSk.accept();
System.out.println("Accepting Client " + counter + "'s Connection...\n");
// a thread for a client
ServerThread st = new ServerThread(connectionSocket, counter++);
st.start();
}
}
class ServerThread extends Thread {
// a client socket
private Socket sk;
// the counter of clients
private int counter;
ServerThread(Socket sk, int counter) {
this.sk = sk;
this.counter = counter;
}
public void run() {
// reader and writer with client and file
FileInputStream inFromFile = null;
BufferedReader inFromClient = null;
DataOutputStream outToClient = null;
// response the request
try {
while (true) {
inFromClient = new BufferedReader(new InputStreamReader(
sk.getInputStream()));
outToClient = new DataOutputStream(sk.getOutputStream());
System.out.println("Waiting for command...");
String cmd = inFromClient.readLine();
System.out.println("The request for: " + cmd);
StringTokenizer token = new StringTokenizer(cmd);
if (token.countTokens() < 2) {
System.out.println("Command style error!");
outToClient.writeBytes(" Bad Request\n\r");
break;
}
else {
String method = token.nextToken();
String filename = token.nextToken();
//Make sure that the filename is correct.
if (filename.charAt(0) != '/') {
System.out.println("Filename command error!");
outToClient .writeBytes(" Bad Request\n\r");
break;
}
filename = (filename.equals("/") ? "INDEX.HTM"
: filename.substring(1));
File file = new File(filename);
if (!file.exists()) {
System.out.println("File not existed!");
outToClient.writeBytes(" Not Found\n\r");
break;
}
//Response the right format request. Identify the file tpye and write it to the client.
System.out.println("Now respond the request...");
inFromFile = new FileInputStream(filename);
byte[] by = new byte[(int) file.length()];
inFromFile.read(by);
//response to get method
if( method.toLowerCase().equals("get")){
if (filename.endsWith(".htm")||filename.endsWith(".html")) {
outToClient.writeBytes("HTTP/1.0 200 OK" + "\n\r");
outToClient.writeBytes("Content-Type: txt/html"
+ "\n\r");
outToClient.writeBytes("Content-Length: "
+ file.length() + "\n\r\n\r");
}
else if (filename.endsWith(".jpg")||filename.endsWith(".jepg")){
outToClient.writeBytes("HTTP/1.0 200 OK" + "\n\r");
outToClient.writeBytes("Content-Type: image"
+ "\n\r");
}
outToClient.write(by);
outToClient.flush();
outToClient.writeBytes("\n\r");
break;
}
//response to put method
if(method.toLowerCase().equals("put")){
//I ignore this method and just print the fileName
System.out.println(filename);
break;
}
//response when the method is incorrect
else {
System.out.println("Method error!");
outToClient
.writeBytes(" Bad Request\n\r");
break;
}
}
}//end while
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the reader, writer and client socket.
System.out.println("Closing Client " + counter
+ "\'s Connection...\n");
try {
if (inFromFile != null)
inFromFile.close();
if (inFromClient != null)
inFromClient.close();
if (outToClient != null)
outToClient.close();
if (sk != null)
sk.close();
} catch (IOException e) {
}
}
}
}
public static void main(String[] args) {
try {
ThreadedServer threadedServer = new ThreadedServer();
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -