I using the following environment. 

Windows 7 64 bit.
Java : jdk1.8.0_131

protobuf-gradle-plugin:0.8.1

protoc:3.3.0

protoc-gen-grpc-java:1.4.0



But while running the project I get response for first call only and on server 
side I obtain following error after that.


io.grpc.netty.NettyServerTransport notifyTerminated
SEVERE: Transport failed
java.io.IOException: An existing connection was forcibly closed by the remote 
host
        at sun.nio.ch.SocketDispatcher.read0(Native Method)
        at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
        at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
        at sun.nio.ch.IOUtil.read(IOUtil.java:192)
        at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
        at 
io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)
        at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100)
        at 
io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:372)
        at 
io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:123)
        at 
io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:644)
        at 
io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:579)
        at 
io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:496)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:458)
        at 
io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
        at 
io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:138)
        at java.lang.Thread.run(Thread.java:748)


Can any one suggest me the solution why this is happening? 

-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/5b9d9f5d-e49c-4c13-bf0e-99746f49ed19%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Attachment: user_info.proto
Description: Binary data

package np.com.keshavbist.service;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.concurrent.TimeUnit;
import np.com.keshavbist.user.*;



/**
 * Created by kbist on 6/30/2017.
 */
public class UserServiceClient {
    public static void main(String[] args) throws Exception {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8090)
                .usePlaintext(true)
                .build();

        UserServiceGrpc.UserServiceBlockingStub blockingStub = UserServiceGrpc.newBlockingStub(channel);

        AuthRequest request = AuthRequest.newBuilder().setPassword("HELLO").setUserName("kbist").build();

        System.out.println("Requesting access ..." + request);

        AuthResponse response = blockingStub.authenticate(request);

        System.out.println(response + "\n Asking for User info");

        UserRequest userRequest = UserRequest.newBuilder().setUser("ksv")
                .setAuthvalue(response)
                .build();

        UserResponse user = blockingStub.userInfo(userRequest);
        System.out.println(user);

        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    }
}
package np.com.keshavbist.service;

import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.io.IOException;
import java.util.Scanner;

/**
 * Created by kbist on 6/30/2017.
 */
public class UserServiceServer {
    Server server;
    UserServiceServer() throws Exception {
         server = ServerBuilder.forPort(8090).addService(new UserServiceImpl()).build();
         server.start();
         server.awaitTermination();
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.err.println("*** JVM is shutting down. Turning off grpc server as well ***");
            UserServiceServer.this.stop();
            System.err.println("*** shutdown complete ***");
        }));
    }

    private void stop() {
        if (server != null) {
            server.shutdown();
        }
    }
    public static void main(String[] args) throws Exception {
        new UserServiceServer();
    }
}

Attachment: build.gradle
Description: Binary data

package np.com.keshavbist.service;

import io.grpc.stub.StreamObserver;
import np.com.keshavbist.user.*;

import java.util.Random;

/**
 * Created by kbist on 6/30/2017.
 */
public class UserServiceImpl extends UserServiceGrpc.UserServiceImplBase {
    private static String token;

    @Override
    public void authenticate(AuthRequest request, StreamObserver<AuthResponse> authResponseObserver){
        String userName = request.getUserName();
        String password = request.getPassword();

        AuthResponse response;
        this.token = new Double (new Random(12342).nextLong()).toString();
        if(userName.equals("kbist") && password.equals("HELLO")){
            response = AuthResponse.newBuilder().setLogged(true)
                    .setMessage("You logged in successfully")
                    .setToken(this.token)
                    .build();
        }else{
            response = AuthResponse.newBuilder().setLogged(false)
                    .setMessage("Incorrect Credentials")
                    .setToken("")
                    .build();
        }

        authResponseObserver.onNext(response);
        authResponseObserver.onCompleted();
    }

    @Override
    public void userInfo(UserRequest request, StreamObserver<UserResponse> responseObserver){
        UserResponse response;
        boolean loggedin = request.getAuthvalue().getLogged();
        String token = request.getAuthvalue().getToken();

        System.out.println(token + "   " + loggedin);

        if (loggedin){
            response = UserResponse.newBuilder().setAddress("KTM")
                    .setAuthinfo(request.getAuthvalue())
                    .setEmail("[email protected]")
                    .setName("Keshav Bist")
                    .setUser("kbist")
                    .build();
        }else{
            response = UserResponse.newBuilder()
                    .setAuthinfo(request.getAuthvalue())
                    .build();
        }

        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}

Reply via email to