-
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
Code:
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
Code:
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.
This is how I am calling the function
Code:
if ((newfd = connector( sockfd, (struct sockaddr *)&client, fdmax, &master))==-1)
{
perror("connector");
close(newfd);
}
-
client is a pointer to a sockaddr structure. However, in the following line, the function treats it as a reference instead of a pointer.
// Error
inet_ntoa(client.sin_addr), newfd);
// Solution
inet_ntoa(client->sin_addr), newfd);
Kuphryn
-
Didn't want to start a new thred with the same title. It runs now but It doesn't seem to bind to a port, as nmap does not show a service running on the desired port and no connections are being accepted.
Code:
#include "list.h"
#define portnum 55555
#define backlog 20
struct sockaddr_in serv;
struct sockaddr_in client;
int main()
{
int sock[backlog]; //array of client sockets
int sockfd, newfd; //serv socket and client
fd_set read, master;
int fdmax;
int i, j;
int index = 1;
int yes = 1;
long flags;
int addrlen;
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
printf("Socket\n");
exit(errno);
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
{
printf("setsockopt\n");
exit(1);
}
serv.sin_family = AF_INET;
serv.sin_port = htonl(portnum);
serv.sin_addr.s_addr = INADDR_ANY;
memset(&(serv.sin_zero), '\0', 8);
if (bind(sockfd, (struct sockaddr *)&serv, sizeof(struct sockaddr)) == -1)
{
printf("bind\n");
exit(errno);
}
if ( listen( sockfd, backlog ) == -1 )
{
printf("listen\n");
exit(errno);
}
printf("Listening...\n");
FD_ZERO(&read);
FD_ZERO(&master);
FD_SET( sockfd, &master );
fdmax = sockfd;
sock[0] = fdmax;
list_free(); i=0;
//snipped