LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-06-2004, 10:37 PM   #1
adoyee
LQ Newbie
 
Registered: Jul 2003
Posts: 16

Rep: Reputation: 0
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 !
 
Old 02-07-2004, 12:08 AM   #2
schmack
LQ Newbie
 
Registered: Feb 2004
Posts: 12

Rep: Reputation: 0
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.
 
Old 02-07-2004, 12:59 AM   #3
adoyee
LQ Newbie
 
Registered: Jul 2003
Posts: 16

Original Poster
Rep: Reputation: 0
It runs.
BTW,
Is the file descriptor of STDIN 0?
 
Old 02-07-2004, 11:11 AM   #4
schmack
LQ Newbie
 
Registered: Feb 2004
Posts: 12

Rep: Reputation: 0
Yes.

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 07:28 PM
Socket error on Connect mdimanna Programming 2 06-24-2005 10:28 AM
sendto error (socket) payal_shah Linux - Networking 0 02-23-2005 12:18 PM
Socket error = #10051 Lenin.Jello Linux - Networking 0 10-15-2004 04:55 PM
socket error 111 Muzica Linux - Software 0 02-13-2004 08:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration