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

📄 queryclient.java

📁 服务/客户端 查询 非完全版本
💻 JAVA
字号:
import java.io.*;
import java.net.*;
public class QueryClient {
	Socket clientSocket1 = null; //Create socket to connect LoansServer 
    PrintWriter out = null;
    BufferedReader in = null;
    Socket clientSocket2 = null;//Create socket to connect CatalogServer 
    PrintWriter out1 = null;
    BufferedReader in1 = null;
    int port=0;//Create port to connect LoansServer 
    int port1=0;//Create port to connect CatalogServer 
    public QueryClient(int port,int port1)
    {
    	this.port=port;
    	this.port1=port1;
    }
    //Connect to Catalog Server and send request
    public void run1(String ln1, String r)
    {
    	try
    	{
    		clientSocket1 = new Socket(ln1, port);
            out = new PrintWriter(clientSocket1.getOutputStream(), true); // "true" means flush at end of line
            in = new BufferedReader(new InputStreamReader(clientSocket1.getInputStream()));
             //Create a buffered reader to read from standard input
                                  
            
            while(r != null) {
            	// Send the line to server
                out.println(r);
                // Wait for a reply
                String reply = in.readLine();
                System.out.println(reply);
                // Print out the reply to standard output
              }
            out.close();
            in.close();
            clientSocket1.close();
                       
    	} catch (IOException e)
    	{
    		System.err.println("QueryClient unable to connect to LoansServer");
    		//e.printStackTrace();
    		System.exit(0);
    	}
    }
    
    
    
  //Connect to Loans Server and send request
    public void run2(String ln2, String r)
    {
    	try
    	{  
    		clientSocket2 = new Socket(ln2, port1);
            out1 = new PrintWriter(clientSocket2.getOutputStream(), true); // "true" means flush at end of line
            in1 = new BufferedReader(new InputStreamReader(clientSocket2.getInputStream()));
                     
            while(r != null) {
            	out1.println(r);
            	// Wait for a reply
                String reply = in1.readLine();
                System.out.println(reply);
                 }
            
            out1.close();
            in1.close();
            clientSocket2.close();
            System.exit(0);
    	} catch (IOException e)
    	{
    		System.err.println("QueryClient unable to connect to CatalogServer");
    		//e.printStackTrace();
    		System.exit(0);
    	}
    	System.exit(0); 
    }
    

	public static void main(String[] args) {
		try
		{
			//Print out command line arguments
			System.out.println(args[0] + " " + args[1] + " " + args[2]+ " " + args[3]+ " " + args[4]+" " + args[5]);
			//Send request to different server according to request type
			if(args[4].equals("C") || args[4].equals("K")){(new QueryClient(Integer.parseInt(args[0]),Integer.parseInt(args[2]))).run2(args[3],(args[4]+" " + args[5]));System.exit(Integer.parseInt(args[0])); }
			else {if(args[4].equals("D")|| args[4].equals("L")){(new QueryClient(Integer.parseInt(args[0]),Integer.parseInt(args[2]))).run1(args[1],(args[4]+" " + args[5]));System.exit(Integer.parseInt(args[2]));}
			else{System.out.println("Wrong request type");System.exit(0);}}
			System.exit(0);
		} catch (Exception e)
		{
			System.err.println("Invalid command line arguments");
		}
	}

}

⌨️ 快捷键说明

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