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.
I'm learning linux sockets and have written what seems to be the classic newbie server program . Create a socket, bind it, listen and accept. Since I wanted to accept several connections I thought I might put the accept() calls in a loop like this:
Code:
for (i=0 ; i < BACKLOG ; ++i)
{
fd_connector[i] = accept(fd_listener,
(struct sockaddr*)&thataddr[i],sizeof(thataddr[i]));
printf("Connection %d accepted from %s\%",
i, inet_ntoa(thataddr[i].sin_addr));
}
To my best knowledge, this would create up to BACKLOG socket descriptors as the connections came in, and fill the required fields in each indexed sockaddr_in struct.
When testing, I just telnet repeatedly from another machine in my local network, expecting to see that machine's 192.168.x.x address pop up. Instead, I get an assortment of bogus IPs. Any insight?
And the output:
I'd be tempted to say that this socket 0 business is probably not right.
Starting...
listener socket 3 created
myaddr stuffed with info
binding done OK
listening...
Connection 0 accepted from 233.107.0.64
on socket: 0
Connection 1 accepted from 160.248.255.191
on socket: 0
Connection 2 accepted from 0.0.0.0
on socket: 0
Connection 3 accepted from 0.0.0.0
on socket: 0
Connection 4 accepted from 0.0.0.0
on socket: 0
Connection 5 accepted from 88.63.1.64
on socket: 0
Connection 6 accepted from 80.68.1.64
on socket: 0
Connection 7 accepted from 127.3.0.0
on socket: 0
Connection 8 accepted from 132.249.255.191
on socket: 0
Connection 9 accepted from 228.248.255.191
on socket: 0
Otherwise, I'm not sure I understand you post infamous
I couldn't even quite get your code to compile as is. I made the required modifications to get it to compile, ran it, and it works fine for me. Here's exactly what I used (the indentation changes weren't necessary, but it's just easier for me to read this way):
It works now... For those who are curious, itsme86 declared an int called size and initialised it with sizeof(struct sockaddr_in).
As it happens the man pages say accept() takes a pointer to the size of the structure's size, not the actual int. So dereferencing size just did the trick I guess.
Mine compiled OK with gcc, but it's true that I got a warning about passing an int as a pointer... Thing is, I couldn't figure out what the accept man pages meant by the type
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.