Sorry for the late response.

The BuildAndStart() does not promise you that it would return a "nullptr" 
if the port you passed is already in use.

You need to use alternative ways to find an unused port yourself.  
You could do the check yourself by randomly selecting a port number, 
creating a test socket and attempting to bind to that port. If successful, 
close the test-socket and use that port number for your gprc server.

For example, in grpc unit tests (where we have to create many servers), we 
wrote a simple port-server utility 
(https://github.com/grpc/grpc/blob/master/tools/run_tests/python_utils/port_server.py)
 
that maintains a list of available ports (it find the available ports by 
using pretty much the same logic I described above)

Hope this helps,
-Sree


On Monday, November 13, 2017 at 7:23:23 AM UTC-8, tfzk wrote:
>
>
> I will use the ServerBuilder to create an service with one of a port of *my 
> defined port range* ( e.g. 50001 to 50100 )
> If one port is used I will use the next one.
>
> *Is this possible and how is this working? I will not check for free port 
> before.*
>
> I tried the following:
>
>    - instantiate the builder
>    - add completion queue
>    - add one of the port to builder -> AddListeningPort(L"0.0.0.0:50001
>    ", grpc::InsecureServerCredentials()
>    - instantiate new service
>    - register service
>    - and call BuildAndStart()
>    - if nullptr returned I try it again with an other port.
>
>
>
> *-> this is not working. An exception occured ( Unhandled exception at 
> 0x0f365dc4 (xxxx.dll) in xxxx.exe: 0xC0000005: Access violation reading 
> location 0xfeeefef2. )in method has_synchronous_methods() at for loop*
>   bool has_synchronous_methods() const {
>     for (auto it = methods_.begin(); it != methods_.end(); ++it) {
>       if (*it && (*it)->handler() != nullptr) {
>         return true;
>       }
>     }
>     return false;
>   }
>
>
>
>
> Here is my code (simplyfied):
>
> void MyService::Run( )
> {
>     ServerBuilder builder;
>     _completionQueue = builder.AddCompletionQueue();
>     
>     for( int portNumber = 50000; portNumber <= 50100; portNumber++ )
>     {
>         _runningService = std::make_shared<MyServiceBase>();
>         builder.AddListeningPort( _address + ":" + std::to_string( 
> portNumber ), grpc::InsecureServerCredentials(), &_port);
>         builder.RegisterService( _runningService.get() );
>
>         _server = builder.BuildAndStart();
>
>         if( !_server )
>             return;
>     }
>
>     HandleRpcs();
> }
>
>
> Thanks in advanced.
>
> Kind regards
>

-- 
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/eab8d76c-8a5f-4538-9bd9-395a2181ebd7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to