c - Socket connect() always returns zero -


i'm writing basic c program on linux utilizes sockets. sake of simplicity, shortened code this:

#include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <stdio.h> int main(int argc, char const *argv[]) {     int sock, ret;     struct sockaddr_in sin;     sock = socket(pf_inet, sock_stream, 0);     printf("%d\n", sock);     ret = connect (sock, (struct sockaddr*)&sin, sizeof(sin));     printf("%d\n", ret);     return 0; } 

and output get:

3 0 

as can see, connect() returns 0 invalid sockaddr_in. returns 0 regardless of address passed it.

it working fine earlier, sudden not. logically, tells me problem local, can't figure out is.

it strange because problem connect() failing -1 though 1 thinks 'did right', rather connect 'working' (returning zero) when it's not supposed to.

please advise.

i running ubuntu 12.04 x64.

there's no guarantee uninitialized struct happen contain data considered invalid. fix bug. there no particular reason expect predictable behavior buggy program this. behavior can vary based on anything, including compile compile of same code.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -