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

SQL Constraints, NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT, CREATE INDEX

SQL Constraints (Image by - Sharma Guides | Subham232330) Constraints   |   |- CHECK   |   |- NOT NULL   |   |- UNIQUE   |   |- PRIMARY KEY   |   |- DEFAULT   |   |- FOREIGN KEY   |   |- INDEX * These constraints are also known as Integrity Constraints . * SQL Constraints:- Constraints are the rules and restrictions applied to the data in a table. * NOT NULL:- Value cannot be Null in a column. * UNIQUE:- Value cannot be the same in a column. * PRIMARY KEY:- Used to uniquely identify a row. * FOREIGN KEY:- References a row in another table. * CHECK:- Satisfies a specific condition. * DEFAULT:- Set default value. * CREATE INDEX:- Used to speed up the reading process.

Normalization Types in DBMS

Normalization Types in DBMS First Normal Form (1NF) A relation will be 1NF if it contains an atomic value.  It states that an attribute of a table cannot hold multiple values. It must hold only single-valued attribute. First normal form disallows the multi-valued attribute, composite attribute, and their combinations. Example: Relation EMPLOYEE is not in 1NF because of multi-valued attribute EMP_PHONE. EMPLOYEE table: EMP_ID EMP_NAME EMP_PHONE EMP_STATE 14 John 7272826385, 9064738238 UP 20 Harry 8574783832 Bihar 12 Sam 7390372389, 8589830302 Punjab The decomposition of the EMPLOYEE table into 1NF has been shown below: EMP_ID EMP_NAME EMP_PHONE EMP_STATE 14 John 7272826385 UP 14 John 9064738238 UP 20 Harry 8574783832 Bihar 12 Sam 7390372389 Punjab 12 Sam 8589830302 Punjab Second Normal Form (2NF) In the 2NF, relational must be in 1NF.  In the second normal form, all non-key attributes are fully functional dependent on the primary key  Example: Let's assume, a school can ...

Authentication vs Authorization

Authentication vs Authorization - Authentication and authorization are the two words used in the security world. They might sound similar but are completely different from each other. Authentication is used to authenticate someone's identity, whereas authorization is a way to provide permission to someone to access a particular resource. These are the two basic security terms and hence need to be understood thoroughly. In this topic, we will discuss what authentication and authorization are and how they are differentiated from each other. What is Authentication? Authentication is the process of identifying someone's identity by assuring that the person is the same as what he is claiming for. It is used by both server and client. The server uses authentication when someone wants to access the information, and the server needs to know who is accessing the information. The client uses it when he wants to know that it is the same server that it claims to be. The authentication by t...