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

📄 client.java

📁 用java语言编写的一个http协议的socket实现
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import IOinput.InputAdress;

/**
 * 客户端程序,用于将客户的请求发送给服务端程序,并接受服务端程序返回的处理结果并做显示等适当的处理
 * @author Crise.Lee
 * @version 1.0
 */
public class Client {
	@SuppressWarnings("deprecation")
	public static void main(String[] args)
	{
		String url=null,command=null;
		int port;
		InputAdress inputurl=new InputAdress();
		Boolean isgo=true;
		while(isgo)
		{
			try {
				//建立一个SOCKET套接字,输入URL
				inputurl.inputbaseURL();
		        url=inputurl.getbaseURL();
		        
		        //输入端口号
		        inputurl.inputPort();
		        port=inputurl.getPort().intValue();
				Socket client=new Socket(url,port);

				if(client.isConnected())
				{
					System.out.println("*******************************************");
					System.out.println("********       连接服务器成功       ********");
					System.out.println("*******************************************");
					
					//输入请求命令
					inputurl.inputCommand();
					command=inputurl.getCommand();
					
					//创建输入输出对象
					BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
					BufferedWriter out=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
					
					//out.write("GET / /DocumentPage/DocumentList.aspx HTTP/1.0\r\n\r\n");
					out.write(command);
					System.out.println("您输入的请求为:"+command);
					//out.write("GET /D:/Downloads/test1.txt HTTP/1.1\r\n\r\n");
					out.flush();
					
					DataOutputStream toFile=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("body.txt")));
					
					String get;
					System.out.println("服务器返回消息:");
					
					Boolean isbody=false;
					while((get=in.readLine())!=null)
					{
						//System.out.println(get);
						if(!(get.length()==0)&&!isbody)
						{
							System.out.println(get);
						}
						else
						{
							isbody=true;
							toFile.writeUTF(get);
							toFile.flush();
						}
					}//while
				}//if
				else 
				{
					System.err.println("错误原因:连接服务器失败.......");
					isgo=true;
				}
				client.close();
			} catch (UnknownHostException e) {
				System.err.println("错误原因:"+e.getMessage());
				//e.printStackTrace();
			} catch (IOException e) {
				System.err.println("错误原因:"+e.getMessage());
				//e.printStackTrace();
			}finally
			{
				                                    //提示用户是否要继续操作
				System.out.print("是否要继续操作?");
				try {
					System.out.print("(yes/no): ");
					BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
					String go=input.readLine();
					if(go.equalsIgnoreCase("yes"))
					{
						isgo=true;
						url=null;
						port=0;
					}
					else
					{
						isgo=false;
					}//else
				} catch (IOException e) {
					System.err.println("结束的时候系统I/O发生错误,程序推出。");
					isgo=false;
					//e.printStackTrace();
				}//try
			}//finally
		}//while
	}
}

⌨️ 快捷键说明

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