/*********************************************************************** * Ce programme emet a destination du port argv[1] sur la machine * argv[0], le nom de fichier passe en argument dans argv[2]. * * Il recoit ensuite le contenu du fichier et le stocke dans un fichier * appele : argv[2].copy * Le protocole est le suivant : envoi dans un message du nom, reception * dans un message de la taille, puis reception des donnees proprement * dites. * * ex: java fileClient linux03 2005 filename ***********************************************************************/ package fileClientReceiverSingleMsg; import java.io.FileOutputStream; 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.HalfDuplexFTPWorker; import csc4509.MessageSequenceException; import csc4509.ReadMessageStatus; public class FileClientReceiverMsgMain { public static void main(String[] argv) throws MessageSequenceException { HalfDuplexFTPWorker mess; int messType; ReadMessageStatus status; SocketChannel rwChan; Socket rwCon; InetSocketAddress rcvAddress; long totalSize = 0L, fileSize; boolean debugState = true; FileOutputStream fos; FileChannel fc; if (argv.length != 3) { System.out.println("usage: java fileClient " + " "); return; } try { 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); if (debugState) { System.out.println("Asking for filename : " + argv[2]); } // put name size and file size into writeBuffer mess = new HalfDuplexFTPWorker(rwChan); mess.sendMsg(1, argv[2]); mess.clear(); status = mess.readMessage(); if (status != ReadMessageStatus.ReadDataCompleted) { System.out.println("Connection closed in read message 1"); return; } messType = mess.getMessType(); if (messType == 2) { fileSize = (Long) mess.getData(); } else throw new MessageSequenceException( "Awaiting MessageType 2 and receiving" + messType); if (totalSize < 0) { System.out.println("Not allowed to access filename : " + argv[2]); return; } fos = new FileOutputStream(argv[2] + ".copy"); fc = fos.getChannel(); totalSize = 0L; while (totalSize < fileSize) { status = mess.readMessage(); if (status == ReadMessageStatus.ChannelClosed) { System.out.println("Connection closed" + " in read message 3 received " + totalSize + "bytes"); break; } if (status == ReadMessageStatus.ReadDataCompleted) { messType = mess.getMessType(); if (messType == 3) { // raw data int writen; ByteBuffer wb = mess.getDataByteBuffer(); wb.flip(); writen = fc.write(wb); totalSize += writen; } else { if (messType == 4) { // serialized data int writen; ByteBuffer wb = mess.getDataByteBuffer(); wb.flip(); writen = fc.write(wb); totalSize += writen; } else { fos.close(); fc.close(); throw new MessageSequenceException( "Awaiting MessageType 3 or 4 and receiving " + messType); } } } if (status == ReadMessageStatus.ReadDataCompleted) { messType = mess.getMessType(); } } // fermeture du fichier et du socket d'emission fos.close(); fc.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 received."); } }