📄 client.java
字号:
/**
* @date 2009/4/6
* @author ***
* @version 1.0.0
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.*;
import java.net.*;
public class Client {
/**
* Main function of the class Client.
*
* @param args
* the first parameter is hostname of the server.
* @throws Exception
*/
public static void main(String[] args) throws IOException {
// Creat a socket
Socket clientSocket = null;
// Open a TCP connection to port 80 of the server.
try {
clientSocket = new Socket(args[0], 80);
} catch (IOException e) {
System.out.println(e);
}
System.out.println("Hello," + args[0] + "is connecting.");
// inFromUser is used to receive the users' input
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
System.in));
// inFromServer is used to get information from server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
// Receive the server's response, show the user the response headers,
getResponse(inFromUser, inFromServer, clientSocket);
// Save the response contents to a file.
saveResponse(inFromServer);
// close the inFromServer and the inFromUser
inFromServer.close();
inFromUser.close();
}
/**
* Receive the server's response, show the user the response headers,
*
* @param request
* the request specified by users.
* @throws IOException
*/
public static void getResponse(BufferedReader inFromUser,
BufferedReader inFromServer, Socket clientSocket)
throws IOException {
try {
PrintWriter outToServer = new PrintWriter(clientSocket
.getOutputStream());
// get user's and send it to server
String request = "";
System.out
.println("\rPlease enter your request as 'GET / HTTP/1.0':");
request = inFromUser.readLine() + "\r\n";
outToServer.println(request);
outToServer.flush();
// print the header of the information from server
System.out.println("Header:");
// read the first line from server
String sentence = inFromServer.readLine();
while (sentence.length() != 0) {
System.out.println(sentence);
sentence = inFromServer.readLine();
}
} catch (IOException e) {
System.out.println(e);
}
}
/**
* Save the response contents to a file.
*
* @param request
* the file's name.
* @throws IOException
*/
public static void saveResponse(BufferedReader inFromServer)
throws IOException {
try {
// input the file'name
System.out.println("\rPlease enter a file's name: ");
BufferedReader input = new BufferedReader(new InputStreamReader(
System.in));
String fileName = input.readLine();
PrintWriter file = new PrintWriter(new FileWriter(fileName));
String sentence = "";
// Write the response contents to the file
sentence = inFromServer.readLine();
while (sentence != null) {
file.println(sentence);
sentence = inFromServer.readLine();
}
// close the file
file.close();
System.out.println("\rThe content is saved in " + fileName + "");
} catch (IOException e) {
System.out.println(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -