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

📄 catalogserver.java

📁 服务/客户端 查询 非完全版本
💻 JAVA
字号:
import java.net.*;
import java.io.*;


public class CatalogServer {
	ServerSocket socket = null;
	int port=0;
	
	public CatalogServer(int port)
	{
		this.port=port;
	}
	
	public void run(String path)
	{
		//Read Catalog File into an array and printout
			 try {
		        	FileReader input = new FileReader(path);					
		            int n=-1;
		            String[] StringArr = new String[10];
			    BufferedReader bufRead = new BufferedReader(input);
			    String line=""; 	// String that holds current file line
		            line = bufRead.readLine();
		            while (line != null){
		              	 n++;
		            	 String lineNew = "";
		            	 lineNew = line;
		                 line = bufRead.readLine();
		                StringArr[n] = new String(lineNew);  
		              }
		           
		            for   (int   x=0;x<StringArr.length;x++){   
		                       	 System.out.println(StringArr[x]);}
		            bufRead.close();
					
		        }catch (ArrayIndexOutOfBoundsException e){
		            /* If no file was passed on the command line, this expception is
					generated. */
					System.out.println("Usage: java ReadFile filename\n");			

				}catch (IOException e){
					// If another exception is generated, print a stack trace
		            e.printStackTrace();
		        }
				//Listen for incoming requests on given port 	
		try
		 {		
			socket = new ServerSocket(port);
			Socket clientSock = null;
			PrintWriter out = null;
			BufferedReader in = null;
			System.err.println("CatalogServer waiting for incoming connections");
			
			while(true)
			{
				clientSock = socket.accept();
				/*System.out.println("CatalogServer Connection accepted from " +
						clientSock.getInetAddress().getHostName() +
					    " port " + clientSock.getPort());*/
				
			    // Create a PrintWriter and BufferedReader for interaction with our stream
			    
			    out = new PrintWriter(clientSock.getOutputStream(), true);
			    in = new BufferedReader( new InputStreamReader(clientSock.getInputStream()));
			
			  //Accepts Query Client request and response
			    String line1;
			    
			    while ((line1=in.readLine()) != null) {
					String[] split = line1.split(" ");
					if(split[0].equals("C"))
					{
						
						try {
							
				        	FileReader input = new FileReader("Catalog-file");					
				            int n=-1;
				            String[] StringArr = new String[10];
					        BufferedReader bufRead = new BufferedReader(input);
					        String line=""; 	
				            line = bufRead.readLine();
				            while (line != null){
				              	n++;
				            	
				            	String lineNew = "";
				            	   lineNew = line;
				                line = bufRead.readLine();
				                StringArr[n] = new String(lineNew);  
				              }
				           
				            bufRead.close();
				            //Search Catalog by book id
				           for (int i=0;i<=9;i++){   
				   		     if(StringArr[i].substring(0, 6).equals(split[1])){
				   		    	//System.out.println(StringArr[i]);
				   		    	 out.println("Details of book id "+split[1]+": "+StringArr[i]);
				   		    	  }}
				           
				        }catch (ArrayIndexOutOfBoundsException e){
				           
							System.out.println("Usage: java ReadFile filename\n");			

						}catch (IOException e){
							
				            e.printStackTrace();
				        }
					
						//Acknowledge closure and print to stderr
						System.err.println("CatalogServer Connection closed");
						System.err.println("CatalogServer waiting for incoming connections");
						break;}
					else {if(split[0].equals("K")) 
					{
                     try {
							FileReader input = new FileReader("Catalog-file");					
				            int n=-1;
				            String[] StringArr = new String[10];
					    BufferedReader bufRead = new BufferedReader(input);
					    String line=""; 	// String that holds current file line
				            line = bufRead.readLine();
				            while (line != null){
				                n++;
				            	String lineNew = "";
				            	lineNew = line;
				                line = bufRead.readLine();
				                StringArr[n] = new String(lineNew);  
				              }
				           	bufRead.close();
				            //Search catalog by key word
				           for (int i=0;i<=9;i++){   
				   		     if(StringArr[i].substring(7).contains(split[1])){
				   		    	//System.out.println(StringArr[i]);
				   		    	 out.println("Search Result: "+StringArr[i]);
				   		    	  }}
				           
				        }catch (ArrayIndexOutOfBoundsException e){
				           
							System.out.println("Usage: java ReadFile filename\n");			

						}catch (IOException e){
							
				            e.printStackTrace();
				        }
						//Acknowledge closure and print to stderr
						System.err.println("CatalogServer Connection closed");
						System.err.println("CatalogServer waiting for incoming connections");
						break;
					}
					
					}
					out.close();
				    in.close();
				    clientSock.close();
			    }
			   
			}	
		    
		}
		catch(IOException e1){System.err.println("CatalogServer unable to listen on given port");
		//e1.printStackTrace();
		System.exit(0);}
	}
	

	public static void main(String[] args) throws IOException {
	  try	{

			(new CatalogServer(Integer.parseInt(args[0]))).run("Catalog-file");

				
		     } catch (Exception e)
		{
			System.err.println("Invalid command line arguments for CatalogServer");
			System.err.println(e);
		}
		}}

		

⌨️ 快捷键说明

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