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

Access Control Methods in DBMS

Access control in DBMS Database access control is a method of allowing access to a company’s sensitive data only to those people (database users) who are allowed to access such data and to restrict access to unauthorized persons. It includes two main components: authentication and authorization. Authentication is a method of verifying the identity of a person who is accessing your database. Note that authentication isn’t enough to protect data. An additional layer of security is required, authorization, which determines whether a user should be allowed to access the data or make the transaction he’s attempting. Without authentication and authorization, there is no data security. Any company whose employees connect to the Internet, thus, every company today, needs some level of access control implemented. Types of Access Control Obsolete access models include Discretionary Access Control (DAC) and Mandatory Access Control (MAC). Role Based Access Control (RBAC) is the most common method...

Joins in DBMS

DBMS Joins: Inner, THETA, Outer, Equi Types of Join Operations Join in DBMS is a binary operation that allows you to combine join product and selection in one single statement. The goal of creating a join condition is that it helps you to combine the data from two or more DBMS tables. The tables in DBMS are associated using the primary key and foreign keys. Types of Join There are mainly two types of joins in DBMS: Inner Joins: Theta, Natural, EQUI Outer Join: Left, Right, Full Inner Join Inner Join is used to return rows from both tables which satisfy the given condition. It is the most widely used join operation and can be considered as a default join-type An Inner join or equijoin is a comparator-based join which uses equality comparisons in the join-predicate. However, if you use other comparison operators like “>” it can’t be called equijoin. Inner Join further divided into three subtypes: Theta join Natural join EQUI join Theta Join Theta Join allows you to merge two tables ba...

Object Oriented and Relational Database

Object Relational and Object-Oriented Database What Is Object Relational Database? An object-relational database (ORD) is a database management system (DBMS) that’s composed of both a relational database (RDBMS) and an object-oriented database (OODBMS). An object-relational database acts as an interface between relational and object-oriented databases because it contains aspects and characteristics from both models. Object-oriented database (ORD) serves two main purposes: It connects the divide between relational databases and the object-oriented modeling techniques that are usually used in programming languages like C#, Java and C++. It bridges the gap between conceptual data modeling techniques for relational and object-oriented databases like entry-relationship diagram (ERD) and object-relational mapping (ORM). What Is Object Oriented Database? An object-oriented database is organized around objects rather than actions and data rather than logic. Therefore, an object database is a d...