Question

Hey guys i have been trying to make a NIO server/client program. My problem is that the server only sends 14 bytes then it won't send anything more. I've sat so long with this that i really can't see anything anymore and therefor decided to see if anyone here can see the problem

Server Code:

import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;

public class Testserver {
    public static void main(String[] args) throws java.io.IOException {

        ServerSocketChannel server = ServerSocketChannel.open();
        server.socket().bind(new java.net.InetSocketAddress(8888));

        for(;;) { 
            // Wait for a client to connect
            SocketChannel client = server.accept();  

            String file = ("C:\\ftp\\pic.png");


            ByteBuffer buf = ByteBuffer.allocate(1024);
            while(client.read(buf) != -1){

            buf.clear();
            buf.put(file.getBytes());
            buf.flip();

            client.write(buf);
            // Disconnect from the client
            }
            client.close();
        }
    }
}

Client:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
public class Testclient {

    public static void main(String[] args) throws IOException {



        SocketChannel socket = SocketChannel.open(new InetSocketAddress(8888));



        FileOutputStream out = new FileOutputStream("C:\\taemot\\picNIO.png");
        FileChannel file = out.getChannel();

        ByteBuffer buffer = ByteBuffer.allocateDirect(8192);

        while(socket.read(buffer) != -1) { 
          buffer.flip();       
          file.write(buffer); 
          buffer.compact();    
          buffer.clear();
        }
        socket.close();        
        file.close();         
        out.close();           
    }
}
Was it helpful?

Solution

  1. You're only sending the filename, not the file contents. The filename is 14 bytes.

  2. You're sending that every time the client sends anything. However the client is never sending anything, so I'm surprised you're even getting 14 bytes.

  3. You're ignoring the result of write().

If you want to send the file contents, open it with a FileChannel. Similarly, open the target file in the client with a FileChannel. Then the copy loop is the same in both places. The correct way to copy a buffer between channels is as follows:

while (in.read(buffer) >= 0 || buffer.position() > 0)
{
  buffer.flip();
  out.write(buffer);
  buffer.compact();
}

Note that this takes care of partial reads, partial writes, and the final writes after reading EOS.

Having said all that, I don't see any reason to use NIO here at all. It would all be much simpler with java.net sockets and java.io streams.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top