Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will write an application that consists of a client and server parts. The ap

ID: 3559947 • Letter: Y

Question

You will write an application that consists of a client and server parts. The application behaves like a client and a server at the same time.

1. The server role. It waits for a message from client. When a message is received it processes it. The 1st message would be a query, asking if a certain file exists on the system The response should be YES/NO The 2nd message would request the file The server should start sending the file in parts. The size of each part would depend on the size of the file. You need to decide what should be the size of each part. Your design should consider lost parts during transmission, out of order parts arriving, and finally some metho to indicate that the file trnsfer is complete.

2. Client role There should be a command interface where you are trying to get a file using file name. (for added functionality, consider partial file name, or just file types - .jpg, mpeg etc.) The client should send a message to N servers. The client recevies replies from N-X servers. Some o the replies will be "YES" others will be "NO" Select one of the servers that replied "YES". Request the file from the server. The file may arrive in parts. reassemle the file.

Explanation / Answer

FServer.java

import java.net.*;
import java.io.*;
public class FServer
{
public static void main(String args[]) throws Exception
{
ServerSocket serverSoc1 = new ServerSocket(95);

Socket clientSoc1 = serverSoc1.accept();
System.out.println("Connected to : " + clientSoc1);
DataInputStream br=null;
   PrintStream pw=null;
   br=new DataInputStream(clientSoc1.getInputStream());
   pw=new PrintStream(clientSoc1.getOutputStream());
Thread.sleep(2000);
String s = br.readLine();
System.out.print(s);
   File f = new File(s);
   if (f.exists())
    {
     
     BufferedReader d = new BufferedReader(new FileReader(s));
     String line;
     while((line = d.readLine()) != null)
     {
      pw.println(line);
      pw.flush();
     }
     d.close();
   }
   else{
       pw.println("NO");
       pw.flush();
   }
   pw.close();
   clientSoc1.close();
   serverSoc1.close();
}
}


FClient.java

import java.net.*;
import java.io.*;
public class FClient
{
public static void main(String args[]) throws Exception
{
Socket soc1 = null;
    soc1 = new Socket(InetAddress.getLocalHost(), 95);
DataInputStream br=null;
   PrintStream pw=null;
   br=new DataInputStream(soc1.getInputStream());
   pw=new PrintStream(soc1.getOutputStream());
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter FileName...");
String s = br1.readLine();
System.out.print(s);
pw.println(s);
while ((s=br.readLine()) != null)
{
   System.out.println(s);
}
br.close();
soc1.close();
}
}
   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote