I have a code for socket that reads a text file and takes user input and display
ID: 3636162 • Letter: I
Question
I have a code for socket that reads a text file and takes user input and display it back.
I need to add some modifications as the following:
1. read a specific line from the same text file based on user's input..for example: if user enters ID, I will display the name and GPA for that ID.
2. if user enters nothing, I will display all the contents of the file.
3. otherwise, close the socket connection
here is the code for server and client
// server
import java.io.*;
import java.io.FileReader.*;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class FinalServer {
public static void main(String[] args) throws Exception {
// create socket, open server socket
ServerSocket servsock = new ServerSocket(6676);
System.out.println("Server started...");
System.out.println("Waiting for a client...");
// Wait for the Client Request and accept a connection
Socket sock = servsock.accept();
// Get the input and output streams of the socket,
//so that you can receive and send data to the client.
InputStream sin = sock .getInputStream();
OutputStream sout = sock.getOutputStream();
// Just converting them to different streams, so that string handling becomes easier.
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String ID = null;
while(true) {
ID = in.readUTF(); // wait for the client to send a line of text.
System.out.println("This " + ID + " ID number was received from the client ");
System.out.println();
out.writeUTF(ID); // send the same line back to the client.
out.flush(); // flush the stream to ensure that the data reaches the other end.
// ***************************//
// sendfile
File myFile = new File("rrr.txt");
byte[] mybytearray = new byte[(int) myFile.length()];
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(myFile));
bis.read(mybytearray, 0, mybytearray.length);
// Get a communication stream associated with the socket
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
// writes on the console
System.out.println(" File data: ");
System.out.write(mybytearray);
System.out.println("");
os.flush();
System.out.println("File transfering is done.");
System.out.println("Terminates the connection with client socket.");
sock.close();
// Get the input and output streams of the socket,
//so that you can receive and send data to the client.
}
}
}
// client
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class FinalClient {
public static void main(String[] argv) throws Exception {
// create a socket with the server's hostname and port
Socket sock = new Socket("127.0.0.1", 6676);
System.out.println("Got a client...");
System.out.println("Connecting");
//byte[] mybytearray = new byte[1024];
// get the input and output streams of the socket to send and recive data
InputStream is = sock.getInputStream();
OutputStream os = sock.getOutputStream();
// Just converting them to different streams, so that string handling becomes easier.
DataInputStream in = new DataInputStream(is);
DataOutputStream out = new DataOutputStream(os);
// Create a stream to read from the keyboard.
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String ID = null;
System.out.println("Type in your ID, and I will send it back to the server: ");
System.out.println();
while(true) {
// wait for the user to type in something and press enter.
// Receive data from the server:
ID = bf.readLine();
System.out.println("Sending this ID # " + ID +" to the server..!!");
out.writeUTF(ID); // send the data to the server.
out.flush(); // flush the stream to ensure that the data reaches the other end.
ID= in.readUTF(); // wait for the server to send a line of text.
System.out.println();
// ends of reading ID
///////////////////////////////////////////////
byte[] mybytearray = new byte[1024];
InputStream ins = sock.getInputStream();
System.out.println("Reading file from server...");
// the file name should be different from the file specified in server program
FileOutputStream fos = new FileOutputStream("rrrcopy.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
// writes on the console
System.out.println(" File data: ");
System.out.write(mybytearray, 0, bytesRead);
System.out.println("");
bos.close();
System.out.println("File saved successfully.");
System.out.println("Client terminated");
sock.close();
}
}
}
Explanation / Answer
/** * Please consider this with changes... * Please find changes with the comment NEW CODE */ //server import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.StringTokenizer; public class FinalServer { public static void main(String[] args) throws Exception { // create socket, open server socket ServerSocket servsock = new ServerSocket(6676); System.out.println("Server started..."); System.out.println("Waiting for a client..."); // Wait for the Client Request and accept a connection Socket sock = servsock.accept(); // Get the input and output streams of the socket, //so that you can receive and send data to the client. InputStream sin = sock .getInputStream(); OutputStream sout = sock.getOutputStream(); // Just converting them to different streams, so that string handling becomes easier. DataInputStream in = new DataInputStream(sin); DataOutputStream out = new DataOutputStream(sout); String ID = null; /** * New Code to terminate socket */ while(true) { ID = in.readUTF(); // wait for the client to send a line of text. boolean status = false; if("Q".equalsIgnoreCase(ID)){ System.out.println("Terminates the connection with client socket."); sock.close(); break; } System.out.println("This " + ID + " ID number was received from the client "); System.out.println(); out.writeUTF(ID); // send the same line back to the client. out.flush(); // flush the stream to ensure that the data reaches the other end. // ***************************// // sendfile File myFile = new File("rrr.txt"); BufferedReader br = new BufferedReader( new FileReader(myFile)); String strLine=""; byte[] mybytearray = (byte[])null; /** * New Code to Read FILE */ if(ID!=null && !ID.isEmpty()){ //Read File Line By Line while ((strLine = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(strLine); String token= st.nextToken(); if(token.equals(ID)){ mybytearray = new byte[strLine.getBytes().length]; mybytearray = strLine.getBytes(); status=true; break; } } if(!status){ strLine="ID not exist in File"; mybytearray = new byte[strLine.getBytes().length]; mybytearray = strLine.getBytes(); } }else{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile)); mybytearray = new byte[(int)myFile.length()]; bis.read(mybytearray, 0,(int) myFile.length()); } /** * EOF NEW CODE */ //bis.read(mybytearray, start,end); // Get a communication stream associated with the socket OutputStream os = sock.getOutputStream(); os.write(mybytearray, 0, mybytearray.length); // writes on the console System.out.println(" File data: "); System.out.write(mybytearray); System.out.println(""); os.flush(); System.out.println("File transfering is done."); // Get the input and output streams of the socket, //so that you can receive and send data to the client. }// } } //client import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.DataOutputStream; import java.io.DataInputStream; import java.io.InputStreamReader; import java.net.Socket; public class FinalClient { public static void main(String[] argv) throws Exception { // create a socket with the server's hostname and port Socket sock = new Socket("127.0.0.1", 6676); System.out.println("Got a client..."); System.out.println("Connecting"); //byte[] mybytearray = new byte[1024]; // get the input and output streams of the socket to send and recive data InputStream is = sock.getInputStream(); OutputStream os = sock.getOutputStream(); // Just converting them to different streams, so that string handling becomes easier. DataInputStream in = new DataInputStream(is); DataOutputStream out = new DataOutputStream(os); // Create a stream to read from the keyboard. BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String ID = null; while(true) { System.out.println("Type in your ID, and I will send it back to the server(Enter Q to quit): "); System.out.println(); // wait for the user to type in something and press enter. // Receive data from the server: ID = bf.readLine(); System.out.println("Sending this ID # " + ID +" to the server..!!"); out.writeUTF(ID); // send the data to the server. out.flush(); // flush the stream to ensure that the data reaches the other end. /** * New Code to terminate socket */ if("Q".equalsIgnoreCase(ID)){ System.out.println("Client terminated"); sock.close(); break; } ID= in.readUTF(); // wait for the server to send a line of text. System.out.println(); // ends of reading ID /////////////////////////////////////////////// byte[] mybytearray = new byte[1024]; InputStream ins = sock.getInputStream(); System.out.println("Reading file from server..."); // the file name should be different from the file specified in server program FileOutputStream fos = new FileOutputStream("rrrcopy.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos); int bytesRead = is.read(mybytearray, 0, mybytearray.length); bos.write(mybytearray, 0, bytesRead); // writes on the console System.out.println(" File data: "); System.out.write(mybytearray, 0, bytesRead); System.out.println(""); bos.close(); System.out.println("File saved successfully."); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.