it copy the file with 0 kb.
can anyone please help me.
I am working in this over a week no progress.
server class
package javaapplication29;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
boolean listening = true;
try
{
serverSocket = new ServerSocket(7777);
}
catch (IOException e)
{
System.err.println("Could not listen on port: 7777");
System.exit(1);
}
System.out.println("Multi server ready");
Socket socket = null;
while (listening)
{
try
{
socket = serverSocket.accept();
new EchoThread(socket).start();
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
}
serverSocket.close();
}
}
class EchoThread extends Thread
{
// Library lib = new Library();
private Socket socket = null;
public EchoThread(Socket socket)
{
this.socket = socket;
}
public void run()
{
System.out.println("enter to run1");
Socket incomingConnection = null;
incomingConnection = socket;
ResultSet rs;
PrintWriter out;
BufferedReader in;
String inputLine = null;
try
{
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
do {
inputLine = in.readLine();
System.out.println("the input is----:"+inputLine);
String [] newinputLine = null;
newinputLine = inputLine.split("#");
System.out.println(newinputLine[0]);
// /*
// * Add Movie
// * add's new movie to database
// */
// if (newinputLine[0].matches("add movie")){
// lib.addItem(newinputLine[1],newinputLine[2],Integer.parseInt(newinputLine[3]),newinputLine[4],Integer.parseInt(newinputLine[5]),newinputLine[6],newinputLine[7],newinputLine[8],newinputLine[9],Integer.parseInt(newinputLine[10]));
// out.println(inputLine);
// }
// /*
// * Add Song
// * add's new song to database
// */
// else if (newinputLine[0].matches("add song")){
// lib.addItem(newinputLine[1],newinputLine[2],Integer.parseInt(newinputLine[3]),newinputLine[4],Integer.parseInt(newinputLine[5]),newinputLine[6],newinputLine[7],newinputLine[8],newinputLine[9],Integer.parseInt(newinputLine[10]));
// out.println(inputLine);
//
// }
// /*
// * Search by Movies Year
// * returns movie between years ("year 1" and "year 2")
// */
// else if (newinputLine[0].matches("search movies by year")){
// String ReturnString="";
//
// System.out.println(newinputLine[1]);
// rs = lib.searchByYearMovie(Integer.parseInt(newinputLine[1]),Integer.parseInt(newinputLine[2]));
// System.out.println(" first ReturnString:" + ReturnString);
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
// out.println(ReturnString);
// System.out.println("search Movies by year ends");
// }
// /*
// * Search Songs by Year
// * returns songs between years ("year 1" and "year 2")
// */
// else if (newinputLine[0].matches("search songs by year")){
//
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// System.out.println("newinputLine[1] : "+newinputLine[1]);
// rs = lib.searchByYearSong(Integer.parseInt(newinputLine[1]),Integer.parseInt(newinputLine[2]));
//
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
//
//
// out.println(ReturnString);
// System.out.println("search Songs by year ends");
// }
// /*
// * Search movie
// * returns all the movies
// */
// else if (newinputLine[0].matches("search movie")){
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// rs = lib.ShowAllMovies();
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
//
//
// out.println(ReturnString);
// System.out.println("search by name ends");
// }
// /*
// * Search Song
// * returns all the songs
// */
// else if (newinputLine[0].matches("search song")){
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// rs = lib.ShowAllSongs();
//
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// out.println(ReturnString);
// System.out.println("search by name ends");
// }
// /*
// * Search Movies by Category
// * returns all the movies with the same category
// */
// else if (newinputLine[0].matches("search movies by category")){
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// System.out.println(newinputLine[1]);
// rs = lib.searchByCatogorMovie(newinputLine[1]);
//
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
// out.println(ReturnString);
// System.out.println("Search Movies by Category ends");
//
// }
// /*
// * Search Movies by Name
// * returns all the movies with the same name
// */
// else if (newinputLine[0].matches("search movies by name")){
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// System.out.println(newinputLine[1]);
// rs = lib.searchByNameMovie(newinputLine[1]);
//
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
// out.println(ReturnString);
// }
//
// /*
// * Search Song by Name
// * returns all the song with the same name
// */
// else if (newinputLine[0].matches("search Song by name")){
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// System.out.println(newinputLine[1]);
// rs = lib.searchByNameSong(newinputLine[1]);
//
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
// out.println(ReturnString);
// }
//
// /*
// * Search Songs by Category
// * returns all the songs with the same Category
// */
// else if (newinputLine[0].matches("search songs by category")){
//
// String ReturnString="";
// System.out.println(" first ReturnString:" + ReturnString);
// System.out.println(newinputLine[1]);
// rs = lib.searchByCatogorSong(newinputLine[1]);
//
// try {
// while (rs.next()) {
// ReturnString = ReturnString + rs.getString(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6) + "#" + rs.getString(7) + "#" + rs.getString(8) + "#" + rs.getString(9) + "#" + rs.getString(10) + "@";
// System.out.println("while ReturnString:" + ReturnString);
// }
// } catch (SQLException ex) {
// System.out.println("catch no resaultset");
// Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println(ReturnString);
// out.println(ReturnString);
// System.out.println("Search Songs by Category ends");
//
// }
// /*
// * Delite All
// * delets all movie and Song
// */
// else if (newinputLine[0].matches("delete all")){
// lib.removeAll();
// System.out.println("deta base clear");
// out.println(inputLine);
// }
// /*
// * Delite by Name
// * delets one movie by name(works on the id)
// */
// else if (newinputLine[0].matches("delete by name")){
// System.out.println("enter to delete by name" );
// lib.removeItem(newinputLine[1], Integer.parseInt(newinputLine[2]));
// out.println(inputLine);
// }
// /*
// * Send Movie
// * send the file of the movie to server
// */
// else if (newinputLine[0].matches("sends movie")){
// }
// /*
// * Send Song
// * send the file of the song to server
// */
// else if (newinputLine[0].matches("sends song")){
// }
// /*
// * Get Movie
// * download the movie from server
// */
// else if (newinputLine[0].matches("get movie")){
// }
// /*
// * Get Song
// * download the song from server
// */
// else if (newinputLine[0].matches("get song")){
// }
// /*
// * Edit Song
// * Edit Song or Movie to Data Base
// */
// else if (newinputLine[0].matches("edit items")){
// System.out.println( newinputLine[0]);
// lib.editItem(newinputLine[1],newinputLine[2],newinputLine[3],Integer.parseInt(newinputLine[4]),newinputLine[5],Integer.parseInt(newinputLine[6]),newinputLine[7],newinputLine[8],newinputLine[9],newinputLine[10],Integer.parseInt(newinputLine[11]));
// out.println(inputLine);
// }
/*
* copy file - here is the problem
* Edit Song or Movie to Data Base
*/
System.out.println( "working");
handleConnection(socket);
// out.println(inputLine);
}
while(inputLine.toUpperCase().equals("END")==false);
out.close();
in.close();
socket.close();
}
catch (IOException e)
{
System.out.println("couldn't read from connection");
}
}
/**
* Interacts with the client Socket to send the contents of the requested
* file to the client.
*
* @param incomingConnection
*/
public void handleConnection(Socket connectionToHandle) {
new Thread(new ConnectionHandler(connectionToHandle)).start();
}
}
client class:
/**
*
*/
package javaapplication29;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* The client uses Socket to connect to a server. The server listens on port
* 3000 with a ServerSocket. The client requests the contents of a file on the
* server's path.
*
* @author Natalie
*
*/
public class RemoteFileClient {
protected String hostIp;
protected int hostPort;
protected InputStream socketReader;
protected PrintWriter socketWriter;
protected FileOutputStream out;
/**
* The Constructor of our class takes an IP address and a port number for a
* remote host and assigns them to instance variables.
*/
public RemoteFileClient(String aHostIp, int aHostPort) {
hostIp = aHostIp;
hostPort = aHostPort;
}
/**
* main Creates the RemoteFileClient, use it to get the contents of a remote
* file, and then prints the result.
*
* @param args
*/
public static void main(String[] args) {
RemoteFileClient remoteFileClient = new RemoteFileClient("127.0.0.1",
7777);
// Open connection with server
remoteFileClient.setUpConnection();
File fileDst = new File("D:\\cool\\Michael Jackson - Pepsi Adverteint.mpg");
remoteFileClient.downloadByteFile("D:\\Michael Jackson - Pepsi Adverteint.mpg",
fileDst);
System.out.println("close the connection with server");
// close the connection with server
remoteFileClient.tearDownConnection();
}
/**
* Connect to remote server. It will set up our Socket and give us access to
* its streams.
*/
public void setUpConnection() {
try {
Socket client = new Socket(hostIp, hostPort);
System.out.println("Multithreaded Client: Connected...");
socketReader = client.getInputStream();
socketWriter = new PrintWriter(client.getOutputStream());
} catch (UnknownHostException e) {
System.out
.println("Error setting up socket connection: unknown host at "
+ hostIp + ":" + hostPort);
} catch (IOException e) {
System.out.println("Error setting up socket connection: " + e);
}
}
/**
* Ask the remote server for the contents of file.
*
* @param fileNameToGet
* @return
*/
public void downloadByteFile(String fileNameToGet, File fileDst) {
// src.RemoteFileClient.createAfileIfNotExists(fileDst);
try {
// send request to the host
System.out
.println("Multithreaded Client: send file path to server");
socketWriter.println(fileNameToGet);
socketWriter.flush();
System.out.println("Multithreaded Client: file path has sent");
DataInputStream in = new DataInputStream(socketReader);
// client send file path to server
out = new FileOutputStream(fileDst);
// client got file size from server
System.out.println("Multithreaded Client: wait for file size");
Long fileSize = in.readLong();
System.out.println("Multithreaded Client: got file size");
if (fileSize != null) {
int received = 0;
byte[] buff = new byte[1024];
System.out
.println("Multithreaded Client: starting download...");
long sizeCount = 0;
while (((received = in.read(buff)) > 0)) {
out.write(buff, 0, received);
sizeCount = sizeCount + received;
System.out.println("sent" + sizeCount);
if ((sizeCount >= fileSize)) {
break;
}
}
out.close();
System.out
.println("Multithreaded Client: download complete successfully!");
}
System.out
.println("Multithreaded Client: notify server about download success.");
socketWriter.println("Got");
socketWriter.flush();
System.out.println("Multithreaded Client: client finished.");
in.close();
} catch (IOException e) {
System.out.println("Multithreaded Client: Error reading from file "
+ fileNameToGet);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
System.out
.println("Multithreaded Client: Error wile trying to close a file "
+ fileDst.getName());
e.printStackTrace();
}
}
}
}
/**
* Disconnect from the remote server.
*/
public void tearDownConnection() {
try {
socketWriter.close();
socketReader.close();
System.out.println("Multithreaded Client: connection closed");
} catch (IOException e) {
System.out
.println("Multithreaded Client: Error tearing down the connection\n"
+ e.getStackTrace());
}
}
}
ConnectionHandler class:
/**
*
*/
package javaapplication29;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* @author Natalie
*
*/
public class ConnectionHandler implements Runnable {
Socket socketToHandle;
/**
* Constructor
*
* @param aSocketToHande
*/
public ConnectionHandler(Socket aSocketToHande) {
socketToHandle = aSocketToHande;
}
public void run() {
try {
DataOutputStream streamWriter = new DataOutputStream(socketToHandle
.getOutputStream());
BufferedReader streamReader = new BufferedReader(
new InputStreamReader(socketToHandle.getInputStream()));
// Server get file path from client
String fileToRead = streamReader.readLine();
File file = null;
if (fileToRead != null) {
file = new File(fileToRead);
// Server send file size in bytes to client
streamWriter.writeLong(file.length());
System.out.println(" File Length" + file.length());
}
System.out.println("Multithreaded Server: Starting send a file..."
+ fileToRead);
FileInputStream fileReader = new FileInputStream(fileToRead);
byte[] buff = new byte[1024];
int sent = 0;
while ((sent = fileReader.read(buff)) > 0) {
streamWriter.write(buff, 0, sent);
System.out.println("while");
}
fileReader.close();
// get message from client if he got a file successfully
String status = streamReader.readLine();
System.out.println("Multithreaded Server: client recieve status "
+ status);
if (status.equalsIgnoreCase("got")) {
System.out.println("Multithreaded Server: Sent successfully!");
} else {
System.out
.println("Multithreaded Server: Error in sending file!");
}
streamWriter.close();
streamReader.close();
System.out.println("Multithreaded Server: Stoped...");
} catch (Exception e) {
System.out
.println("Multithreaded Server: Erro handling a client\n "
+ e.getStackTrace());
}
}
}