Skip to main content

Implement a file server to transfer a file from server side to client side.

Implement a file server to transfer a file from server side to client side.



Server-side


import java.net.*;
import java.io.*;
public class ftpserv
{
public static void main(String args[]) throws Exception
{
ServerSocketss = null;
Socket s = null;
try
{
ss = new ServerSocket(4000);
System.out.println("Connecting with client");
s = ss.accept();
System.out.println("Connection is successful");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = br.readLine( );
BufferedReader br2= new BufferedReader(new FileReader(str) );
System.out.println("File found");
// keeping output stream ready to send the contents
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
String str2;
while((str2 = br2.readLine()) != null) // reading line-by-line from file
{
pw.println(str2); // sending each line to client
}
System.out.println("File send successfully");
}
catch (SocketException e)
{ System.out.println(e);}
catch (FileNotFoundException e)
{
System.out.println("File does not exist");
}
catch (IOException e)
{ System.out.println(e); }
catch (Exception e) { System.out.println(e); }
finally
{
s.close();
ss.close(); // closing network sockets
}}}





Client side


import java.net.*;
import java.io.*;
public class ftpclient
{
public static void main( String args[ ] ) throws Exception
{
Socket s = null;
try
{
s = new Socket( "localhost", 4000);
System.out.print("Enter path of file atserver: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println(str);
BufferedReader br2 = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str2;
System.out.print("Enter new path of file at client: ");
str2= br.readLine();
BufferedWriter bw = new BufferedWriter(new FileWriter(str2));
while((str2 = br2.readLine()) != null) // reading line-by-line
{
bw.write(str2); bw.newLine();
}
bw.close();
System.out.println("File saved");
}
catch (SocketException e)
{ System.out.println(e); }
catch (FileNotFoundException e)
{
System.out.println("File does not exist");
}
catch (IOException e)
{ System.out.println(e); }
catch (Exception e)
{ System.out.println(e); }
finally
{
s.close();
}}}

Comments

Popular posts from this blog

JS Code for Generating OTP

JS Code for Generating OTP -  * Learn how to create a simple JavaScript function to generate a random 4-digit OTP. (GENERATED BY - ChatGPT) function OTP() { let otp = ""; otp = Math.floor(Math.random() * 9000 + 1000); return otp; } console.log("Your OTP is-", OTP());

Concurrency Control

What is Concurrency Control? Concurrency Control in Database Management System is a procedure of managing simultaneous operations without conflicting with each other. It ensures that Database transactions are performed concurrently and accurately to produce correct results without violating data integrity of the respective Database. Concurrent access is quite easy if all users are just reading data. There is no way they can interfere with one another. Though for any practical Database, it would have a mix of READ and WRITE operations and hence the concurrency is a challenge. DBMS Concurrency Control is used to address such conflicts, which mostly occur with a multi-user system. Therefore, Concurrency Control is the most important element for proper functioning of a Database Management System where two or more database transactions are executed simultaneously, which require access to the same data. Potential problems of Concurrency Here, are some issues which you will likely to face wh...

Top 10 Most Famous Photographers of All Time

*Top 10 Most Famous Photographers of All Time* If you want to take truly memorable and moving photographs, you can learn something by studying the pictures of famous photographers. Some of the most beloved artists are deceased, but some are still delighting us with their photographs. The list below includes some of the more famous photographers that still impact our lives today. 1. *Ansel Adams* is probably the most easily recognized name of any photographer. His landscapes are stunning; he achieved an unparalleled level of contrast using creative darkroom work. You can improve your own photos by reading Adams’ own thoughts as he grew older, when he wished that he had kept himself strong enough physically to continue his work. 2. *Yousuf Karsh* has taken photographs that tell a story, and that are more easily understood than many others. Each of his portraits tells you all about the subject. He felt as though there was a secret hidden behind each woman and man. Whether he captures a gl...