Reputation: 33
I'm trying to just bind a socket with this :
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
int main()
{
int fd,namelen,newfd;
struct sockaddr_in sin = {AF_INET};
if(fd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)==-1)
printf("socket : %d\n",errno);
if(bind(fd,(struct sockaddr *)&sin,sizeof(sin))==-1)
printf("bind : %d\n",errno);
}
And that return "bind : 88", I think this mean ENOTSOCK fd, isn't a socket really ? or 88 isn't ENOTSOCK ?
Upvotes: 3
Views: 1567
Reputation: 11942
Take care about parenthesis, in fact fd = 0 in your case.
Because ==
is evaluated before =
(see C Operator Precedence), your code is equivalent to fd = (socket(...) == -1)
.
You should replace
if(fd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)==-1)
with
if((fd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
Upvotes: 4