⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 Icarnegie university SSD8电子书和全部练习答案
💻 JAVA
字号:
/**
* @author lu hongyong 043742
* @see <code>Client.java</code>
* @version 1.0
* 
* The file is to build a Simple HTTP/1.0 Server with a Command Line Interface.
*
*/

import java.io.*; 
import java.net.*; 
import java.util.*;


public class Server { 

// The port number in static final type . 
private static final int socketPort = 6789;

// Default constructor.
public Server (){ }

/**
* This method is used to deal with the user's "GET" request.
* 
* @param <code>StringTokenizer</code> and <code>DataOutputStream</code>.
* @return void.
* @exception <code>FileNotFoundException</code> and <code>IOException</code>.
*/
public void methodGet(StringTokenizer tokenizedLine, DataOutputStream outToClient) {

String fileName;
fileName = tokenizedLine.nextToken();
if(fileName.startsWith("/") == true) {

try {

fileName = "C:\\www\\test\\" + fileName.substring(1);
System.out.println(fileName);
File file = new File(fileName);
int numOfBytes = (int)file.length();

FileInputStream inFile = new FileInputStream(fileName);

byte[] fileInBytes = new byte[numOfBytes];
inFile.read(fileInBytes);

outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");

// Jugde the user's requested file's extended type name.
if(fileName.endsWith(".jpg")) 
outToClient.writeBytes("Content-Type: image/jpeg\r\n");
if(fileName.endsWith(".html") || fileName.endsWith(".htm")) 
outToClient.writeBytes("Content-Type: text/html\r\n");
if(fileName.endsWith(".gif")) 
outToClient.writeBytes("Content-Type: image/gif\r\n"); 

// Write to the client.
outToClient.writeBytes("Content-length: " + numOfBytes + "\r\n");
outToClient.writeBytes("\r\n");
outToClient.write(fileInBytes, 0, numOfBytes);

} catch (FileNotFoundException fnfe){

System.err.println("The specified file is not exist!");
fnfe.printStackTrace();

} catch (IOException ioe){

System.err.println("IO erro: ");
ioe.printStackTrace();

}
}
}

public static void main(String argv[]) throws IOException { 

// Create a new Server object.
Server server = new Server();
String clientSentence = ""; 
ServerSocket welcomeSocket = null;

try 
{
// Create a server socket to respond to the clients' responds.
welcomeSocket = new ServerSocket(socketPort); 
System.out.println("Socket");

} catch (IOException e) {

System.err.println("IO erro: ");
e.printStackTrace();
}

// Always run.
while(true) { 

Socket connectionSocket = null;
try { 

// Accept the client's socket connection.
connectionSocket = welcomeSocket.accept(); 
System.out.println("welcomeSocket.accept()");
}catch(IOException ioe) {

System.err.println("IO erro: ");
ioe.printStackTrace();
}

BufferedReader inFromClient = 
new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream())); 

DataOutputStream outToClient = 
new DataOutputStream(connectionSocket.getOutputStream()); 

try {
// Read the client's request.
clientSentence = inFromClient.readLine(); 

} catch(IOException io) {

System.err.println("IO erro: ");
io.printStackTrace();
} 

StringTokenizer tokenizedLine = 
new StringTokenizer(clientSentence);

if(tokenizedLine.nextToken().equals("GET")) {

// If the request is a "GET" request, then deal with it.
server.methodGet(tokenizedLine, outToClient);
} else {
// If it is not a valid request, then print out the erro message.
System.out.println("Bad request!"); 
}

connectionSocket.close();
}

} 

} 











⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -