Results 1 to 2 of 2
  1. #1
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default Sending bytes prematurley

    I have a server and client program, that needs to transmit a file. However, as soon as the client connects to the server, the server is sending all the bytes, before the client is reading.

    Server:
    Java Code:
                File myFile = new File(src_dir+prefix+ze+i+".lxs");
                    BufferedOutputStream out = new BufferedOutputStream( sock.getOutputStream() );
                    PrintWriter pw = new PrintWriter(out);
                    String str = prefix+ze+i+".lxs";
                    pw.println(str);
                    pw.flush();
                    pw.close();
                    FileInputStream fis = new FileInputStream(myFile);
    		BufferedInputStream bis = new BufferedInputStream(fis);
                    int id;
                    while ((id = bis.read()) != -1)
    		{
    			out.write(id); //the data to be sent
    			System.out.println(id);
    		}
         out.write(-1);
            out.flush();
            out.close();
    	bis.close();
            sock.close();
    Client:

    Java Code:
     Socket sock = new Socket(ip_fld.getText(),13267); // heres where the server is sending the bytes
    BufferedInputStream in = new BufferedInputStream( sock.getInputStream() );
    BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    String fs;
    fs = br.readLine(); // this is working however
    System.out.println(fs);
    FileOutputStream fos = new FileOutputStream(fs);
    BufferedOutputStream out = new BufferedOutputStream(fos);
    int i;
    System.out.println("Receiving data...");
    while ((i = in.read()) != -1)
    {
    	out.write(i);
    }
    out.flush();
    in.close();
    out.close();
    sock.close();

  2. #2
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    Ok, so it turns out the program was closing the connection prematurley, so i made it it recieves a "done" message, then close. However, the file transfer is only transfering some, the same amount everytime. The size is 2113, and less that half is being transfered, but the client is still in the recieve loop (no -1 terminate byte). Is the filesize 2113 to big? i am using a BufferedInputStream and a DataOutputStream on the server.

Similar Threads

  1. XOR operation on bytes
    By divyanshu023 in forum New To Java
    Replies: 1
    Last Post: 09-17-2009, 08:11 PM
  2. Sending unsigned bytes using DatagramPacket
    By sranil in forum Networking
    Replies: 2
    Last Post: 04-28-2009, 03:52 AM
  3. How to read first 9 bytes and write in...?
    By aRTx in forum New To Java
    Replies: 8
    Last Post: 03-26-2009, 03:54 PM
  4. midi bytes
    By willemjav in forum Advanced Java
    Replies: 77
    Last Post: 07-29-2008, 04:10 PM
  5. how to know the number of bytes
    By gabriel in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 06:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •