LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   socket error (https://www.linuxquestions.org/questions/programming-9/socket-error-143205/)

adoyee 02-06-2004 10:37 PM

socket error
 
part of codes£º
.
.
.
if (sockfd=socket(PF_INET,SOCK_STREAM,0)<0){
Die ("server socket");
}
memest(&my_addr,0,sizeof(my_addr));
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(MYPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;

if ((bind(socfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)))<0){
Die ("Bind server socket");
}
.
.
.

When running the program,exit:

Bind server socket: Socket operation on non-socket.

-------------
the value of the sockfd is 0 !
:confused:

schmack 02-07-2004 12:08 AM

The problem is in your attempt to make the assignment and test in the if statement.

if (sockfd=socket(PF_INET,SOCK_STREAM,0)<0){
Die ("server socket");
}

The = binds after the <, so your statement effectively says:

if ( sockfd =
(socket(PF_INET,SOCK_STREAM,0) < 0)
) {
...
}

socket() will return a value which will either evaluate to < 0 or >= 0,
which means s will get 0 or 1 ... not the socket.

I always try to split it to two lines:

sockfd = socket(PF_INET,SOCK_STREAM,0);
if (sockfd < 0) {
Die("server socket");
}

Good luck.

adoyee 02-07-2004 12:59 AM

It runs.
BTW,
Is the file descriptor of STDIN 0?

schmack 02-07-2004 11:11 AM

Yes.

stdin is 0, stdout is 1 and stderr is 2 on Unix-based systems.


All times are GMT -5. The time now is 01:07 PM.