简体   繁体   中英

Creating a connection using grpc between a python server and an android(java) client

I am running a simple GRPC server on python on my local machine. When I'm trying to connect to it, using java, from my android device, i keep getting the Caused by: io.grpc.StatusRuntimeException: UNAVAILABLE error. Note that i tried to connect to the server, via a python client and it worked as expected. The problem only stands when using a java client.

I have tried to use a client in python to check whether the proto files where the problem, and it worked correctly, so i believe the problem is with the connection between the python server and java client combination.

    private ManagedChannel mChannel;
    private TestGrpc.TestBlockingStub blockingStub;
    private TestGrpc.TestStub asyncStub;

    mChannel = ManagedChannelBuilder.forAddress("10.0.0.17", 50051).build();
    blockingStub = TestGrpc.newBlockingStub(mChannel);
    helloMessage testMessage = helloMessage.newBuilder()
    .setMessageContent("NAME")
    .build();
    helloMessage msg= blockingStub.sayHello(testMessage);

proto file:

syntax="proto3";
option java_package = "io.grpc.testing";
option java_multiple_files = true;
option java_outer_classname = "TestClass";
option objc_class_prefix = "TST ";

package TestCode;

service Test{
    rpc sayHello(helloMessage) returns (helloMessage) {}
    rpc streamTest(helloMessage) returns (stream helloMessage) {}
}

message helloMessage{
    string messageContent = 1;
}

python server

import protofile_pb2
import protofile_pb2_grpc


# create a class to define the server functions, derived from
# calculator_pb2_grpc.CalculatorServicer
class TestService(protofile_pb2_grpc.TestServicer):

    # calculator.square_root is exposed here
    # the request and response are of the data type
    # calculator_pb2.Number
    def sayHello(self, request, context):
        response = protofile_pb2.helloMessage()
        response.messageContent = "hello mister "+request.messageContent
        return response
# create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

# use the generated function `add_CalculatorServicer_to_server`
# to add the defined class to the server
protofile_pb2_grpc.add_TestServicer_to_server(
        TestService(), server)

# listen on port 50051
print('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()

# since server.start() will not block,
# a sleep-loop is added to keep alive
try:
    while True:
        time.sleep(86400)
except KeyboardInterrupt:
    server.stop(0)

This should return an iterator with one value, being the string: "hello mister NAME". actual result: Caused by:io.grpc.StatusRuntimeException: UNAVAILABLE

I found the solution to this problem was that i was using an insecure port for the communication and using SSL for the connection from android which expected handshaking. You can solve this by creating server certificates and using secure channel communications or changing your change to build with.Plaintext()

https://grpc.io/docs/guides/auth/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM