Overview
Features
Download
Documentation
Community
Add-Ons & Services

Restrict EchoServer sample to single connection

Please post support and help requests here.

Restrict EchoServer sample to single connection

Postby hdetter » 22 Apr 2008, 11:12

Hi,

I'm trying to modify the EchoServer example in a way, that only one client is allowed to connect to the server. If the server has a single connection, no other connect should be possible, it must be denied.
Any help in how to accomplish this?

Thanks!

Hans
hdetter
 
Posts: 6
Joined: 22 Apr 2008, 09:03
Location: Other

Re: Restrict EchoServer sample to single connection

Postby alex » 22 Apr 2008, 13:50

> I'm trying to modify the EchoServer example in a way, that only one client is allowed to connect to the server. If the server has a single connection, no other connect should be possible, it must be denied.
> Any help in how to accomplish this?

As things currently stand, you can not do that from the sample code side. The way to it would be to extend ))SocketReactor(( interface with a member function that would give you the number of handlers.
Then, you would query the reactor in ))EchoServiceHandler(( and act appropriately depending on whether there are any active event handlers or not.

There may be specifics to your application that I am not aware of, but using reactor to achieve such functionality seems awkward. A logical approach would be to use TCPServer in conjunction with TCPServerParams::setMaxQueued().

HTH

Alex
alex
 
Posts: 1044
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Restrict EchoServer sample to single connection

Postby guenter » 22 Apr 2008, 14:35

I would also say that unless you're a big fan on the Reactor pattern, then using the SocketReactor for a server that serves only a single connection is a bit, um, inappropriate. For this case, even using a plain ServerSocket and calling acceptConnection() in a loop would probably do it.

If you absolutely must use the reactor, an option would be to create your own implementation of the SocketAcceptor. Alternatively, have the ServiceHandler's constructor unregister the SocketAcceptor and its destructor register the SocketAcceptor again. But that seems a bit hacky...
guenter
 
Posts: 1091
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Restrict EchoServer sample to single connection

Postby hdetter » 28 Apr 2008, 11:10

Thanks for the advice.
So basically I took the TimeServer example (Ubuntu 7.10), to follow Alex hint about setMaxQueued().

The code looks now like:

Code: Select all

// set-up a server socket
ServerSocket svs(port);

//Dedicated parameter for connection handling
TCPServerParams *srvParams=new TCPServerParams();
srvParams->setMaxQueued(1);

// set-up a TCPServer instance
TCPServer srv(new TimeServerConnectionFactory(format), svs,srvParams);
// start the TCPServer
srv.start();

// set-up a server socket
ServerSocket svs(port);
 

I also modified the run() method of class TimeServerConnection not to exit after one single timestamp, but output them in a loop.

The result is, that still I can connect from different clients at the same time and all of them get back the timestamp.

What is my problem?

Thanks again

Hans


hdetter
 
Posts: 6
Joined: 22 Apr 2008, 09:03
Location: Other

Re: Re: Restrict EchoServer sample to single connection

Postby alex » 28 Apr 2008, 12:27

> The result is, that still I can connect from different clients at the same time and all of them get back the timestamp.

> What is my problem?

How do you verify that you are connected "at the same time"? Are you sure your connections are not processed one at a time (i.e. serially)?

Also, at this point, I am not sure whether you want to allow sequential connections from one and only one client or allow one connection at any given time from multiple clients?

Alex
alex
 
Posts: 1044
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Re: Re: Restrict EchoServer sample to single connection

Postby hdetter » 28 Apr 2008, 12:46

The thing I want to achieve is: The server must not accept a new connection as long as it serves the one and only connection from a connected client. It must not matter, from where the client connection originates. The server must refuse any new connection, if one client is connected.

As written, I took the timeserver having the TCPServerParams set and the run() method of TimeServerConnection is looping and printing timestamps. From localhost I start telnet, connect to the server and get the timestamps printed in the telnet console, then I let it run and connect to server using another telnet session. And again I let me connect and also will printout timestamps. The same happens with a third telnet client connection. So in total I have three client and all get data from the server. But what I want is, that I can only establish the one and only connection. If this one and only connection gets closed, a new client connection should succeed.

Thanks!

Hans


hdetter
 
Posts: 6
Joined: 22 Apr 2008, 09:03
Location: Other

Re: Re: Re: Re: Restrict EchoServer sample to single connection

Postby alex » 28 Apr 2008, 13:51

> If this one and only connection gets closed, a new client connection should succeed.

Then my original advice clearly will not satisfy your requirements . You can leave the default number of queued connections as is and put the logic of whether to reply or not in in your ))TimeServerConnection(( with ))TimeServerConnectionFactory(( memorizing the currently connected client.

However, all that seems bit too high-level for your case. If you would like to refuse the connection earlier, then I have to agree with Guenter's advice - use ))ServerSocket(( and do your own logic. Look at Net/testsuite/src/EchoServer.cpp for example. If you will have custom code on the client side, then ))DialogSocket(( may come handy as well (see Net/testsuite/src/DialogSocketTest).

Alex
alex
 
Posts: 1044
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 2 guests

cron