On Thursday, 1 May 2014 at 08:08:37 UTC, Tim wrote:
On Tuesday, 29 April 2014 at 17:19:41 UTC, Adam D. Ruppe wrote:
On Tuesday, 29 April 2014 at 17:16:33 UTC, Tim wrote:
Is there anything I'm doing wrong?

You should be using a blocking socket. With them, the operating system will put your thread on hold until a new connection comes in. Without them, it will endlessly loop doing absolutely nothing except checking if a new connection is there yet. Horribly, horribly inefficient.

Blocking sockets are now working as expected, but I've one problem. When I do the following in my server-accept-thread:

while (m_oSocket.isAlive)
{
        oSocketSet.reset();
        oSocketSet.add(m_oSocket);

nAcceptedSockets = Socket.select(oSocketSet, null, null, 25.msecs);

        if (nAcceptedSockets > 0)
        {
                // Do something...
        }

}

... and the following in my client:

void connect()
{
        m_oSocket = new TcpSocket(AddressFamily.INET);
        m_oSocket.connect(new InternetAddress("127.0.0.1", 12345));
}

The CPU usage is low as long as my connect is connected. When I disconnect the client using the following few lines:

void disconnect()
{
        m_oSocket.shutdown(SocketShutdown.BOTH);
        m_oSocket.close();
}

... the CPU usage goes up. I think that SocketShutdown.BOTH causes Socket.select to fail which results in an endless loop. Any suggestions how to handle that problem?

@Ali Çehreli: Thanks, didn't know that UFCS can also handle such constructs :)

Small correction: "The CPU usage is low as long as my client is connected. When I disconnect the client using the following few lines:"

Reply via email to