Thread: chat server problem

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    3

    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);
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    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

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    3
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client works on a LAN but don't send all the data
    By Niara in forum Networking/Device Communication
    Replies: 9
    Last Post: 01-04-2007, 04:44 PM
  2. SMTP Server Not Working
    By (TNT) in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-15-2003, 05:33 AM
  3. Requesting Java Chat Client Maintenence
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-02-2003, 01:57 PM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM
  5. win32 Winsock problem
    By spoon_ in forum Windows Programming
    Replies: 3
    Last Post: 07-19-2002, 01:19 AM