Hi, everyone:
    The ClientActor want to use Tcp.Write send a file to the ServerActor, 
but the server cannot received the data.I use the debug to watch the 
received message, the ServerActor receive message is 
"ErrorClosed(Connection reset by peer)".So How to solve this problem?

    Can someone give me some advises? Thank you for any reply!
    
    ServerActor:

public class ServerActor extends UntypedActor {

    private ActorRef tcpActor;
    @Override
    public void preStart() throws Exception {

        if (tcpActor == null) {

            tcpActor = Tcp.get(getContext().system()).manager();

        }

        tcpActor.tell(TcpMessage.bind(getSelf(),

                new InetSocketAddress("localhost", 9090), 100), getSelf());

    }
    public static Props props(ActorRef tcpActor) {

        return Props.create(ServerActor.class, tcpActor);

    }



    public ServerActor(ActorRef tcpActor) {

        this.tcpActor = tcpActor;

    }
    @Override

    public void onReceive(Object message) throws Throwable {

        if (message instanceof Tcp.Bound) {

            System.out.println("In ServerActor - received message: bound");

        } else if (message instanceof Tcp.CommandFailed) {

            getContext().stop(getSelf());

        } else if (message instanceof Tcp.Connected) {

            final Tcp.Connected conn = (Tcp.Connected) message;

            System.out.println("In ServerActor - received message: 
connected");

            final ActorRef handler = getContext().actorOf(

                    Props.create(SimplisticHandlerActor.class));

            getSender().tell(TcpMessage.register(handler), getSelf());

        }

    }

}

SimplisticHandlerActor:


public class SimplisticHandlerActor extends UntypedActor {
    @Override
    public void onReceive(Object msg) throws Throwable {

        if (msg instanceof Tcp.Received) {

            RandomAccessFile raf = null;

            ByteBuffer bs = ((Tcp.Received) msg).data().toByteBuffer();

            File srcFile = new File("testData/spill_out/test" + ".txt");

            try {

                raf = new RandomAccessFile(srcFile, "rw");

            } catch (FileNotFoundException e) {

                e.printStackTrace();

            }

            FileChannel fileChannel = raf.getChannel();

            fileChannel.write(bs);

            fileChannel.close();

            raf.close();

            bs.clear();

        } else if (msg instanceof Tcp.ConnectionClosed) {

            getContext().stop(getSelf());

        }

    }

}


ClientActor:

public class ClientActor extends UntypedActor {

    private ActorRef tcpActor;

    final InetSocketAddress remote;

    public static Props props(InetSocketAddress remote, ActorRef tcpActor) {

        return Props.create(ClientActor.class, remote, tcpActor);

    }

    public ClientActor(InetSocketAddress remote, ActorRef tcpActor) {

        this.remote = remote;

        this.tcpActor = tcpActor;

        if (tcpActor == null) {

            tcpActor = Tcp.get(getContext().system()).manager();

        }

        tcpActor.tell(TcpMessage.connect(remote), getSelf());

    }

    @Override

    public void onReceive(Object message) throws Throwable {

        if (message instanceof Tcp.CommandFailed) {

            System.out.println("In ClientActor - received message: failed");

            getContext().stop(getSelf());

        } else if (message instanceof Tcp.Connected) {

            System.out.println("In ClientActor - received message: 
connected");

            getSender().tell(TcpMessage.register(getSelf()), getSelf());

            getContext().become(connected(getSender()));

            getSender().tell(new Tcp.WriteFile("testData/spill_out/test.txt"
,0,4,new Tcp.NoAck(null)),getSelf());

        }

    }

    private Procedure<Object> connected(final ActorRef connection) {

        return new Procedure<Object>() {

            @Override

            public void apply(Object msg) throws Exception {

                if (msg instanceof ByteString) {

                    connection.tell(TcpMessage.write((ByteString) msg), 
getSelf());

                } else if (msg instanceof Tcp.CommandFailed) {

                    // OS kernel socket buffer was full

                } else if (msg instanceof Tcp.Received) {

                    System.out.println("In ClientActor - Received message: " 
+ ((Tcp.Received) msg).data().utf8String());

                } else if (msg.equals("close")) {

                    connection.tell(TcpMessage.close(), getSelf());

                } else if (msg instanceof Tcp.ConnectionClosed) {

                    getContext().stop(getSelf());

                }

            }

        };

    }

}










-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to