Note that there are some explanatory texts on larger screens.

plurals
  1. POSocket operation on non-socket, Client server program in C
    primarykey
    data
    text
    <p>I'm new to socket programming and I am trying to write a simple socket that connects to another socket on my PC (<code>nc -l 35353</code>)</p> <p>I keep getting a error when trying to bind the socket and I don't know how to debug it.</p> <hr> <p><strong>UPDATE:</strong> The socket call is returning 0 as a file descriptor, although the man page does not say this is illegal, I thought unix/linux reserve fd 0, 1 and 2 for stdin, stdout and stderr by default. I am not sure if this has anything to do with the bind error I am seeing, I just felt this might be appropriate.</p> <p>Here is the code</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #include&lt;string.h&gt; #include&lt;netinet/in.h&gt; #include&lt;sys/types.h&gt; #include&lt;sys/socket.h&gt; //typedef struct sockaddr_in sockaddr_in; int main() { int sock_fd; if( sock_fd = socket(AF_INET, SOCK_STREAM, 0) &lt; 0) { perror("Socket Creation error!\n"); return 1; } struct sockaddr_in myaddr; memset((char*)&amp;myaddr, 0, sizeof(myaddr)); myaddr.sin_family = AF_INET; uint32_t myip = (127&lt;&lt;24)|(0&lt;&lt;16)|(0&lt;&lt;8)|1; myaddr.sin_addr.s_addr = htonl(myip); myaddr.sin_port = htons(1337); int binderror = bind(sock_fd, (struct sockaddr*)&amp;myaddr, sizeof(myaddr)); printf("bind error %d\n",binderror); if( binderror &lt; 0) { perror("Bind Error!\n"); return 1; } struct sockaddr_in serveraddr; memset((char*)&amp;serveraddr, 0, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons(35353); //unsigned char serverip[] = {127,0,0,1}; uint32_t serverip = (127&lt;&lt;24)|(0&lt;&lt;16)|(0&lt;&lt;8)|1; serveraddr.sin_addr.s_addr = htonl(serverip); if( connect(sock_fd, (struct sockaddr*)&amp;serveraddr, sizeof(serveraddr)) &lt; 0 ){ perror("Could not connect\n"); return 0; } } </code></pre>
    singulars
    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