Note that there are some explanatory texts on larger screens.

plurals
  1. POsocket connect() returning 0 even after peer reset
    primarykey
    data
    text
    <p>I have an iPad app in which I'm creating and connecting a posix <code>socket</code>. Calls to <code>connect(...)</code> are not responding as expected when the remote device rejects the connection attempt.</p> <p>A simplified example of how I'm creating and connecting the <code>socket</code>:</p> <pre><code>struct sockaddr_in server; server.sin_family = AF_INET; int socket = socket(AF_INET, SOCK_STREAM, 0); if(socket &lt; 0) { NSLog(@"socket create failed: %s", strerror(errno)); } else if(inet_aton(mAddress, &amp;server.sin_addr)) { server.sin_port = htons(mPortNumber); if(connect(socket, (struct sockaddr *)&amp;server, sizeof(server)) == -1) { NSLog(@"connect() failed: %s", strerror(errno)); } else { NSLog(@"connect() succeeded"); } } </code></pre> <p>In most cases, the above example works: e.g. for a remote device (hereafter referred to as 'the server') which is accepting connections, <code>connect()</code> returns <code>0</code> as expected. For a non-existent address, <code>connect()</code> blocks for a while then returns <code>-1</code>, and <code>errno</code> is <code>ETIMEDOUT</code>.</p> <p>From the docs, if the server rejects the connection attempt I would expect <code>connect()</code> to return <code>-1</code> and for <code>errno</code> to be <code>ECONNREFUSED</code> or perhaps <code>ECONNRESET</code>. Instead, <code>connect()</code> returns <code>0</code>.</p> <p>I've WireSharked the TCP conversation multiple times and can confirm that only ever consists of two packets:</p> <blockquote> <p>Client (iPad) -> Server: <code>SYN, ACK</code></p> <p>Server -> Client (iPad) : <code>RST, ACK</code></p> </blockquote> <p><strong>Why does connect return <code>0</code>?</strong></p> <p>Update: Connecting to the same server from a different client, e.g. Qt on Windows (winsock?) or Mac OSX (also posix) works as expected: i.e <code>connect()</code> returns <code>-1</code>, <code>ECONNREFUSED</code>. This leads me to believe it's a problem with the iPad end rather than the server end.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload