📄 loansserver.java
字号:
import java.net.*;
import java.io.*;
public class LoansServer {
ServerSocket socket = null;
int port = 0;
public LoansServer(int port)
{
this.port=port;
}
public void run(String path)
{
//Read Loans File into an array and printout
try {
FileReader input = new FileReader(path);
int n=-1;
String[] StringArr = new String[5];
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 exception 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("LoansServer waiting for incoming connections");
while(true)
{
try{clientSock = socket.accept();
/*System.out.println("LoansServer Connection accepted from " +
clientSock.getInetAddress().getHostName() +
" port " + clientSock.getPort());*/}
catch (SocketException e2){e2.printStackTrace();}
// 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("L"))
{
try {
FileReader input = new FileReader("loans-file");
int n=-1;
String[] StringArr = new String[5];
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 Loans by user id
for (int i=0;i<StringArr.length;i++){
if(StringArr[i].substring(0, 6).equals(split[1])){
//System.out.println("Loans record of user id "+split[1]+" is "+StringArr[i]);
out.println("Loans record of "+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("LoansServer Connection closed");
System.err.println("LoansServer waiting for incoming connections");
break;
}
else {if(split[0].equals("D"))
{
try {
FileReader input = new FileReader("loans-file");
int n=-1;
String[] StringArr = new String[5];
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 due date by book id
for (int i=0;i<StringArr.length;i++){
if(StringArr[i].substring(7, 13).equals(split[1])){
//System.out.println("This book's due date:"+StringArr[i].substring(14, 22));
out.println("The due date of book id "+split[1]+" is "+StringArr[i].substring(14, 22));
}}
}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("LoansServer Connection closed");
System.err.println("LoansServer waiting for incoming connections");
break;
}
}
out.close();
in.close();
clientSock.close();
}
}
}
catch (IOException e) {
System.err.println("LoansServer unable to listen on given port");
System.exit(0);
}
}
public static void main(String[] args) throws IOException {
try
{
(new LoansServer(Integer.parseInt(args[0]))).run("loans-file");
} catch (NumberFormatException e)
{
System.err.println("Invalid command line arguments for LoansServer");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -