/*********************************************************************** * Ce programme emet a destination du port argv[1] sur la machine * argv[0], le fichier passe en argument dans argv[2]. * * L'emission se fait par send sur un SocketChannel. * * Le protocole est le suivant : envoie de la taille du nom, puis du nom, puis * envoie de la taille du fichiers, suivi des donnees proprement * dites. * * ex: java fileClient linux03 2005 filename ***********************************************************************/ package fileClientSenderSingle; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.SocketChannel; import csc4509.HeaderFTP; public class FileClientSenderMain { public static void main(String[] argv) { ByteBuffer writeBuf; SocketChannel rwChan; Socket rwCon; InetSocketAddress rcvAddress; long totalSize = 0L; boolean debugState = true; File file; FileInputStream fis; FileChannel fc; HeaderFTP head; if (argv.length != 3) { System.out.println("usage: java fileClient " + " "); return; } try { writeBuf = ByteBuffer.allocate(4096); InetAddress destAddr = InetAddress.getByName(argv[0]); rwChan = SocketChannel.open(); rwCon = rwChan.socket(); // on recupere l'adresse IP de la machine cible rcvAddress = new InetSocketAddress(destAddr, Integer.parseInt(argv[1])); // on connecte le socket d'emission au port distant rwCon.connect(rcvAddress); file = new File(argv[2]); fis = new FileInputStream(file); fc = fis.getChannel(); totalSize = file.length(); head = new HeaderFTP(argv[2], totalSize); head.putInByteBuf(writeBuf, 1); if (debugState) { System.out.println("Sending : " + head); } writeBuf.flip(); rwChan.write(writeBuf); writeBuf.clear(); int readSize = 0; totalSize = 0L; do { // lecture d'un bloc du fichier original readSize = fc.read(writeBuf); if (readSize > 0) { totalSize += readSize; writeBuf.flip(); rwChan.write(writeBuf); if (writeBuf.position() == writeBuf.limit()) { writeBuf.clear(); } else { writeBuf.compact(); } } } while (readSize > 0); // fermeture du fichier et du socket d'emission fc.close(); fis.close(); rwChan.close(); rwCon.close(); } catch (SocketException se) { se.printStackTrace(); return; } catch (UnknownHostException ue) { ue.printStackTrace(); return; } catch (IOException ie) { ie.printStackTrace(); return; } System.out.println(totalSize + " bytes sent."); } }