📄 server.java
字号:
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
public class Server implements java.lang.Runnable {
static int serverSocketPort = 8000;
/**
* userSocket represent the socket connect to the user
*/
private Socket userSocket;
/**
* serverSocket represent the socket connect to the server
*/
static Socket serverSocket;
static String commandFromUser = "";
static String serverName = "";
static String fileLocation = "";
static String httpType = "";
static int portNum = 80;
/**
* Constructor
*
* @param socket
*/
public Server(Socket socket) {
userSocket = socket;
}
/**
* The main function
*
* @param argv
* @throws IOException
*/
public static void main(String argv[]) throws IOException {
ServerSocket listenSocket = new ServerSocket(serverSocketPort);
while (true) {
Runnable run = new Server(listenSocket.accept());
Thread threadServer = new Thread(run);
threadServer.start();
}
}
public void run() {
try {
String totalCommand = getCommandFromUser(); // Get all infomation of
// the input from the user
StringTokenizer tokenizedLine = new StringTokenizer(totalCommand);
String command = tokenizedLine.nextToken();
if (command.equals("GET") && tokenizedLine.hasMoreTokens()) {
get(totalCommand); // If the command is a Right command, analyse the total command
System.out.println("The user want to get info form : \n"
+ serverName + fileLocation + " at port " + portNum);
serverSocket = new Socket(serverName, portNum); //Create a new socket to connect with the server
DataOutputStream outToServer = new DataOutputStream(
serverSocket.getOutputStream());
String commandToServer = commandFromUser + " " + fileLocation
+ " " + httpType + "\r\n\r\n";
outToServer.writeBytes(commandToServer); //Send the user's command to the server
System.out.println("The command is: " + commandToServer);
BufferedReader inFromServer = new BufferedReader(
new InputStreamReader(serverSocket.getInputStream()));
String infoFromServer = inFromServer.readLine();
String totalInfo = "";
while (infoFromServer != null && !infoFromServer.isEmpty()) { //Get all informaiton sent by the server
totalInfo += infoFromServer;
infoFromServer = inFromServer.readLine();
}
System.out.println(totalInfo);
DataOutputStream outToClient = new DataOutputStream(userSocket
.getOutputStream());
outToClient.writeBytes(totalInfo); //Send the received information to the user
outToClient.close();
} else{
wrongCommand();
}
userSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Get the user's command
* @return the total information getted from the user
* @throws IOException
*/
private String getCommandFromUser() throws IOException {
System.out.println(userSocket.getInetAddress() + " is connected");
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(
userSocket.getInputStream()));
String clientCommand = inFromClient.readLine();
String totalCommand = "";
while (clientCommand != null && !clientCommand.isEmpty()) {
totalCommand += clientCommand + "\r\n";
clientCommand = inFromClient.readLine();
}
return totalCommand;
}
/**
* Analyse the user's input command
* @param totalCommand
* the user's all input
*/
private void get(String totalCommand) {
StringTokenizer tocken = new StringTokenizer(totalCommand);
commandFromUser = tocken.nextToken(); //Get command "GET"
String totalURL = tocken.nextToken();
httpType = tocken.nextToken(); //Get http type like "HTTP/1.0"
String httpHead = "http://";
StringTokenizer urlToken = new StringTokenizer(totalURL
.substring(httpHead.length()), ":");
serverName = urlToken.nextToken(); //Get server's name
if (urlToken.hasMoreTokens()) { //if the user input the port number
String portString = urlToken.nextToken();
StringTokenizer portToken = new StringTokenizer(portString, "/");
portNum = Integer.parseInt(portToken.nextToken());
fileLocation = "/" + portToken.nextToken();
} else { //if the user does not input the port number
StringTokenizer nameToken = new StringTokenizer(serverName, "/");
serverName = nameToken.nextToken();
portNum = 80;
fileLocation = "/" + nameToken.nextToken();
}
}
/**
* if the user does not input a right command
* @throws IOException
*/
private void wrongCommand() throws IOException {
DataOutputStream outToClient = new DataOutputStream(userSocket
.getOutputStream());
String totalInfo = "Wrong command";
outToClient.writeBytes(totalInfo);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -