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

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -