Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

SocketConduit connect close -> bad file descriptor

Moderators: kris

Posted: 01/26/09 14:39:11

I want to make a few socket requests and therefore adapted the example from the wiki as following:

private import  tango.io.Console;

private import  tango.net.SocketConduit, 
                tango.net.InternetAddress;

void main()
{
        // make a connection request to the server
        auto request = new SocketConduit;
        
        for (int i=0; i<1000; i++) {
            request.connect (new InternetAddress ("localhost", 8080));
            request.output.write ("hello\n");
    
            // wait for response (there is an optional timeout supported)
            char[64] response;
            size_t len = request.input.read (response);
            // trim the length of the response
            auto received = response[0..len];
            
            // close socket
            request.close;
    
            // display server response
            Cout (received).newline;
        }
}

But I always get a bad file descriptor exception:

tango.core.Exception.SocketException?: Unable to connect socket: Bad file descriptor

I tried request.shutdown and request.detach as well, but with no luck.

Do I really need to put

auto request = new SocketConduit;

within the loop?

/lars

Author Message

Posted: 01/27/09 04:32:47 -- Modified: 01/27/09 04:33:40 by
kris

That's a good question

I'm not entirely sure whether you can call connect() multiple times on a raw BSD-style socket or not. There's no 'disconnect' network function that I'm aware of, either.

That aside, closing the SocketConduit in the illustrated manner will close the raw socket too, which likely would account for the error message you noted. Internally, for server/accept sockets and the like, Tango does reuse the wrapper object appropriately. That functionality is not explicitly exposed though. What happens if you call connect multiple times without closing the conduit? Perhaps with a shutdown() call between them?

Posted: 01/27/09 16:45:22

If I call connect() before closing the connection I get:

Unable to connect socket: Transport endpoint is already connected

which seems reasonable. The same happens when shutdown is called before connect.

/lars

Posted: 01/27/09 17:13:46

Yep, makes sense to me.

So, in your example case, you'd like a 'disconnect' function in order to reuse the socket for another address? Or, is it that you would prefer to re-assign fresh socket handles to an existing SocketConduit wrapper?

Posted: 01/28/09 13:49:35

I just want to write more then once on a socket and read the according response. I figured out that this should be possible without closing the connection. I modified the code so that it creates the socket and connect on startup and just writes and reads within the loop. The problem is that the loop is running only three times before the program is terminated.

private import  tango.io.Console;

private import  tango.net.SocketConduit, 
                tango.net.InternetAddress;

void main()
{
        // make a connection request to the server
        auto request = new SocketConduit;
        
        request.connect (new InternetAddress ("localhost", 8080));

        for (int i=0; i<1000; i++) {
            
            request.output.write ("hello\n");
    
            // wait for response (there is an optional timeout supported)
            char[64] response;
            size_t len = request.input.read (response);
            // trim the length of the response
            auto received = response[0..len];
                        
            // display server response
            Cout (received).newline;
        }

        // close socket
        request.close;
    
}

Can someone explain that to me. Is it because a buffer is growing to large?

/lars

Posted: 01/29/09 02:49:12

ok, that's quite different from the first example (which was reconnecting the socket each time around the loop). The second example should operate just fine ... there is no buffering, except perhaps at the O/S level, and it should happily read whatever is sent back from the server.

You noted that the program is terminated, but didn't convey why or what any exception may have indicated. Can you provide that information please? For example, does it timeout while waiting for a server response?