Thread: help with linked list in C

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    29

    help with linked list in C

    in my code , i have trouble adding strings to list, and checking if they exist
    before adding to the list i check if it already exists, if it does not, then i add it, but the problem is when i add the first time it works, then all the next times never detects it as already there in the list, even if it already is...

    client_list is the linked list
    buffer is the string, that it reads into from the socket, so buffer will act as the username.
    which i want to see if already exists before adding

    i check for T because i know it will have to start with T, and theres other possiblilties, but i havent done them for now

    edit: it seems everytime i do the already_there function it detects client_list == NULL, why is that?

    Code:
    #include "header.h"
    int main(int argc, char **argv) {
    	client *client_list=NULL; item *item_list=NULL;
    	item_list = add_item("bike", "8", "70", "5", "False", item_list);
    	int socket_fd, client_socket_fd; 
    	int client_array[FD_SETSIZE];
    	struct sockaddr_in server_address, client_address;
    	socklen_t client_length; 
    	fd_set allset, rset;
    	char buffer[MAXDATASIZE+1]; 
    	int maxfd, i, nready, nread;
    	int yes = 1;
    	for(i=0; i < FD_SETSIZE; i++) client_array[i]=-1;
    	if ((socket_fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) error("ERROR socket error");
    	server_address.sin_family = AF_INET; 
    	server_address.sin_port = htons(PORT);
    	server_address.sin_addr.s_addr = INADDR_ANY;
    	memset(&(server_address.sin_zero), 0, 8);
    	if((setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int))) == -1) error("ERROR setsockopt error");
    	if (bind(socket_fd, (struct sockaddr *)&server_address, sizeof(struct sockaddr)) == -1) error("ERROR on binding");
    	if (listen(socket_fd, BACKLOG) == -1) error("ERROR on listening");
    	FD_ZERO(&allset);		     
    	FD_SET(socket_fd, &allset);
    	maxfd = socket_fd;
    	client_length = sizeof(client_address);
    	while(1) {                  
    		rset = allset;
    		nready = select(maxfd+1, &rset, NULL, NULL, NULL);
    		if (nready < 0) perror("select()");
    		else {
    			if (FD_ISSET(socket_fd, &rset)) {
    				client_length = sizeof(struct sockaddr_in);
    				if ((client_socket_fd = accept(socket_fd, (struct sockaddr *)&client_address, &client_length)) == -1) { 
    					perror("accept"); continue;
    				}
    				if (client_socket_fd > maxfd) maxfd = client_socket_fd;
    				client_array[client_socket_fd] = client_socket_fd; 
    				FD_SET(client_socket_fd, &allset); 
    			}
    		}
    		for (i = 3; i <= maxfd; i++) {
    			if ((client_array[i] > 0) && (FD_ISSET(i, &rset))) {
    				nread = read(i, buffer, MAXDATASIZE);
    				if (nread > 0) {
    					buffer[nread] = '\0';  
    					if (buffer[0]=='T') {
    						if (already_there(client_list, buffer)==0) {
    							write(1, buffer, strlen(buffer)); write(1, " connected!\n", 12);
    							char tmp_buffer[strlen(buffer)]; strcpy(tmp_buffer,buffer);
    							client_list = add_client(tmp_buffer, client_list, i);
    							send_message(i, "OK");
    						}
    						else {
    							send_message(i, "NO");
    							write(1, "Rejected incoming client - ", 27); write(1, buffer, strlen(buffer)); write(1, "! (Same name user already connected)\n", 37);
    						}
    					} 
    				}
    				else {
    					if (nread == -1) {
    						perror("read");
    						printf("Server encountered read error on fd %d, closing\n", i);
    					} 
    					else if (nread == 0) {
    						printf("Server found EOF on fd %d, closing\n", i);
    						client_list = remove_client(client_list, i);
    					}
    					CLOSECONNECTEDCLIENT
    				}
    			}
    		}
    	}
    	close(socket_fd);
    	return 0;
    }
    this is from the other file
    Code:
    typedef struct client_list {
    	char *client_name;
    	int client_socket_fd;
    	struct client_list *next;
    } client;
    
    int already_there(client *client_list, char *username) {
    	if (client_list == NULL) return(0);
    	if (strcmp(client_list->client_name, username)==0) {
    		return(1);
    	}
    	else return already_there(client_list->next, username);
    }
    
    client *add_client(char *username, client *client_list, int client_socket_fd) {
    	client *new_client = (client *)malloc(sizeof(client)); 
    	new_client->client_name = username;
    	new_client->client_socket_fd = client_socket_fd;
    	new_client->next = client_list;
    	return new_client;
    }
    
    client *remove_client(client *client_list, int client_socket_fd) {
    	if (client_list == NULL) return NULL;
    	if (client_list->client_socket_fd == client_socket_fd) {
    		client *tempNextP;
    		tempNextP = client_list->next;
    		free(client_list);
    		return tempNextP;
    	}
    	client_list->next = remove_client(client_list->next, client_socket_fd);
    	return client_list;
    }
    Last edited by omega666; 04-02-2011 at 02:35 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It starts off NULL so your first call here is going to return 0:
    Code:
    						if (already_there(client_list, buffer)==0) {
    							write(1, buffer, strlen(buffer)); write(1, " connected!\n", 12);
    							char tmp_buffer[strlen(buffer)]; strcpy(tmp_buffer,buffer);
    							client_list = add_client(tmp_buffer, client_list, i);
    							send_message(i, "OK");
    						}
    You would be better off taking our your linked list code, dropping it into a small stand-alone program and making sure you can add / remove / find / etc., first, and once you know it behaves how you exactly want it to, then move it back to your socket code. Testing both at the same time is just going to make things difficult for you.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    29
    nvm i think i got it
    Last edited by omega666; 04-02-2011 at 07:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM