Filetransfer over sockets

hi,
im currently working on a filetransfer over sockets. this is what i do:
i have a connection from the client to the server. on both sides i have a PrintStream for the output and a bufferedReader to read the responses. the client asks for a file and the server answers with the md5-hash size of the file, so that the client knows how many bytes it has to read. that works fine.
now the filetransfer:
the server writes the bytes of the file directly to the OutputStream (not the PrintStream). the client opens a BufferedInputStream on the mentioned socket and reads in as many bytes, as the server said before.
now the problem:
sometimes the filetransfer works fine, but sometimes some bytes are missing. i wonder where they are gone. it cant be a problem with the flush() method, because im using OutputStream to send the data. (i tested BufferedOutputStream with flush() with the same results).
maybe it's not working because a attached a BufferedReader and a BufferedInputStream to the InputStream on the client-side...
does anybody know, whats going wrong?
let me know if you want to see some code.
greetings,
henrik

i have solved this problem by using java.nio

Similar Messages

  • Synchronizing peers in transfer over Socket

    Hi again!
    I'm relatively new to Java, so there are many issues I still have to figure out ;-)
    Here are two of them:
    1) How can peers be synchronized in transfer over Socket? For example:
    This is the receiving peer:
    DataInputStream dsin = new DataInputStream(socket.getInputStream());
    String s = dsin.readUTF();This is the sending peer:
    DataOutputStream dsout = new DataOutputStream(socket.getOutputStream());
    dsout.writeUTF("something...");
    dsout.close();At the receiving peer in some cases the EOFException or SocketException (Socket closed) are raised. I suspect this happens either because the receiver tries to read input before it is fully transfered or when the sender already managed to close the socket's OutputStream.
    So how can such a transfer be synchronized?
    2) What if both peers close their socket's OutputStream? Does this result in closing the connection and releasing all its resources, such as port?
    Thank you everyone!

    this is what I learnt:Sorry, try again!
    DO NOT close the Socket on any endpoint until you make sure that the other endpoint managed to call socket.getInputStream().This 'rule' is quite unnecessary.
    According to the documentation, getInputStream() may return a stream which buffer was discarded by network software if a broken connection was detected.That's a complete misreading and misquoting of the documentation. A reset can happen to the connection at any time; can cause loss of data from te socket receive buffer; and has nothing to do with the operation of getting the input stream. Getting the input stream of a socket has nothing to do with the network or the state of the connection. It just constructs a Java object. So this rule is quite pointless.
    Even more, it seems to be a good practice not to close the Socket until you are sure that the other endpoint will not try to read from the channel.You've got that back to front as well. THink about that some more and you will see it involves an infinite regression.
    The only rules you need are these:
    (a) don't close the socket until you're sure the other end has finished writing. You have to close the socket while the other end is still reading, so that it will get a EOS indication, so it will know you've gone away, so it will stop writing and close its end of the socket.
    (b) closing the socket or either of its streams closes the other two items. You should always close the topmost output stream of a socket and nothing else.

  • Send XML over Sockets

    I am beginning and I need to send XML formatted data from Swing Client to Server for parsing/processing/and response.
    There are no web server available to use. I think its best to use socket connection, but how do I send xml data over socket, and how do I receive it from server and parse it?
    I have read and cannot find a similar example.

    Xerces has DOMSerializer which can be used to serialize a Document object to a string. Once it's a string it's trivial to transmit over a socket connection. On the other side, to parse it, you'll have to create a StringReader for the received string and then an InputSource from the StringReader. You'll then be able to pass the InputSource to the DocumentBuilder.parse( ) method to generate a Document on the receiving side.
    Hope that helps,
    - Jesse

  • What is the advantage of using RMI over socket connection

    plz tell me guys what is the advantage of using RMI over socket connection bcoz inherently RMI also uses socket connection.so what is the exact difference in between thm and what is the advantage of using RMI over socket connection.

    i knew tht bt i http://www.catb.org/~esr/faqs/smart-questions.html#writewell
    How To Ask Questions The Smart Way
    Eric Steven Raymond
    Rick Moen
    Write in clear, grammatical, correctly-spelled language
    We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding (often enough to bet on, anyway). Answering questions for careless and sloppy thinkers is not rewarding; we'd rather spend our time elsewhere.
    So expressing your question clearly and well is important. If you can't be bothered to do that, we can't be bothered to pay attention. Spend the extra effort to polish your language. It doesn't have to be stiff or formal — in fact, hacker culture values informal, slangy and humorous language used with precision. But it has to be precise; there has to be some indication that you're thinking and paying attention.
    Spell, punctuate, and capitalize correctly. Don't confuse "its" with "it's", "loose" with "lose", or "discrete" with "discreet". Don't TYPE IN ALL CAPS; this is read as shouting and considered rude. (All-smalls is only slightly less annoying, as it's difficult to read. Alan Cox can get away with it, but you can't.)
    More generally, if you write like a semi-literate b o o b you will very likely be ignored. So don't use instant-messaging shortcuts. Spelling "you" as "u" makes you look like a semi-literate b o o b to save two entire keystrokes.

  • URGENT : Pass non-serializable objects over sockets

    I am working with a non-serializable class.I can't change it to serializable(since it's an API class).I can't create a subclass either as I need to pass the same exact Object .I have tried wrapping it in a serializable class (as transient )but then the particular object that I need is not serialized.Is there a way to pass non-serializable objects over sockets?.Some other way of doing this?. Should i use RMI or Externalize?. Any help would be appreciated.

    {snip}
    Like this:
    public class SerializableLibraryClass extends
    LibraryClass implements Serializable {
    }Then you've got a class that is exactly the same as
    LibraryClass, but is now serializable.Not quite. The base class still isn't Serializable. Take, for example, the following code:
    public class A
        public int a1;
    public class B extends A implements Serializable
        public int b1;
    B beforeSend = new B();
    beforeSend.a1 = 1;
    beforeSend.b1 = 2;
    objectOutputStream.writeObject(b);
    B afterSend = (B)(objectInputStream.readObject());Both beforeSend and afterSend have fields a1 and b1. For beforeSend, a1 = 1 and b1 = 2. For afterSend, b1 = 2 but a1 = 0. The superclass wasn't Serializable; therefore, its members weren't Serialized and, when the object was created on the other side of the stream, a1 was left at its initial value.
    In short, you can't just Serialize the subclass. That Serialization won't apply to its superclasses.

  • Pass non-serializable objects over sockets

    I am working with a non-serializable class.I can't change it to serializable(since it's an API class).I can't create a subclass either as I need to pass the same exact Object .I have tried wrapping it in a serializable class (as transient )but then the particular object that I need is not serialized.Is there a way to pass non-serializable objects over sockets?.Some other way of doing this?.

    Have you considered leaving it in situ and using RMI to manipulate it remotely ?
    To be a little pedantic, if you need the "same exact object" then there's no technique that will work for you, since you can only copy an object between JVMs, not move it.
    D.

  • Problem with Sending files over Sockets

    Hi. I have a problem when I try to send files over sockets. When I send text files it works well but when I try to send .jpg or other kind of files it doesn't work well because some characters are wrong, I hope you can help me.
    Here is my code:
    //Sender code
    import java.io.*;
    import java.net.*;
    class Servidor{
         Servidor(){
              try{
                   String arch="art.jpg";
                   ServerSocket serv=new ServerSocket(125);
                   Socket socket=serv.accept();
                   System.out.println("Conectado");
                   int c;
                   BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                   FileInputStream fis=new FileInputStream(arch);
                   File f=new File(arch);
                   bw.write(""+f.length());
                   bw.newLine();
                   bw.flush();
                   System.out.println("Escribiendo");
                   while((c=fis.read())!=-1){
                        bw.write(c);
                   bw.flush();
                   fis.close();
                   System.out.println("Terminado");
                   while(true){
              }catch(Exception e){
                   System.out.println(e);
         public static void main(String arg[]){
              new Servidor();
    //Client code
    import java.io.*;
    import java.net.*;
    class Cliente{
         Cliente(){
              try{
                   Socket socket=new Socket("localhost",125);
                   System.out.println("Conectado");
                   BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                   long tam=Long.parseLong(br.readLine());
                   long act=0;
                   FileOutputStream fos=new FileOutputStream("resp.jpg");
                   System.out.println("Recibiendo: "+tam);
                   while(act<tam){
                        fos.write(br.read());
                        act++;
                   System.out.println("Terminado: "+act+" / "+tam);
                   fos.close();
              }catch(Exception e){
                   System.out.println(e);
         public static void main(String arg[]){
              new Cliente();
    }

    I don't think you want BufferedReader and OutputStreamWriter, you want ByteArrayOutputStream and ByteArrayInputStream . The problem is that you are sending jpegs (binary data) as text.

  • Image over sockets

    Hello,
    What I'm basically trying to do is to send an Image among strings over sockets.
    I have a serializable object and I'm sending back and forth. In this object I have an ImageIcon but when I try to send it to the client I'm getting the following error on the server-side:
    failed to load image contents
    java.io.IOException: failed to load image contents
    at javax.swing.ImageIcon.writeObject(ImageIcon.java:418)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:890)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1333)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:291)
    at ThreadedDataObjectHandler.run(ThreadedDataObjectServer.java:90)
    line 90: out.writeObject(myObject); // myObject is the serializable objectout is:
    ObjectOutputStream out=new ObjectOutputStream(incoming.getOutputStream()); any ideas on how to fix this??
    Thanks
    -rolo

    Exception says "java.io.IOException: failed to load image contents"
    most probably you are trying to send an image which has had an eror when loading the image.
    See the image load status of the image icon.

  • Binary file transfert over socket : data corupted !

    Hi everyone,
    I am trying to transfert binary files over Socket (java.net) and I get corrupted data... I know the data I write in the Socket is ok and I wonder why I don't get it right at the end.
    Does anyone know about it ?

    Ok i have re-written it without the Packet class and it know works...
    I give my code in case someone would be interested.
    ENCODER :
    public class Encoder {
         // the file to send
         private File file;
          * Constructor of the Encoder class
          * @param path the path to the file to send
         public Encoder(String path){
              this.file = new File(path);
              this.encodeFile();
          * This method contains the connection an file tranfert code
         private void encodeFile(){
              try {
                   // opening file reading stream
                   FileInputStream fis = new FileInputStream(this.file);
                   // connection...
                   Socket sock = new Socket("127.0.0.1", 5698);
                   // opening an output stream
                   BufferedOutputStream bos = new BufferedOutputStream(sock.getOutputStream(), 1024);
                   // start time
                   long start = System.currentTimeMillis();
                   // setting up the buffer
                   byte[] buffer = new byte[1024];
                   /* ---- File Transfert Loop ---- */
                   // reading for the first time
                   int read = fis.read(buffer);
                   while (read > 0){
                        bos.write(buffer);
                        bos.flush();
                        read = fis.read(buffer);
                   /* ----End Of File Transfert ---- */
                   // end time
                   long end = System.currentTimeMillis();
                   // closing streams and connection
                   fis.close();
                   bos.close();
                   sock.close();
                   // display file transfert duration
                   System.out.println("Completed in :" + (end - start) + " ms !");
              } catch (IOException e) {
                   e.printStackTrace();
    DECODER :
    public class Decoder {
         private File destFile;
         public Decoder(String path){
              try {
                   // setting up destination file
                   this.destFile = new File(path);
                   // setting up file writting stream
                   FileOutputStream fos = new FileOutputStream(this.destFile);
                   // setting up connection server
                   ServerSocket serv = new ServerSocket(5698);
                   // accepting client connection request
                   Socket sock = serv.accept();
                   // setting up reading stream for the connection
                   BufferedInputStream bis = new BufferedInputStream(sock.getInputStream());
                   // setting up byte buffer
                   byte[] buffer = new byte[1024];
                   // first reading
                   int read = bis.read(buffer);
                   while (read != -1){
                        // writting buffer content into file
                        for (int i=0; i < read; i++){
                             fos.write(buffer);
                        // reading next bytes
                        read = bis.read(buffer);
                   //closing streams
                   fos.close();
                   bis.close();
              } catch (IOException e) {
                   e.printStackTrace();

  • Labview connect over sockets with php

    Hello, i´ve a simply led.
    I would control value of led over sockets using PHP programming language. My question is, where are documentation and examples? I found many examples between 2 or more Labview applications but not in PHP. I don´t found documentation that explins information to send to Labview application from PHP sockets...
    It´s possible? 
    Thanks!

    Hello, it work! I changed the number of bytes that TCP READ read. When string is equals "a", it set led to true:
    PHP Code:
             $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            socket_bind($socket, "127.0.0.1");
            socket_connect($socket, "127.0.0.1", 4005);
            if(strcmp($data,"")==0){
                $msg = "Default msg";
            } else {
                $msg = $data;
            $size = strlen($msg);
            $sock_data = socket_write($socket, $msg, strlen($msg));
            $res = "Data sended ($size) with response $sock_data.";
    But now, a get this error when TCP close:
    But i don´t understand because i´ve defined my IP in VI Server:Machine Access .
    Any idea?

  • Serialization over sockets

    Hi All!
    So i've decided to perform serialization over sockets - so i made a server that has an ObjectOutputStream and a client that has ObjectInputStream and i wrote a Properties object.
    I managed to get passed the EOFException problem and the same object writing problem (using reset()), but i've read that this isn't efficient, and on top it all it has the problem of blocking.
    I've read some posts regarding Serialization and NIO but i couldn't get from them what i should do - so can someone plz explain to me:
    1) How can i write objects to sockets using NIO
    2) Furthermore - if this really solves the memory and efficiency problems
    3) A code sample or a ref. would really be appriciated.
    Regards,
    Snayit

    Basically I would ask myself if I really need NIO - for MOST purposes the old-blocking IO is very good and much easier to use.
    One of the biggest problems with blocking IO is, that for thousands of clients you need an own thread which causes many OSes to truggle.
    I really recommend buffering the streams! (Via BufferedInput/OutputStream) but do not forget to flush() them if you really want the data to be sent.
    If you want to use NIO you have to write the object-data into a byte-array (creating a bytearrayoutputstream -> ObjectOutputStream) and writing this byte-arrays.
    hope this helps, lg Clemens

  • Jdb fails to connect to running java application over sockets

    I'm trying to use jdb to connect to a running instance of a java program.
    If I use dt_shmem:
    java -agentlib:jdwp=transport=dt_shmem,address=shmem,server=y,suspend=n JDBTest
    jdb -attach shmemSet uncaught java.lang.Throwable
    Set deferred uncaught java.lang.Throwable
    Initializing jdb ...
    quitworks fine.
    If I use dt_socket, jdb crashes:
    java -agentlib:jdwp=transport=dt_socket,address=9000,server=y,suspend=n JDBTestP:\workspace\JDBTest>jdb -attach 9000
    java.io.IOException: shmemBase_attach failed: The system cannot find the file specified
    at com.sun.tools.jdi.SharedMemoryTransportService.attach0(Native Method)
    at com.sun.tools.jdi.SharedMemoryTransportService.attach(SharedMemoryTra
    nsportService.java:90)
    at com.sun.tools.jdi.GenericAttachingConnector.attach(GenericAttachingCo
    nnector.java:98)
    at com.sun.tools.jdi.SharedMemoryAttachingConnector.attach(SharedMemoryA
    ttachingConnector.java:45)
    at com.sun.tools.example.debug.tty.VMConnection.attachTarget(VMConnectio
    n.java:358)
    at com.sun.tools.example.debug.tty.VMConnection.open(VMConnection.java:1
    68)
    at com.sun.tools.example.debug.tty.Env.init(Env.java:63)
    at com.sun.tools.example.debug.tty.TTY.main(TTY.java:964)
    Fatal error:
    Unable to attach to target VM.
    It looks like jdb is trying to connect over shared memory instead of using sockets.
    Trying jdb -attach localhost:9000 has the same effect.
    I can however connect to the running Java program using the Eclipse debugger using a remote configuration.
    What can I do that jdb will use sockets?
    I'm using Windows 2000 SP4 and Java 1.5.0_07.
    Many thanks in advance
    Thomas

    jdb -help says:
    -attach <address>
    attach to a running VM at the specified address using standard connector
    On Windows the 'standard', ie. default, connector uses the shared memory transport.
    To connect to a socket, you need to tell jdb to use the socket attaching connector, ie
    jdb -connect com.sun.jdi.SocketAttach:port=9000
    It is hard to remember the names and params of all the connectors. You can do
    jdb -listconnectors
    to refresh your memory.
    HTH.

  • String Data-Loss over Sockets

    I have some code that sends strings through a socket to a C++ program on another machine.
    The test strings I have been using are three characters long (i.e. 6 bytes). The buffer size I have put on the socket is 72 bytes, so I should have no problem writing these strings.
    The first three strings always arrive at the other program fine, BUT after that, only a substring of each string seems to get through. I am absolutely certain that the strings are complete before I send them, because I print it to the screen at the same time.
    I have tried change the style of output stream I use (I have used DataOutputStream, BufferedOutputStream, BufferedWriter, StringWriter, and PrintWriter. It still happens.
    Can anybody tell me why this might happen?
    - Adam

    Without more info it is hard guessing. If you want
    reassurance that it should work, then yes, it should
    work.-(Well that's kinda what I'm looking for. I'm wondering if anybody knows of a reason why a C++ program (running in windows -- btw, not by my choice) would read the string differently than a java program?
    For all the XxxWriter types you used, I hope you
    declared the charset to be one that preserves the
    2bytes/char you expect (UTF-8 and ISO8859-1 aren't).I haven't modified that from whatever default is set. This may be the probelm, I will look into that, thanks.
    You certainly did not use the BufferedOutputStream
    without somehow encoding the characters into bytes,
    so how did you do? I'm not sure (it was last week that I tried it) but I think the BufferedOutputStream has a method writeBytes(String) that I used.
    For DataOutputStream, I hope you
    used writeChar, which guarantees that 2bytes/char are
    send. Nope. I mostly tried to stick with methods that accept a String, so that I was sure that it was sent out in the right format to be read back in as a String. I wasn't sure what the actually format of a String is, when passed through a socket, specifically, if there is a terminating character (I know C uses a \0 to end the string). Is there any additionl info need to write a string to a socket?
    If you did all this, ... well I would not
    fiddle with the socket's buffer size.Sorry, but I may have to maintain a low buffer size, because these strings are not the only things being sent over this socket. Do you think the buffer size is affecting the problem. I wondered, but the buffer size seems more than large enough to send 3 character strings.
    That's all that comes to mind. Did you try netcat
    (aka nc) as a server to make sure the problem is not
    at the receiving end?I haven't tried this yet, but I will if I can't figure this out. Unfortunately, I'm NOT the author of the code that recieves the data, and the guy who is has simply assumed that the problem is my fault (although he's never actually tested his code with the string data being sent before), and is not interested in checking to make sure he's done it right. I tried looking over his code, but he's got the input routine burried in an #include file that I don't have.
    Thanks for the input, Harald. There are a few things there that I will look into.
    - Adam

  • Gzip only specific files extensions over sockets

    I am writing a socket webserver class that gzips only HTML files back to the browser (client). If the HTML file contains any images, I will have to send that over uncompressed. I am using GZIPOutputStream for my HTML objects, and a regular DataOutputStream for my other objects. None of my images show up correctly when I use DataOutputStream alone, but when I also gzip them, they show up fine. Also, all the images/html files show up fine if I exclude all the gzipping altogether. So it seems as though I have to either gzip them all ... or none at all.
    Is there a way to gzip only certain file extensions?
    Thanks!

    None of my images show up correctly when I use DataOutputStream alone, but when I also gzip them, they show up fine.Perhaps you aren't calling flush on the DataOutputStream (but the GZIP stream is) ?
    So it seems as though I have to either gzip them all ... or none at all. Probably not, I believe there is just an error in your code.
    Is there a way to gzip only certain file extensions?Sure,
    if (fileShouldBeGzipped(file.getName()))
      sendGzipFile(file);
    else
      sendFile(file);Of course, you must provide the fileShouldBeGzipped logic.
    Are you really implementing a webserver? That is, is it using http? If so, the standard practice (protocol) is that a client adds a header field which states what type of content encoding it can deal with in the response (i.e. gzipped content). If the server is capable, it replies with its own header stating the content is indeed gzipped along with the gzipped content. This way the client knows that it must decompress the data before doing something with it.

  • HTTP 1.1: Using streams over socket; How ?

    Can anyone give me a short example as how to handle streams over a persistent socket-connection ?
    I've trouble handling this.
    I can't "reget" the stream (create a new input/output stream over an existing socket-connection).
    Is there someone who can help me ?
    I'm getting confused !!!

    Since no one helped I could only try to find out myself:
    The problem is I used some example-source that fitted well for HTTP 1.0, but led to erroneous results when applied to HTTP 1.1
    It's no problem in HTTP 1.1, but you need to read ALL input (headers and possibly content) before trying to process anything:
    From the headers you need to read until an empty line is discovered.... Don't just break on return or newline, this will leave an extra character in the input:
    Protocol requires both \r and \n. So you always break 1 char to early when scanning bytes and then concluding on a \r that the line is over.
    Then you should possibly read 'contentlength bytes' (I've not tried it out yet) in case of POST.
    Flushing is enough to write all data to output.
    Open and close can't work. "Regetting" a stream is not possible, so all input for this request must be taken from inputstream before trying to process the next request.

Maybe you are looking for

  • How to fix order value not to exceed a specified amount in a Purchase Order

    Can you please let me know the config steps required to fix a certain amount in a purchase order ( I mean to say one designated General Manager Purchase has got delegation of powers to issue purchase order for an amount of Rs. 10 lacs only).  Is it p

  • Can you Batch convert files to pdf's Acrobat 9 standard

    i am looking through Acrobat 9 standard and can not see any batch conversion to convert all the files in a folder to PDF's not (combining them) is this something available in other versions pro or extended? or am i just doing it wrong? may a script o

  • Using an iMac in a Motorcaravan/RV

    I'm currently working on a self built recreational vehicle (Motorcaravan) and am seriously considering using a 20" iMac as an entertainment center while on the road. I have a few (quite technical) questions that I haven't been able to find any answer

  • SmartForms & Smartstyles

    My smartforms work fine when I use Courier-font for styling! But when I use any other font-family, it is printing junk character! Please let me know what could be the reason! Thanks in advance! Message was edited by: Sam

  • MICROSOFT Does it again

    The best and most trustful Web information is PCWorld. Thanks for such a great review and information on Windows 8.1. Thanks Microsoft and all the help Microsoft received with windows 8,  including MVP. Thanks Pcworld it is long due that we are final