PDA

View Full Version : Chat server problem


sPyder
01-02-2004, 07:45 PM
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);
}

i3839
01-02-2004, 10:12 PM
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)".

Loco
01-02-2004, 10:24 PM
The problem as "i3839" explains is that you are receiving a pointer to a sockaddr struct, and you are treating it as a struct...

Also, the accept() will probably fail. This is the prototype for accept():
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);

And you are using a struct sockaddr** as the second argument in your call.

As i3839 wrote, please direct your questions to the corresponding forum, otherwise you won't get replies...

I'm moving this question to the General C/C++ forum

sPyder
01-02-2004, 10:38 PM
Thanks for the help. Sorry about the wrong forum deal though, I saw networking and thought it would go here ;).

RobSeace
01-02-2004, 11:37 PM
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...