I'm trying to learn how to work with Java 7's updated NIO library, and I've never really bothered learning NIO before, so I'm having some trouble.

This test code I made to send a file over a socket using entirely NIO channels is doing really bizarre things. It ALWAYS hangs up and never finishes on the client side and sometimes causes Windows Explorer to go haywire and crash the computer completely.

If you could take a look at it and tell me what you think might wrong, that would be great.

Also, if you could show me some example code for completing this task in NIO, that would be very beneficial.

Thanks!
[CODE]
package bg.jdk7.io;
 
import static java.nio.file.StandardOpenOption.*;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
 
public class NetIO {
 
	static volatile long filesize;
 
	public static void main(String[] args) {
		new Thread(new Client()).start();
		try {
			ServerSocketChannel ssc = ServerSocketChannel.open();
			ssc.bind(new InetSocketAddress(5555));
			SocketChannel sc = ssc.accept();
			if(sc.isConnected()) {
				ByteBuffer buff = ByteBuffer.allocate(10240);
				Path fp = Paths.get(System.getProperty("user.home")+"\\Documents\\clip0025.avi");
				if(Files.exists(fp)) {
					FileChannel fc = (FileChannel) Files.newByteChannel(fp, StandardOpenOption.READ);
					long tot = Files.size(fp);
					long run = 0;
					int read = 0;
					int prog = 0;
					while((read = fc.read(buff))>0) {
						buff.rewind();
						sc.write(buff);
						run+=buff.position();
						int last = prog;
						prog = (int)(((double)run/tot)*100);
						if(prog !=last) {
							System.out.println(prog + "%");
						}
						buff.flip();
					}
					fc.close();
					System.out.println("Sending completed");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
	static class Client implements Runnable {
 
		public void run() {
			try {
				SocketChannel sc = SocketChannel.open();
				sc.connect(new InetSocketAddress("localhost",5555));
				if(sc.isConnected()) {
					Path dpf = Paths.get("\\NIO_TESTING\\");
					Path dp = Paths.get(dpf+"\\clip.avi");
					Files.createDirectories(dpf);
					FileChannel fc = (FileChannel)  Files.newByteChannel(dp, CREATE, WRITE, TRUNCATE_EXISTING);
					ByteBuffer buff = ByteBuffer.allocate(10240);
					int read;
					int total = 0;
					while((read = sc.read(buff))>0) {
						total+=read;
						buff.rewind();
						fc.write(buff);
						System.out.println(fc.size());
						buff.flip();
						if(total == filesize) System.out.println("File data received successfully...");
					}
					System.out.println("Completed successfully");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}[/CODE]