This one puzzled me for a bit. I opened a socket, bound it to a local interface alias address, called connect to a remote machine. Everything was fine. No errors, errno = 0. Then I called write on the socket and it says: "broken pipe". It seems that the cause was that I forgot to set the family for the call to connect:
struct sockaddr_in servaddr; memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;servaddr.sin_port = htons( atoi(serv) ); if ( !inet_pton(AF_INET, host, &servaddr.sin_addr) ) { printf("inet_pton failed\n"); rc = -1; } else if ( connect(fd,(const struct sockaddr *)&servaddr, sizeof(servaddr)) == 0 ) { printf("Connected\n"); // return connected socket rc = fd; }
So I thought I'd file this one for reference, in case I get it again. It's amazing how a simple typo can get you into so much trouble.
No comments:
Post a Comment