Network file transfer

metalstorm metalheadstorm at gmail.com
Tue Oct 11 04:01:25 EDT 2011


Right i've had a look, and god my head hurts lol.

ServerPipelineFactory and ClientPipelineFactory:

...
	public ChannelPipeline getPipeline() throws Exception
	{
		// Create a default pipeline implementation.
		ChannelPipeline pipeline = pipeline();

		// Add the text line codec combination first,
		// pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192,
		// Delimiters.lineDelimiter()));
		// pipeline.addLast("decoder", new StringDecoder());
		// pipeline.addLast("encoder", new StringEncoder());

		pipeline.addLast("executor", executionHandler);

		// and then business logic.
		pipeline.addLast("handler", new TimeServerHandler());

		return pipeline;
	}

TimeClienthandler:

	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws IOException
	{

		ChannelBuffer buf = (ChannelBuffer) e.getMessage();
		byte[] line;
		if (buf.hasArray())
		{
			line = buf.array();
		} else
		{
			line = new byte[buf.capacity()];
			buf.getBytes(0, line);
		}
		FileOutputStream out = new FileOutputStream("testFile.txt");
		out.write(line);

		// close the FileOutputStream once you are done
		out.close();

TimeServerHandler:

	@Override
	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent
e) throws IOException
	{

		Channel ch = e.getChannel();

		File myFile = new File("E:\\Downloads\\DummyFile.txt");
		if (myFile.exists())
		{

			System.out.println("got file: " + myFile.getName());
			FileInputStream in = new FileInputStream(myFile);

			int bytes = 0;
			byte[] buffer = new byte[8192];
			int len;

			while ((len = in.read(buffer)) > 0)
			{
				// ch.write(buffer, 0, len);
				ch.write(buffer);
				bytes += len;
			}
		}


Server throws this error:

got file: DummyFile.txt
11-Oct-2011 09:00:22 org.jboss.netty.channel.socket.nio.NioWorker
WARNING: Unexpected exception in the selector loop.
java.lang.IllegalArgumentException: unsupported message type: class [B
	at
org.jboss.netty.channel.socket.nio.SocketSendBufferPool.acquire(SocketSendBufferPool.java:55)
	at org.jboss.netty.channel.socket.nio.NioWorker.write0(NioWorker.java:461)
	at
org.jboss.netty.channel.socket.nio.NioWorker.writeFromTaskLoop(NioWorker.java:393)
	at
org.jboss.netty.channel.socket.nio.NioSocketChannel$WriteTask.run(NioSocketChannel.java:269)
	at
org.jboss.netty.channel.socket.nio.NioWorker.processWriteTaskQueue(NioWorker.java:268)
	at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:199)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
11-Oct-2011 09:00:23 org.jboss.netty.channel.socket.nio.NioWorker
WARNING: Unexpected exception in the selector loop.
java.lang.NullPointerException
	at org.jboss.netty.channel.socket.nio.NioWorker.write0(NioWorker.java:504)
	at
org.jboss.netty.channel.socket.nio.NioWorker.writeFromTaskLoop(NioWorker.java:393)
	at
org.jboss.netty.channel.socket.nio.NioSocketChannel$WriteTask.run(NioSocketChannel.java:269)
	at
org.jboss.netty.channel.socket.nio.NioWorker.processWriteTaskQueue(NioWorker.java:268)
	at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:199)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

Am i going about this the right way? please say i'm close :)

--
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Network-file-transfer-tp6875434p6879990.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list