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

Similar Messages

  • Can u tell how to send the pictures using datagrams sockets in java

    hi to all, i am making chat application , i am datagrams socket(packets) for sending and receiving messages . datagrams only send and receive messages in bytes . can u tell me how to send the picture to other person using datagrams.
    Thankyou.

    look her
    http://forum.java.sun.com/thread.jsp?forum=31&thread=330989&tstart=0&trange=15
    and look up Serialisation

  • 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.

  • How to send a file using IOCP?

    When using blocking sockets, all I had to do to send a file was to open the file and loop through it and send it in chunks.
    But I find sending a file using overlapped sockets to be more challenging. I can think of the following approach to do it:
    I open the file and send the first chunk, and I keep track of the file handle and file position.
    Now when I get a completion packet indicating that some data has been sent, I check to see if the socket is currently in the process of sending a file, and if it is, I retrieve the file handle and file position and send the next chunk.
    I repeat step 2 until I reach the last chunk in the file, and then I close the file.
    Is this approach correct?
    Note: I don't want to use TransmitFile().

    This approach is more or less correct, but maybe you'd have to know some more things.
    If send "returns" it means, that you buffer has been copied into the internal buffer of system or the network interface card or whatever... in general it means, that you can free/reuse the buffer you have used, but it doesn't mean, that the data
    has been delivered (it does not even mean it has been sent already).
    That's why I'm normally using some flow-control (messages from the receiver) to verify the real data flow.
    The next point is, that you shouldn't read from the file only after you got the ok that the first chunk has been sent. You should read the data as soon as possible so that you can respond much quicker to a send-complete-message. I'd recommend to send using
    multiple buffers.
    Rudolf

  • Help: Send and Receive through Datagram Socket

    I am trying to create a client/server using Datagram Socket (UDP) as communication. I have a Recive and a Send class that implements runnable and are responsible for receiving and sending of message. I have two other classes Server and Client that are utilizing these. Now i can get the receive and send to work fine, but i am having trouble retrieving the message from the receive thread that i created in each of server/client. I've tried inserting a method so the server/client can call on to get the message (Receive.Get_Message()) but unfortunately this isn't working as i wanted. Since my server and clients are not implemented as runnables, i can only issue the command in some serialize fashion. What i really want to do is that whenever the message is receive by the Receive thread, it will immediately be send back to server/client and display right then, independent of what i was doing. (i have a command interface for S/C so it just sits there to wait for command, now what i would like is to have the S/C display the message it gets immediately after it gets it even when it's waiting for user input. since my program is waiting for a command, i really could not use the method describe above to achieve this) Could anyone help me with this?
    Thanks!
    Hung-Hsun Su

    u cant use the observer pattern. or at least i can't figure out how to implement it with sockets.
    you should use threading.
    public void run()
      while(true)
         //keep reading from socket
         InStream output = socket.getInputStream();
         InputStreamReader ir = new InputStreamReader(output)
         BufferedReader reader = new BufferedReader(ir);
         String s = null;
         while(s == null)
           s = reader.readLine();
         //then do whatever you want to the received string "s"
    }

  • 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.

  • Sending empty files using SFTP Adapter

    I am trying to send empty files using SFTP adapter. The interface has to send the file whether its empty file or containing data using SFTP adapter. I am using BizTalk Server 2013 R2. Is it a bug or the hotfix is already there for this issue.

    The issue here is not your SFTP not able to send 0KB files, but the file receive adapter that is receiving the file. The file adapter deletes 0 KB files and doest not transmit it further.
    If u have a ftp receive for example you should be able to send 0KB files.
    If u have a custom file receive adapter , is it handling 0KB files ?
    Regards &lt;br/&gt; When you see answers and helpful posts,&lt;br/&gt; please click Vote As Helpful, Propose As Answer, and/or Mark As Answer

  • Send A file Using JMS

    I want to send a file Using JMS.. and want to have
    These Details in the message that send.
    Receivers IP address & The file
    That mean I want to send a File from one client to another client Using a Queue
    Can Anyone help me to solve this problem
    thanks

    Read the file then use a JMS BytesMessage or TextMessage. Add your own JMS header for the recievers ip and any other info you want to annotate the message with.
    Watch out for character encoding and max file size problems.
    If your client / server are not on the same network you're likely to run into firewall issues.
    Don't kid yourself that this will be a transactional solution.
    Assumes both clients are Java (JMS headers are a pain to read otherwise)

  • How to send large files using web service

    hello everyone,
    I am new to this forum, so please pardon me if I post some silly problem...
    I have created one service which sends file when client (jsp) request it. I am using JBOSS as my server. purpose of this application is when client request some fle then service will send this file... and most of the time we need to send only pdfs and ppts...
    Problem is, this service sends txt, java files easily of any size but when i tried sending PDF, PPT then i got xml.SAXParseException.......
    I thought this error is because of some characters, but how to fix it....
    I am working on Linux.
    code snippet is:
    import java.io.*;
    public class MyHelloService
    public String file_size (String name)
         String s = new String("");
    byte[] sendata1=new byte[100];
         try
              System.out.println("name recived is :::::::::::"+name);
              FileInputStream in=new FileInputStream(name);
              int size=0;
              size=in.available();
              System.out.println("FILE SIZE IS:::::"+size);
              byte[] sendata11=new byte[size];
              i=in.read(sendata11);
              System.out.println(new String(sendata11));
              s=new String(sendata11);
         catch(Exception e)
                   System.out.println("EXCEPTION IN JWS:::"+e);
                   s=new String("nofilefounderror");
         return s;
    pls tell me what am i doing wrong ad how to fix this?
    and one more thing can i send byte array from a web service as i tried but couldnt do that... so i am reading everything in a single byte array and then converted to string.....
    is it possibel to send file in a chunk?if yes, how to do that?
    waiting for the reply..... pls reply as soon as possible....
    Rashi

    hi,
    I am sending file from server to client i.e client will request for a file and service will send it back....... no socket connection is there...I am using JBOSS and apache axis.
    pls help me out.....
    Rashi

  • How to send a file using RMI

    hello,,
    I do one program using RMI that shoud transfer a file.. I compiles and no errors .while i am trying to execute this program i found some exception .
    rmiUnmarshalling exception and writeAborted Exception.
    ////Server.java
    import java.rmi.*;
    import java.rmi.server.*;
    import java.io.*;
    public class Server extends UnicastRemoteObject implements serverInterface
         private InputStream in;
         String filename;     
         public Server(String filename) throws RemoteException
              this.filename = filename;          
         public InputStream send() throws RemoteException
              try
                   in = new FileInputStream(filename);
                   int c;
                   while((c = in.read()) != -1)
                        System.out.println((char)c);
              catch(Exception ex)
                   System.out.println("Exception in impl : "+ex);
              return in;
         public static void main(String arg[])
              try
                   Naming.rebind("Server" , new Server("E:\\muthukumar\\RMI\\files\\interinter.java"));
                   System.out.println("Server is ready");
              catch(Exception e)
                   System.out.println("Exception in server : "+e);
    /// serverInterface.java
    import java.rmi.*;
    import java.io.*;
    public interface serverInterface extends Remote
         public InputStream send() throws RemoteException;
    ///Client.java
    import java.rmi.*;
    import java.rmi.server.*;
    import java.io.*;
    class Client
         public static void main(String arg[])
              try
                   serverInterface inter= (serverInterface)Naming.lookup("//localhost/Server");
                   InputStream in = inter.send();
                   int c;     
                   while((c = in.read()) != -1)
                        System.out.println((char)c);
              catch(Exception e)
                   System.out.println("Exception in client : "+e);
    please help me...
    thanks in advance..
    regards,
    Muthukumar

    If you know the file is too large to send as one chunk, then call different methods where you have a method to start saving the file then a method that saves chunks at a time and then a close when you're done. Otherwise, you could probably open a socket stream and have a reader on the client side and a writer on the server side.
    There are probably better ways to transfer files for sure. RMI is Remote Method Invocation similar to a Windows RPC (Remote Procedure Call) so that you can run some code on one machine from another. Streams and sockets are probably better for transfer. Another way you could do it is to use RMI to start up a socket listener like, but not limited to, FTP and then send the file through that API and then you could use RMI to stop the listener when you're done so it is only available when you need it. Even easier is to just have an FTP server running and transfer the file with the API. You can even use FTPS and you don't have to bother with the encryption to protect the data over the net.

  • File format while sending a file  using FTP Process

    Hi,
    I am facing a formatting problem when i send a file from SAP application server to a different sever using FTP process.
    The problem is like this:
    Lets say I have a file with 10 records on the application server. When i am downloading this file to a pc, the file is showing 10 lines.
    But when i open the same file on the other server it is showing 10 records as 1 line.
    The file i am sending is a text file.(ex: acc_payable.txt)
    If i open the same file on the other server using word pad it is showing 10 records in 10 different lines.
    I want the file to be opened using notepad and want to see the each record in a different line
    Can anybody help me on this issue
    Regards,
    Radhakrishna

    Another stab at "simplest": can you avoid embedding
    newline characters in the strings
    your write and reply on PrintWriter's
    println() methods instead?But, if I understand the OP, this will cause the same problem. He is running on Unix but wants to generate a DOS type eol. The println() method will use the OS default; therefore, he needs to explicitly specify the DOS eol.

  • How to send A file using Webservice?

    I heard about webservice when I read through XML...
    It a bit like XML cannot show off it strength without webservice...lol
    What is webservice? What library should I use to send a file from server to client using webservice?
    Webservice is Quite new for me. Any expert here can direct me to the tutorial link that can teach me how to use webservice?
    I go through some code :
    <?xml version="1.0"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://namespaces.cafeconleche.org/xmljava/ch2/"
    xmlns="http://namespaces.cafeconleche.org/xmljava/ch2/"
    elementFormDefault="qualified">What is xmlns? Is webservice can run it at localhost? My house don't have internet. :(
    Thank....

    to learn webservice..you can try these samples...
    http://www.csd.abdn.ac.uk/~bscharla/teaching/CS5302/practicals/practical8.shtml

  • How to send Excel File using file adapter

    Hello,
    Please could you tell how i can send an excel file using the file adpater?
    thank you.

    Hi hassiba ,
    If u want to doc on creating modules just check the wonderful document.
    How To Create Modules for the J2EE Adapter Engine at service.sap.com. It will clear all the basics of creating Adapter module.
    https://websmp102.sap-ag.de/~form/sapnet?_SHORTKEY=01100035870000582377&
    Thanks ,
    sekhar.

  • Sending/receiving files using SFTP

    Hi,
    Is there a way to use the Secure File Transfer Protocol (SFTP) in ABAP to send/receive files?
    I already used the FTP functionality from the function group SFTP, but I'm honestly not sure whether ABAP also handles the secure version of FTP.
    If there is this functionality, please provide me the necessary function modules, ...
    Thanks in advance
    Bye,
    Timo

    FTP functionality woks fine in ABAP. Below is some code which can be used for FTP access to send/receive files.
    Initially, get values for the below fields
      DATA: l_user(30) TYPE c VALUE <userid>,
            l_pwd(30)  TYPE c VALUE <password>,
            l_host(64) TYPE c VALUE <host string>,
      DATA: l_slen     TYPE i,
            l_error,
            l_pwd(30)  TYPE c.
      CONSTANTS: c_dest TYPE rfcdes-rfcdest VALUE 'SAPFTP',
                 c_key  TYPE i              VALUE 26101957.
    connect to ftp server
      l_pwd = p_pwd.
      l_slen = STRLEN( l_pwd ).
      CALL FUNCTION 'HTTP_SCRAMBLE'
        EXPORTING
          SOURCE      = l_pwd
          sourcelen   = l_slen
          key         = c_key
        IMPORTING
          destination = l_pwd.
      CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
        EXPORTING
          text = 'Connect to FTP Server'.
      CALL FUNCTION 'FTP_CONNECT'
        EXPORTING
          user            = l_user
          password        = l_pwd
          host            = l_host
          rfc_destination = c_dest
        IMPORTING
          handle          = w_hdl
        EXCEPTIONS
          OTHERS          = 1.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.
      CLEAR : w_outfile, w_file_count.
    concatenate <directory> <filename> to field.
    MOVE field to w_outfile.
    PERFORM download_to_ftp TABLES <int'table>
                            USING w_outfile.
    FORM download_to_ftp TABLES it_tab
                         USING l_string TYPE char64.
      CALL FUNCTION 'FTP_R3_TO_SERVER'
        EXPORTING
          handle         = w_hdl
          fname          = l_string
          character_mode = 'X'
        TABLES
          text           = it_tab[]
        EXCEPTIONS
          tcpip_error    = 1
          command_error  = 2
          data_error     = 3
          OTHERS         = 4.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
                RAISING invalid_output_file.
      ENDIF.
    Above is used to send.
    For receiving use
      CALL FUNCTION 'FTP_SERVER_TO_R3'
        EXPORTING
          handle         = w_hdl
          fname          = l_string
          character_mode = 'X'
        TABLES
          text           = it_tab[]
        EXCEPTIONS
          tcpip_error    = 1
          command_error  = 2
          data_error     = 3
          OTHERS         = 4.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
                RAISING invalid_output_file.
      ENDIF.
    Hope this will help U.
    Pl. award appropriate points.

  • How to send TDMS file using TCP/IP

    Hi,
    I'm working with a cRIO-9025, chassis cRIO-9118 and NI 9239 module.
    I've done an acquisition (voltage) where data is in 1D Wfm SGL. I've saved this data using a TDMS file, but now I want to send this file to an other computer (in this case mine) because the file is stored in the target. To do that I've implemented this example:
    http://zone.ni.com/devzone/cda/epd/p/id/2842#0requirements
    The problem is that the tdms file is created but it hasn't any data. I attach the image of the program. The program is done in other project (not which I use to make acq) to make it easy and then implement it when it works.
    *The name file in the client is not used because I had a conflict with path.
    What I'm looking for is make an acquisition of 10 minutes every 30 minutes and then send this data to an other computer (or others). I'm not sure if it's the best way, maybe is better to send data at the same time as the acquisition is being done, but my theacher told me that is easier if I send the file when acquisition has finished. To make acq I use FPGA and RT.
    I would appreciate your help. Thanks!!
    Solved!
    Go to Solution.

    I believe the FTP VIs are built into LabVIEW 2011SP1 - I have the VI present, but I could have installed the internet toolkit (don't know).
    Look in the following location:   Function Palette -> Connectivity -> Internet -> FTP
    You're really looking for the VI named, "FTP Get File.vi".  What this VI does is contacts an FTP server and requests a file from it.  If your files are on the target, and you know the filename that will be created (such as files created in a sequence) you can request the file from the target and have them copied to the local machine (Windows).  RT cannot "push" files to the Windows side unless you have an FTP server (like FileZilla Server) running on the Windows host.
    It's a very simple one-VI solution.  You specify host (the IP Address of the target you're pulling files from), the remote path for the file (for instance if the file is located in c:\ni-rt\data\myfile.zip then that's what you put as the remote path of the file), the local path for the file (where you want the file copied to, along with the name of the file, such as c:\mydir\myfile.zip), the username to log into the FTP server with (default is 'anonymous') and the password to use (default is an empty string).  The importan thing to note is whether to use a binary transfer or not - if the file is a text file, no binary transfer needed; if the file is a binary file then you get better results using a binary transfer - I prefer to always use binary transfers, as non-binary transfers actually "interpret" and "convert" text information in the files.
    -Danny

Maybe you are looking for

  • Sender JDBC channel stop send messages

    Hi, We have  a sender JDBC scenario which is working correctly time ago. However, sometimes this channel   stop sending messages although the Adapter Monitor shows that they regularly execute the polling interval. We have confirmed that there isn't a

  • Grouping of Procurement elements for requirements lying in various months

    Dear all, Is there any Lot sizing procedure wherein only one Procurement Element (PR/ Planned Order) to be generated after the MRP run for the requirements lying in different months. With lot sizing procedure "MB" it is possible to group the PR gener

  • Getting an Error when running LR installation

    This is the error I get when I try to intall the downloaded trial Lightroom:  Operation could not be completed. (com.apple.installer.pagecontroller error -1.)

  • TS1503 what is the other part of syncing

    When I go to sync my phone there is a huge part of "other" in yellow that is taking up a lot of space. I have deleted photos and music to help with space but nothing is helping. What is "other" on my phone?

  • Classic and a 03 iMac

    I My 17 G4 1GHz 2003 iMac Refuses to install or boot any version of OS9 or 10.0-10.2. It will boot fine into Panther to Leopard10.5.8. Is this a firmware issue or is it incapable of booting into classic. I have done some looking and apparently this m