📄 client.java
字号:
import java.io.*;
import java.net.*;
class Client {
// Reader and writer in system
static BufferedReader stdIn = new BufferedReader(new InputStreamReader(
System.in));
static PrintWriter stdOut = new PrintWriter(
new OutputStreamWriter(System.out), true);
public static void main(String argv[]) throws Exception {
//make sure the user enter a single argument.
if (argv.length != 1) {
stdOut.println("Usage: Client <server>");
return;
}
// the client socket
Socket clientSocket = new Socket(argv[0], 80);
// Reader and writer between client and server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(
clientSocket.getOutputStream()), true);
stdOut.println(argv[0] + " is listening to your request:");
// the command read from user
String cmd = "";
// the response temporarily readed
String temp = "";
// the web information
String recieve = "";
//html sentencesread from server
String display = "";
//the condition whether reading sentences html or web information
boolean flag = false;
cmd = stdIn.readLine();
outToServer.println(cmd + "\r\n\r\n");
// Read the response from server and identify the content.
while (true) {
temp = inFromServer.readLine();
if (temp == null)
break;
if (flag == false && temp.length() == 0)
flag= true;
if (flag == false)
display = display + "\n" + temp;
else
recieve = recieve + "\n" + temp;
}
stdOut.println("Header:\n" + display);
//Output the html sentences into a html file.
stdOut.print("Enter the name of the file to save:");
stdOut.flush();
cmd = stdIn.readLine();
PrintWriter fileOut = new PrintWriter(new FileWriter(cmd), true);
fileOut.println(recieve);
// Close these reader and writer.
fileOut.close();
inFromServer.close();
outToServer.close();
stdIn.close();
stdOut.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -