Thursday, October 14, 2010

Wiltons Giant Cupcake Recipe

Java File Transfer System (Client Server) (TCP / IP) network file transfer system with Java


CLIENT ***************
fts package;
import java.net .*;
import IngressoUscita.Console;

public class Client {public static void

main (String [] args) throws UnknownHostException, ClassNotFoundException {


for (;;) {

Console.scriviStringa ("File Transfer System \\ n");
; String ip;
Console.scriviStringa ("start 'beginning' to exit 'go out' \\ n ");
Console.leggiStringa String asd = ();
if (asd.equalsIgnoreCase ("start")) {
; Console.scriviStringa ("Enter ip adresa Server");
; Console.leggiStringa ip = ();
InetAddress server_ip InetAddress.getByName = (ip);
Console.scriviStringa ("Enter IP:" + server_ip)
Chiesta chst = new Chiesta(ip_server, 2020);
                chst.chiesta();
              
            }
            if (asd.equalsIgnoreCase("esco")) {
                    break;
                }


            else {
                continue;
            }
        }
    }
}
***********

package fts;

import java.io.*;
import java.net.*;
import IngressoUscita.Console;

public class Chiesta {

    Socket so;
    private InetAddress server;
    private int port;
    private DataInputStream dis;
    private DataOutputStream dos;
    private DataOutputStream dosFile;
    private String dirDest;
    private String fileDest;

    public Chiesta(InetAddress ia, int pp) {
        this.server = ia;
        this.port = pp;
    }

    public void chiesta() throws ClassNotFoundException {

        try {
            so = new Socket (server, port);


DataInputStream dis = new (so.getInputStream ());
dos = new DataOutputStream (so.getOutputStream ());

num_file dis.readInt = int ();

Console.scriviStringa ("> File List: \\ n");
; int counter = 0;
counter = num_file;
String fileElenco;
Console.scriviStringa ("Server sends file number:" + num_file)
; while (counter> 0) {
fileElenco dis.readUTF = ();
System.out.println (fileElenco)
counter -;}

; Console.scriviStringa ("Which want to download file: enter name of file (sample.txt) \\ n");

String fileScelto ;
fileScelto Console.leggiStringa = ();
; dos.writeUTF (fileScelto)

Console.scriviStringa ("where to save the file, for example (C: \\ \\) \\ n");
dirDest = Console . leggiStringa ();

dirCheck File = new File (dirDest)
; if (! (dirCheck.exists ())) {
dirCheck.mkdir ();
;}

Console.scriviStringa (Insee the file name to save: esempio.doc] ");
fileDest = Console.leggiStringa();


            try {

                byte buffer;
                dosFile = new DataOutputStream(
                        new FileOutputStream(dirDest + "\\" + fileDest));

                while ((buffer = dis.readByte()) != -1) {
                    dosFile.writeByte (buffer);}


;} catch (Exception e) {
; Console.scriviStringa ("Caught exception" + e.getMessage());
            } finally {
                try {
                    if (dis == null) {
                        dis.close();
                    }
                } catch (IOException ioe) {
                }
            }
        } catch (IOException e) {
            System.out.println ("Caught exception:" + e.getMessage ());} finally {

try {

dos.close ( )
dis.close ();

so.close ();
;} catch (IOException e) {
; Console.scriviStringa ("Captured l'ecccezione: " + e.getMessage());
            }
        }
    }
}
*******
SERVER

package fts;

import java.io.*;
import java.net.*;
import IngressoUscita.Console;

public class Server {

    public static void main(String args[]) {
System.out.println ("Server waiting for client connection ..."); ServerSocket ss
;

try {ss = new ServerSocket (2020);

while (true) {Socket
aus ss.accept = ();
Console.scriviStringa ("\\ n Connected a client:" + aux);
; new Request (aus);

}} catch (IOException and )
{System.out.println ("Caught exception:" + e.getMessage ());
;}

}} ****************


package fts;

import java.io. *;
import java.net .*;
import IngressoUscita.Console;

public class Request extends Thread {private File dir

;
private File f;
private File [] listaFile;
private Socket know;
private DataInputStream disFile;
private DataInputStream dis;
private DataOutputStream dos;

public Request (Socket s) throws IOException {
this.so = s;
; dis = new DataInputStream (so.getInputStream ());
dos = new DataOutputStream (so.getOutputStream ());
, start ();
}

    public void run() {
        try {

            dir = new File("C:\\src");
            listaFile = dir.listFiles();
            dos.writeInt (listaFile.length);
Console.scriviStringa ("Send fillet to client ...");

; Console.scriviStringa ("command file list");
; if (listaFile.length> 0) {
for (int i = 0; i < listaFile.length; i++) {
                    if (listaFile[i].isFile()) {
                        dos.writeUTF("(" + i + ")" + listaFile[i].getName());
                    } else if (listaFile[i].isDirectory()) {
                        System.out.println("Directory " + listaFile[i].getName());
                    }
                }
            } Else {
dos.writeUTF ("File not found");



}} catch (Exception e) {
Console.scriviStringa ("Caught exception:" + e.getMessage ());}

try {String
fileScelto;
; fileScelto dis.readUTF = ();

for (int i = 0; i < listaFile.length; i++) {
                if (fileScelto.equals(listaFile[i].getName())) {
                    f = listaFile[i];
                    byte buffer;
                    disFile = new DataInputStream(
                            new FileInputStream(f));

                    try {
                        while ((buffer = disFile.readByte()) != -1) {
                            dos.writeByte(buffer);
                        }
                        {
                        }
                    } Catch (FileNotFoundException fnfe) {
Console.scriviStringa ("File not found");
} catch (IOException IOE) {
Console.scriviStringa ("Problems reading file.");
} finally {
                        try {
                            if (dis != null) {
                                dis.close();
                            }
                        } catch (IOException ioe) {
                            Console.scriviStringa ("Caught exception:" + ioe.getMessage ());

}
} } else {
                    Console.scriviStringa("File non cè");
                }
           
}} catch (Exception e) {
Console.scriviStringa ( "Caught exception:" + e.getMessage ());} finally {

try {
Console.scriviStringa ("Close the connection:" + s);
dis.close () ;
dos.close ();
; so.close ();
} catch (IOException e) {
; Console.scriviStringa ("Caught exception:" + e.getMessage ());
;}

}}}

0 comments:

Post a Comment