LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-04-2004, 08:47 PM   #1
ahogg
LQ Newbie
 
Registered: Apr 2004
Location: Paris, France
Distribution: Mandrake 9.2
Posts: 5

Rep: Reputation: 0
Question inet_ntoa() problem with array of sockaddr_in


Hi all...

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?
 
Old 05-04-2004, 09:49 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
That looks right to me. How bogus are the IPs?
 
Old 05-04-2004, 10:47 PM   #3
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
bah why can't i delete!!!

Last edited by infamous41md; 05-04-2004 at 10:49 PM.
 
Old 05-05-2004, 04:49 AM   #4
ahogg
LQ Newbie
 
Registered: Apr 2004
Location: Paris, France
Distribution: Mandrake 9.2
Posts: 5

Original Poster
Rep: Reputation: 0
Here's the whole beastie + it's output:

Code:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <errno.h>

#define BACKLOG 10
#define LISTENERPORT 6660

main()
{
	printf("Starting...\n");
	int fd_listener, fd_connector[BACKLOG];
	struct sockaddr_in myaddr;
	struct sockaddr_in thataddr[BACKLOG];

	if((fd_listener = socket(PF_INET, SOCK_STREAM, 0)) == -1)
		perror("Listener error:");
	else
		printf("listener socket %d created\n",fd_listener);
	
	myaddr.sin_family = AF_INET;
	myaddr.sin_port = htons(LISTENERPORT);
	myaddr.sin_addr.s_addr = htons(INADDR_ANY);
	memset(&(myaddr.sin_zero),'\0',8);
	printf("myaddr stuffed with info\n");

	if(bind(fd_listener, (struct sockaddr *)&myaddr, sizeof(myaddr))==-1)
	{	
	perror("Bind error:");	
		
	}
	else
		printf("binding done OK\n");
	
	listen(fd_listener, BACKLOG);
	printf("listening...\n");
	
	int i;
	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\non socket: %d\n",
			       i, inet_ntoa(thataddr[i].sin_addr, fd_connector[i]));
	}
}
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
 
Old 05-05-2004, 08:43 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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):

Code:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

#define BACKLOG 10
#define LISTENERPORT 6660

int main(void)
{
  int fd_listener, fd_connector[BACKLOG];
  struct sockaddr_in myaddr;
  struct sockaddr_in thataddr[BACKLOG];
  int i, size = sizeof(struct sockaddr_in);

  printf("Starting...\n");
  if((fd_listener = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    perror("Listener error:");
  else
    printf("listener socket %d created\n", fd_listener);

  myaddr.sin_family = AF_INET;
  myaddr.sin_port = htons(LISTENERPORT);
  myaddr.sin_addr.s_addr = htons(INADDR_ANY);
  memset(&(myaddr.sin_zero),'\0',8);
  printf("myaddr stuffed with info\n");

  if(bind(fd_listener, (struct sockaddr *)&myaddr, sizeof(myaddr))==-1)
    perror("Bind error:");
  else
    printf("binding done OK\n");

  listen(fd_listener, BACKLOG);
  printf("listening...\n");

  for (i=0 ; i < BACKLOG ; ++i)
  {
    fd_connector[i] = accept(fd_listener,
      (struct sockaddr*)&(thataddr[i]), &size);
    printf("Connection %d accepted from %s\non socket: %d\n",
      i, inet_ntoa(thataddr[i].sin_addr), fd_connector[i]);
  }

  return 0;
}

Last edited by itsme86; 05-05-2004 at 08:46 AM.
 
Old 05-05-2004, 11:21 AM   #6
ahogg
LQ Newbie
 
Registered: Apr 2004
Location: Paris, France
Distribution: Mandrake 9.2
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks!

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

socklen_t * addrlen

At any rate, thanks a lot!

Anthony
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
about struct sockaddr_in greghua Programming 11 07-29-2005 11:01 AM
Accessing member functions of sockaddr_in Stack Overflow Programming 4 01-27-2005 05:11 PM
sockaddr_in and sockaddr AquamaN Programming 4 05-02-2004 03:52 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
LaTeX Problem: \array Config Programming 1 06-30-2002 05:00 AM

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

All times are GMT -5. The time now is 10:19 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration