UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » Chat server problem

#1 2004-01-02 08:45 PM

sPyder
Member
Registered: 2004-01-02
Posts: 2

Re: Chat server problem

I am making a chat server which accepts connections by telnet. I have the basic body of it down and when I compile it I get this error

In file included from sh0ck.c:1:
list.h: In function `connector':
list.h:194: error: request for member `sin_addr' in something not a structure or union

Which pertains to this function

int connector(int socket, struct sockaddr *client, int fdmax, fd_set *set )
{
	int addrlen;
	int newfd;
	addrlen = sizeof(struct sockaddr);
        if ((newfd = accept(socket, (struct sockaddr *)&client, &addrlen)) == -1)
	{
        	perror("accept");
		return -1;
	}
	else 
	{
                FD_SET(newfd, set);
                if (newfd > fdmax)
                	fdmax = newfd;
                printf("sh0ck: new connection from %s on socket %d\n",
			inet_ntoa(client.sin_addr), newfd);
        }
	return newfd;
}


I know that it has something to do with passing the structure to the funtion, I'm just unclear as to what it is.

EDIT:
This is how I am calling the function

if ((newfd = connector( sockfd, (struct sockaddr *)&client, fdmax, &master))==-1)
{
	perror("connector");
	close(newfd);
}

Offline

#2 2004-01-02 11:12 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Chat server problem

1: Why the hell do you use telnet???

2: Don't. ;)

3: Please don't post threads asking help with trivial compile errors, or if you do, do it in a C/C++ forum.

4: 'client' is a pointer, not a structure, so use "inet_ntoa(client->sin_addr)".

Offline

#3 2004-01-02 11:24 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: Chat server problem

Offline

#4 2004-01-02 11:38 PM

sPyder
Member
Registered: 2004-01-02
Posts: 2

Re: Chat server problem

Thanks for the help. Sorry about the wrong forum deal though, I saw networking and thought it would go here ;).

Offline

#5 2004-01-03 12:37 AM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Chat server problem

Well, yes, the pointer and "." is definitely the cause of THAT
error, however if you just use "->" in place of the ".", the compiler
will spit out another error at you, complaining that "sin_addr" isn't
a member of "struct sockaddr"...  You'll need to either cast that
to "struct sockaddr_in *", or change your function prototype to
accept sockaddr_in instead of sockaddr...

Offline

  • Index
  • » C
  • » Chat server problem

Board footer

Powered by FluxBB