ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If your "ERROR on binding" message is printing, then the problem isn't with accept(), it's with bind(). And I was asking about COMPILER errors/warnings, but providing the full source code served the same purpose.
It doesn't matter what address you are binding on, as long as its an address belonging to a network interface on your machine. Since you are using INADDR_ANY then its taken care of for you.
When socket functions such as bind() returns with an error (-1), they also set the global C variable errno. You can get further information from this variable with the following code:
Code:
#include <iostream>
#include <cerrno> //Provides the errno variable
#include <cstring> //Provides the strerror() function
using namespace std;
//Your code here...
if ( bind( /* Args... */ ) == -1 ) {
cout << strerror(errno);
//Your error handling here...
}
//More code...
I think you could have a potential problem with the datatype of portno. You have it as a plain old int (likely 32 bits, and signed) when it should be an unsigned short int (16 bits). When you run it through htons() (the 's' is for "short") it converts your int to an unsigned short int and reverses the byte order, potentially munging the port number in the process. For all you know it could be turning into port 0 or some such nonsense. Be careful with implicit casts.
Anyway, your code compiles and runs just fine here. Add some code to print out details on your bind() error and correct the datatype of portno, then we'll go from there.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.