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

Computer Full Forms

COMPUTER - full form or meaning is :  Common Operating Machine Purposely Used for Technological and Educational Research. COMPUTER ABBREVIATIONS CPU - Central Processing Unit RAM - Random Access Memory ROM - Read Only Memory PROM - Programmable Read Only Memory EPROM - Erasable PROM EEPROM - Electrically EPROM HDD - Hard Disk Drive FDD - Floppy Disk Drive KBD - KeyBoard I/O - Input & Output CD - Compact Disk DVD - Digital Video Disk SMPS - Switch Mode Power Supply POST - Power ON Self Test BIOS - Basic Input Output System VDU - Visible Display Unit LED - Light Embedded Diode LCD - Liquid Crystal Display USB - Universal Serial Bus VGA - Video/Visual Graphic Adapter LAN - Local Area Network WAN - Wide Area Network MAN - Metropolitan Area Network HLL - High-Level Language LLL - Low-Level Language MIPS - Million of Instruction Per Second Mbps - Mega Bytes Per second Kbps - Kilo Bytes per second HTTP - Hyper Text Templates WWW - World Wide Web IP - Int...

DBMS Keys

DBMS Keys KEYS in DBMS is an attribute or set of attributes which helps you to identify a row (tuple) uniquely in a relation(table). They allow you to find the relation between two tables. Keys help you uniquely identify a row in a table by a combination of one or more columns in that table. Key is also helpful for finding unique record or row from the table. Database key is also helpful for finding unique record or row from the table. Example: Employee ID FirstName LastName 11 Andrew Johnson 22 Tom Wood 33 Alex Hale In the above-given example, employee ID is a primary key because it uniquely identifies an employee record. In this table, no other employee can have the same employee ID. Here are some reasons for using sql key in the DBMS system. Keys help you to identify any row of data in a table. In a real-world application, a table could contain thousands of records. Moreover, the records could be duplicated. Keys in RDBMS ensure that you can uniquely identify a table record despite ...

Colors in CSS

Ways to declare Colors in CSS (Image by - Sharma Guides | Subham232330) 1. Color Name 2. Hex Value 3. RGB() and RGBA() 4. HSL() and HSLA() 5. HWB() * Color Name:- background-color:red; * HEX Value:- background-color:#001122; * RGB():- background-color:rgb(25,31,52); * RGBA():- background-color:rgba(0,0,0,1.5);          |           Transparency The hexadecimal system uses values from 0 to 255 but in RGB we can use 0% to 100% as well.