📄 modifiedclient.java
字号:
/*
* Author: yanghui
* 063398
*/
import java.io.*;
import java.net.*;
//count the number of bytes that it sends and receives
public class ModifiedClient {
/*The connection port*/
final static int portNumber = 80;
/*The work content where your files stores*/
final static String path = "G:\\JAVA-Workspace\\Exe1\\src\\";
/*main method*/
public static void main(String[] argv){
/*the server name */
String webName = null;
/*the packet head*/
String head = "";
/*the request send to the server*/
String request = null;
/*the data from the server*/
String fileData = "";
//count the bytes that is sends and receives.
int bytesOfSend=0;
int bytesOfReceive=0;
/*initialize the server name*/
//webName = argv[0];
webName="www.sina.com";
try{
/*create a socket with the server*/
Socket clientSocket = new Socket(webName,portNumber);
/*read from the user and get the request*/
System.out.println("A server is now connencting...\nEnter your request");
BufferedReader inFromUser = new BufferedReader(
new InputStreamReader(System.in));
request = inFromUser.readLine();
/*data get stream to the server*/
DataOutputStream outToServer = new DataOutputStream(
clientSocket.getOutputStream());
/*data send to the server*/
BufferedReader inFromServer = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
/*send the data to the server*/
outToServer.writeBytes(request + "r\n\r\n");
head = inFromServer.readLine();
/*read the packet head*/
//while(!head.startsWith("<html>"))
while(!head.equals("")){
bytesOfSend += head.getBytes().length; //count the bytes it sends
System.out.println(head);
head = inFromServer.readLine();
}
/*get the data body*/
head = inFromServer.readLine();
while(head != null){
bytesOfReceive+=head.getBytes().length; //count the bytes it receives
fileData += head;
head = inFromServer.readLine();
}
System.out.println("The sengding bytes are " + bytesOfSend + ",and the receiving bytes are " + bytesOfReceive); //print out the bytes of the send and the received packets.
/*save the data by the user's command*/
System.out.println("Enter filaname you want to save:");
BufferedReader inFromCMD = new BufferedReader(
new InputStreamReader(System.in));
String filename = inFromCMD.readLine();
File file = new File(path + filename);
FileWriter fileWrites = new FileWriter(file);
fileWrites.write(fileData);
fileWrites.close();
clientSocket.close();
}catch(SocketException e){
System.out.println("socket error!IN Client" + e.getMessage());
}catch(IOException e){
System.out.println("io exception in Client");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -