Send many files through a socket without closing Buffered Streams?

Hi,
I have an application that sends/receives files through a socket. To do this, on the receiver side I have a BufferedInputStream from the socket, and a BufferedOutputStream to the file on disk.
On the sender side I have the same thing in reverse.
As you know I can't close any stream, ever.. because that closes the underlying socket (this seems stupid..?)
therefore, how can I tell the receiver that it has reached the end of a file?
Can you show me any examples that send/receive more than one file without closing any streams/sockets?

Hi,
As you know I can't close any stream, ever.. because that closes the underlying socket (this seems stupid..?)Its not if you want to continuosly listen to the particular port.. like those of server, you need to use ServerSocket.
for sending multiple files the sender(Socket) can request the file to server (ServerSocket). read the contents(file name) and then return the file over same connection, then close the connection.
For next file you need to request again, put it in loop that will be better.
A quick Google gives me this.
Regards,
Santosh.

Similar Messages

  • How to send image file through mail without   any attachment

    Plz tell  me how to send image file through mail without any attachment  ( i mean not converting  that image into pdf or any format )  i want to send that text or image  through mail .

    Hi Sandeep,
    I think you can setup the type of email in Shared office Settings in transaction S016.
    There is an option called <Preset document classes>
    You choose this pushbutton to branch to the maintenance screen for the document classes that are directly displayed to users in the Business Workplace for selection when they use the Create function. The name under which the documents are displayed can also be maintained.
    http://help.sap.com/saphelp_nw70/helpdata/en/6c/69c30f418d11d1896e0000e8322d00/content.htm
    Haven't tried it though.
    Regards,
    Siddhesh

  • Sockets: can only send once file through

    Hi,
    I am using sockets to send text and files to a client on a Clio. I want to send multiple files through. However, only the first file goes through. The rest are never received (although they are uploaded). My question is:
    Why can not send anything through the socket (text or files) after the first file is sent?
    The fileSend() is on the server side, fileReceive is on the client side.
    public static void fileSend (Socket uploadSocket, String source) {
         try {
             InputStream inFile = new FileInputStream(source);
             InputStream in = new BufferedInputStream(inFile);
             OutputStream out = new BufferedOutputStream(uploadSocket.getOutputStream());
             System.out.println("Sending " + source + ".");
             int data;
             int bytes = 0;
             while ((data = in.read()) != -1) {
              bytes++;
              out.write(data);
             bytes++;
             out.write(data);
             if (in != null) in.close();
             if (out != null) out.flush();
             System.out.println("Upload complete: " + bytes + " Bytes!");
         catch (Exception e) {
             System.err.println("Couldn't upload " + source + ": " + e.getMessage());
       public static void fileReceive (Socket downloadSocket, String destination) {
         try {
             InputStream in = new BufferedInputStream(downloadSocket.getInputStream());
             OutputStream outFile = new FileOutputStream(destination);
             OutputStream out = new BufferedOutputStream(outFile);
             System.out.println("Downloading data to " + destination + ".");
             int data = in.read();
             int bytes = 0;
             while (data != -1) {
              bytes++;
              out.write(data);
              data = in.read();
             bytes++;
             if (out != null) {
              out.flush();
              out.close();
             outFile.close();
             System.out.println("Download complete: " + bytes + " Bytes!");
             in.skip(in.available());
         catch (Exception e) {
             System.err.println("Couldn't download " + destination + ": " + e.getMessage());
        }Thanks,
    Neetin

    I think its better to pass the outputstream to the filesend() method and inputstream to fileReceive() method
    something like this:
    OutputStream out = new BufferedOutputStream(uploadSocket.getOutputStream());
    public static void fileSend (OutputStream os, String source) {
      //write your file onto the output stream
    InputStream in = new BufferedInputStream(downloadSocket.getInputStream());
    public static void fileReceive (InputStream is) {
      //Read from the input stream
    }This should work.. Good luck

  • How to transfer a binary file through the socket?

    hi,
    i have a problem when I want to transfer a binary file through the socket. On the server side we write a program with C. On the client side with use java. The socket worked fine when we transfer some asci files. But failed when transfer the binary file. How to solve the problem? Can someone give me some advice? Thanks a lot.
    sincerely,
    zheng chuanbo

    i use streams to read the binary file. The problem is I don't know when the transfered stream finished. With text file I can recognize the end of the transfer by a special string, but how to deal with the end of a stream? should the server send an "EOF" or something else to the client to notify the end?

  • Why can't i send multiple files through airdrop ? , it accepts but shows only one photo

    why can't i send multiple files through airdrop ? , it accepts but shows only one photo

        Hello there Laurengrayce,
    I message all of the time and I want you to do the same, to anyone you would like!  Demmo provided a good suggestion of having this person check their services and ensuring they're setup as a 10-digit number, but I would like to provide some of my own suggestions.
    If they are in fact already setup as a 10-digit number, I would recommend deleting this persons contact from your phone as well as the entire messaging thread (long press and hold on thread to delete).  Readd them to your contact list and try again.
    Please advise if this continues and they've already checked with their provider.
    Thanks,
    MelissaM_VZW
    Follow us on Twitter @vzwsupport

  • How to send a file through the portal

    Hello,
    I have a problem in sending the file. I have selected a file kris.ppt and from context menu, I have selected sent to option and I have sent a file. I got a copy of that to my lotus notes but in that I had a format as kris.ppt.html. I cannot open the file. I can open the file when the portal is running. But I want to open the file when the portal is closed. Can anyone tell me the solution.
    cheers
    kris

    Hello Shankar,
    I tried that option also by default I'm getting 2 options.
    One is the kris.ppt.html and other is the kris.ppt. I cannot open the first one rather I could open the second. I can open the 1st one if the portal is opened. Can you suggest anything regarding that one.
    cheers
    kris

  • Can anyone help me in sending a file through FTP

    i have written a program to transfer files or directories in zipformat through FTP.i have also installed filezella server to check whether it is working or not.i have connected to filezella server but i am not able to send file.can any one send the code for sending the file.
    i have also tried the jakarta api sendfile ().but in that i don't what is mode means.
    for your reference i am sending the code as far developed.
    import java.io.*;
    import java.util.*;
    import java.util.zip.*;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import org.apache.commons.net.ftp.*;
    import org.apache.commons.net.tftp.*;
    class FTPTransfer
    static public void main(String[]p)
    FTPTransfer ob = new FTPTransfer();
    // ob.Create_Zip_File(new File("outfile.zip"),new File[]{new File("c:\\trialtwo")}, false);
    ob.ftpConnect();
    // connecting to FTP server
    public void ftpConnect()
    boolean error = false;
    FTPClient ftp = null;
    try
    int reply;
    //ftp.connect("ftp.comcast.com");
    ftp = new FTPClient();
    ftp.connect( "localhost",21);
    // ftp.login( username, password );
    System.out.print("reply from FTP Server :"+ftp.getReplyString());
    // After connection attempt, you should check the reply code to verify
    // success.
    reply = ftp.getReplyCode();
    if(!FTPReply.isPositiveCompletion(reply)) {
    ftp.disconnect();
    System.err.println("FTP server refused connection.");
    System.exit(1);
    // ftp.sendFile();
    // transfer files
    // ftp.logout();
    TFTPClient tftp = new TFTPClient();
    System.out.print("before sending");
    tftp.sendFile("c:\\trialtwo\\a-1.txt",1,new FileInputStream("c:\\trialtwo\\a-1.txt"),"localhost",21);
    System.out.print("after sending");
    catch(IOException e)
    error = true;
    e.printStackTrace();
    finally
    if(ftp.isConnected())
    try
    ftp.disconnect();
    catch(IOException ioe)
    // do nothing
    System.exit(error ? 1 : 0);
    //send files
    // filename - The name the remote server should use when creating the file on its file system.
    // mode - The TFTP mode of the transfer (one of the MODE constants).
    // host - The remote host receiving the file.
    //port - The port number of the remote TFTP server.
    private void listContents( File Zip_File, File dir, ZipOutputStream out )
    throws Exception
    String[] files;
    files = dir.list();
    byte[] buf = new byte[1024];
    File[] file = dir.listFiles();
    for (int i = 0; i < file.length; i++) {
    System.out.println("*********** "+file);
    // File f = new File(dir,file[i]);
    try{
    FileInputStream in = new FileInputStream(file[i]);
    out.putNextEntry(new ZipEntry(file[i].getPath()));
    int len;
    while((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
    in.close();
    catch(Exception e)
    zipEntry(Zip_File, file[i], out);
    } // end listContents()
    void Create_Zip_File(File Zip_File,File[] To_Be_Zipped_Files,boolean Skip_Dirs)
    try
    // Open archive file
    FileOutputStream stream=new FileOutputStream(Zip_File);
    ZipOutputStream out=new ZipOutputStream(stream);
    for (int i=0;i<To_Be_Zipped_Files.length;i++)
    //if (To_Be_Zipped_Files[i]==null
    // || !To_Be_Zipped_Files[i].exists()
    // || (Skip_Dirs ))
    // continue;
    System.out.println("Adding "+To_Be_Zipped_Files[i].getName());
    zipEntry(Zip_File, To_Be_Zipped_Files[i], out);
    out.close();
    stream.close();
    System.out.println("Finished zipping : "+ Zip_File.getAbsolutePath());
    catch (Exception e)
    e.printStackTrace();
    System.out.println("Error: " + e.getMessage());
    return;
    private void zipEntry(File Zip_File, File file, ZipOutputStream out) throws Exception
    if (file.isDirectory())
    listContents(Zip_File, file, out);
    return;

    You'll stand a better chance of getting help if you're more specific about what problems you're encountering. "Can't send file" doesn't say much.
    When you post code, please use[code] and [/code] tags as described in Formatting tips on the message entry page. It makes it much easier to read.

  • Sending a File Using DataGram Sockets

    I writting one java program that can be used to transfer a file from one computer to another computer.
    I wrote using Sockets.
    But i want to do that using DatagramSockets. Can we send a file using DataGramSockets?
    Please reply soon.

    You can use DataGram but it is harder.
    One issue is you need to retransmit missing packets. This means you need to keep track of which packets have been sent and be able to send missing ones again.
    If Socket work for you why do you want to use DataGrams

  • Sending audio files through web service

    Hi,
    How can we send audio (mp3,wmv) files through web services ? i have tried making a byte array of the file and then sending the byte array through the web service, but it give OutOfMemory error. so is there any better and practical way of doing it ?

    Just read catalina.bat and find where $JAVA command is called, for NT it should be "%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% "
    "%_EXECJAVA% is java
    %JAVA_OPTS% and %CATALINA_OPTS% are enviroment parameter for java command. Just added one of them in your case.
    By the way, " i am using a HP PDA on the client side, so i cannot increase the JVM heap size there", you should info. the client, otherwise the client may be get "out of memory".

  • How do you send a file through iMessages

    On my mac I can drag a file over to imessages, I can even download it on another id on the mac. I have tried to send the same file to my iphone and ipad and nada.
    Yet, I have no problem with pictures.
    How do you send files through iMessage to your ipad?

    Yes, that works from mac to mac. That is a wonderful feature.
    I did find Apple's list of supported files and it is not complete. It did not list PDF as a supported file so I suspect there are others. I am afraid that epub and some others that would make life easier are not supported at this time. I did find reference to where the file i on the ipad but you have to jailbreak your iphone and/or ipad to do that and I am not interested in doing that.

  • Sending multiple files using one socket

    Hi guys
    I'm working on a simple app that sends multiple files over LAN or I-NET. The problem is that the app run seems to be non-deterministic. I keep getting this error on the client side:
    java.io.UTFDataFormatException: malformed input around byte 5
            at java.io.DataInputStream.readUTF(Unknown Source)
            at java.io.DataInputStream.readUTF(Unknown Source)
            at service.DownloadManager.storeRawStream(DownloadManager.java:116)
            at service.DownloadManager.downloadFiles(DownloadManager.java:47)
            at manager.NetworkTransferClient$1.run(NetworkTransferClient.java:104)The byte position changes every time I run a transfer. The error is caused by this line: String fileName = in.readUTF(); Here's the complete code:
    Client
    private void storeRawStream() {                               
            try {
                FileOutputStream fileOut;                       
                int fileCount = in.readInt();           
                for(int i=0; i<fileCount; i++) { 
                    byte data[] = new byte[BUFFER];
                    String fileName = in.readUTF();               
                    fileOut = new FileOutputStream(new File(upload, fileName)); 
                    long fileLength = in.readLong();                                 
                    for(int j=0; j<fileLength / BUFFER; j++) {
                        int totalCount = 0;
                        while(totalCount < BUFFER) {                       
                            int count = in.read(data, totalCount, BUFFER - totalCount);
                            totalCount += count;                 
                        fileOut.write(data, 0, totalCount);
                        fileOut.flush();
                        bytesRecieved += totalCount;                                  
                    // read the remaining bytes               
                    int count = in.read(data, 0, (int) (fileLength % BUFFER));                                        
                    fileOut.write(data, 0, count);              
                    fileOut.flush();
                    fileOut.close();      
                    transferLog.append("File " + fileName + " recieved successfully.\n");  
            } catch (Exception ex) {
                ex.printStackTrace();
        }Server
    public void sendFiles(File[] files) throws Exception {
            byte data[] = new byte[BUFFER];
            FileInputStream fileInput;                                       
            out.writeInt(files.length);              
            for (int i=0; i<files.length; i++) {   
                // send the file name
                out.writeUTF(files.getName());
    // send the file length
    out.writeLong(files[i].length());
    fileInput = new FileInputStream(files[i]);
    int count;
    while((count = fileInput.read(data, 0, BUFFER)) != -1) {
    out.write(data, 0, count);
    bytesSent += count;
    fileInput.close();
    out.flush();
    Does anybody know where's the problem? Thanx for any reply.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    Send the length of each file ahead of each file, with DataOutputStream.writeLong().
    When reading, read that long, then stop reading bytes when you've read exactly that length.

  • Sending attachment file through utl_smtp package in oracle 8i

    dear friends,
    i am facing one problem here. I can send mail
    from oracle8i by using utl_smtp or utl_tcp package. but I am not able to send a file
    from perticular directory as attachments.
    how it will be possible. if any one have idea
    about this pl. guide me. it is very urgent.
    thanks in advance
    sunil kant pandey

    Can you provide me a sample code which sends a mail using utl_snmp package ?
    thanks in advance,
    kalpen.
    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by [email protected]:
    dear friends,
    i am facing one problem here. I can send mail
    from oracle8i by using utl_smtp or utl_tcp package. but I am not able to send a file
    from perticular directory as attachments.
    how it will be possible. if any one have idea
    about this pl. guide me. it is very urgent.
    thanks in advance
    sunil kant pandey<HR></BLOCKQUOTE>
    null

  • How to send XML files through Business Connector to client URL

    Dear ALL
    I am new to SAP BC. We have setup BC 4.8 and would like to send out a XML file from BC to Client URL. Could someone please guide me.
    Please suggest solutions.
    Thanks
    Ahmed

    Hello Mickael
    Thanks for your reply. No, we do not have PI. This BC will be used for point to point communication with client.
    Scenario:
    R/3 server to send XML files to BC. BC will load these files ( using pub.getfile service), this file is to be parsed using pub.loaddocument service and then sent to client in XML format wrapped with digital signature. As i am new to BC i am unable to parse this file and wrap it with the digital signaature to send it.
    Kindly advise on how best can we perform this action.
    Thanks
    Ahmed

  • Send Java objects through a socket

    I want to send some java objects to a J2SE server from a J2me socket. In normal j2me socket there is no way of putting whole obejct to the output stream. Is there any way that you can send a object from a socket ? Please advice..
    Thanks.

    Check this article on making serializable objects in MIDP. If the objects you want to send receive across the network implement this, then you can easily transform them into byte arrays and back to objects so they can be sent across a network.
    shmoove

  • Send other files through bluetooth

    could anyone suggest a way to send files other than music videos and offices through bluetooth. I wantd to share .pdf files and all, but its not working. And also please suggest me a gud filemanager for lumia 720

    Hi, raghav1988. Transferring PDF files via Bluetooth is not supported. However, you add it as a suggestion here: https://windowsphone.uservoice.com/forums/101801-feature-suggestions. skm93 We're unable to recommend a specific application. Suggest checking the Store using your phone for an app with that capability. Feel free to post here if you have further questions. 

Maybe you are looking for

  • Firefox opening pdfs

    I have a MacBookPro with OS X Version 10.7.5 and would like to open PDFs in Firefox 16.0.2. I have tried adding Google Docs Viewer, PDF Viewer 3.0.0.2 and PDF viewer 0.6.39 Can't get any of them to work Ideas? ClearKase

  • Minimize button doesn't appear.

    The minimize button doesn't appear in Photoshop CS for Mac, and I don't know how minimize the program and it's harming my workflow. I'm a new user on Mac.

  • Provide Feedback for the Final Cut Pro User Manual and Onscreen Help Here

    The Apple documentation team for Final Cut Pro would like to know what you think about the Final Cut Pro 5 Help documentation (available in the Help menu): • How often do you use the Final Cut Pro Help? Under what circumstances do you use it most? •

  • Jumpstart questions/problems

    OK fellas - hopefully this is an easy one for ya... I have a jumpstart server successfully set up using 12/02 release of Solaris 9...I want to be able to boot net - install and come back in an hour or so and be looking at the console login prompt. My

  • Module pool subscreen issue

    Hi I my module pool application, I have a tab strip having 4 tabs designed in local scrolling (PAI). But after entering some values on tab 1 , when I navigate to tab 2, the gui components of tab1 also appear on tab2  and screen becomes messed up. How