Advertisement

06.22.2008 at 08:31AM PDT, ID: 23505779
[x]
Attachment Details

[C TCP server] Problem with Linked List, splitted string and while()

Asked by roccogalati in C Programming Language, CYGWIN, TCP/IP

Tags: C

Hi!

I will try to explain my problem as well as I can... i hope you can help me...
i tried so hard but i wasn't able to solve it...

i always have this file:


15 nick
25 marcus
18 sarah

and i add a function to my server which can send back to the client the age of each user, by giving it a text line.
Fo example, if client sends FIND10 where 1 and 0 are numbers on 16 bit in network byte order, the server has to send back the message HERE 1 15, where 1 is the text line where there is stored 15 which is the age of nick.
(0 specifies the end of command)

This works fine if the client ask for just a text line, but if he sends two consecutive FIND commands like: FIND1 and then FIND20 the server has a strange behavior.

It answer correctly for the first request, but then, for the second FIND command  it doesn't send the correct value and i don't know the reason.

For each request the server has to answer like:
HERE text_line value(age)

so for a command like FIND30
it answers like:
HERE 3 18

i'm using a client to test it, and this is the result:

(./client_test) trace - send_bytes(FIND)
(./client_test) trace - send_netshort(1)
(./client_test) trace - send_netshort(0)
(./client_test) trace - expect_bytes:
- expected 'HERE'
- received 'HERE'
(./client_test) trace - expect_netshort: expected 1, received 1
(./client_test) trace - expect_netshort: expected 15, received 15
(./client_test) trace - send_bytes(FIND)
(./client_test) trace - send_netshort(2)
(./client_test) trace - send_netshort(0)
(./client_test) trace - expect_bytes:
- expected 'HERE'
- received 'HERE'
(./client_test) error - expect_netshort: expected 2, received 0
(./client_test) info - test 'FIND of age at the second line 2: 25' failed

client code for this is:

static struct {
    char *cmd;
    int id1, term;
    char *status;
    int val1;
    char *msg;
  }
  cost_1[] = {  
    { FIND, 1, 0, HERE, 2, "FIND of age at the first line 2: 25 1: 15" },
    { FIND, 2, 0, HERE, 25, "FIND of age at the second line 2: 25" },
    { FIND, 3, 0, HERE, 590, "FIND of age at the third line 2: 25: 18" },
    };
 
 
  for (i=0; i<sizeof(cost_1)/sizeof(cost_1[0]); i++) {
    Send_bytes (sockfd, cost_1[i].cmd, sizeof(COST)-1);
    Send_netshort (sockfd, cost_1[i].id1);
    Send_netshort (sockfd, cost_1[i].term);
    Expect_bytes (cost_1[i].msg, sockfd, cost_1[i].status, sizeof(HERE)-1);
      Expect_netshort (cost_1[i].msg, sockfd, cost_1[i].id1);
       Expect_netshort (cost_1[i].msg, sockfd, cost_1[i].val1);
  }

and it works fine. The problem is with my server.

In my server code I use this functiom to trap the command:

else if(strncmp(comando, "FIND", 4) == 0)
                      {    
                    if (auth == 0)
                              {
                    Writeline(fd, noauth, strlen(noauth));
                    }
                   else {
                         if (llist->next == NULL )
                                     {
                            Writeline(fd, noob, strlen(noob));
                         }
   
                         else      if (llist ->next != NULL )
                                         {  
                                char * temp = comando+4;
                                short arr;
                                int val = 0;
   
                                do
                                   {
                                     Writeline(fd, here, strlen(here));
                                       memcpy(&arr, temp, 2);
                                     Writeline(fd, &arr, 2);
                                       temp = temp + 2;
                                     val = ntohs(arr);
                                     search_value(llist, val, fd);
      
                                    } while (val);
                               }
                           }
                         }

and my search_value() function is:

int search_value(struct NODE *llist, int text_line, int fd) {
 int retval = -1;
 int i = 1;
 int net;
 
     
  while(llist != NULL) {
  if(llist->line == text_line){ /* if there is a result for the request */
      net = htons(llist->code);
      Writeline(fd, &net, 2);             /* send the value to the client */
      llist = llist->next;
    return i;
      }      
 
  else  i++;
  llist = llist->next;
 
 }
 if (i != 1)             /* else htons(0) si returned */
 net = htons(65535);   //if there is no object, i return HERE 65535
 Writeline(fd, &net, 2);
 return retval;
}


and i load the content of the text file with this:

void load_file_in_list(){
 
 llist = (struct NODE *)malloc(sizeof(struct NODE));
 llist->code = 0;
 llist->next = NULL;
 llist->size = 0;
 llist->line = 0;  
   
   FILE * pFile;
   char buffer [100];
   int code;
   char name[1000];
 
   
   pFile = fopen (objs_file , "r");
   if (pFile == NULL) perror ("Error opening file");
   else
   {
     while ( fgets (buffer , 100 , pFile) != NULL )
     {
       
         sscanf(buffer,"%d %s \n", &code, name);
         line++;
         append_node(llist, name, code, line); /* insert values in the list */
         size++;
        }
         fclose (pFile);
      }

}


I hope someone can help me... i'd really want to solve this problem...
i'm posting the entire code below...


( it can be because i use a global variable (int line=0) in order to count lines in the text_file in my load_file_in_list()? )


p.s. if you need i can attach my server source file...

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
//search function()
 
int search_value(struct NODE *llist, int text_line, int fd) {
 int retval = -1;
 int i = 1;
 int net;
 
     
  while(llist != NULL) {
  if(llist->line == text_line){ /* if there is a result for the request */
	net = htons(llist->code);
	Writeline(fd, &net, 2);             /* send the value to the client */
	llist = llist->next;
    return i;
	}	
  
  else  i++;
  llist = llist->next;
 
 }
 if (i != 1)             /* else htons(0) si returned */
 net = htons(65535);
 Writeline(fd, &net, 2);
 return retval;
}
 
// end
 
 
 
// How i trap the command
while (comando) 
   {
        char *next = strstr(comando, "\r\n");
        if (next)
        {
            *next = '\0'; //Dont move next yet.
        }
        if ( lenght <= 500 )
        {   
             if ( strncmp(comando, "HELO", 4) == 0)
             {     
                 //username = strtok((comando+4), "\r\n"); If I remember correctly, username is a 16 char string right after HELO. Right now, comando contains HELOusername\0\nnextcommand\r\n ... So you do not want a strtok on \r\n
                 username = comando+4;
                 if (strlen(username) > 16)
                 {
                      Writeline(fd, alert, strlen(alert));
                 }
                 Writeline(fd, passw, strlen(passw));
                 read(fd, password, MAX_LINE);  
                 passwd = strtok(password, "\r\n");
 
                 if (verify_login(username, passwd)==1)
                 {
                      auth=1;
                      Writeline(fd, good, strlen(good));
                 }
                 else if(verify_login(username, passwd)==-1)
                 {
                      auth=0;
                      Writeline(fd, fail, strlen(fail));
                 }  
           
		         
				 }  
				 
					
			 else if ( strncmp(comando, "LIST", 4) == 0)
				 {  
                      display_list(llist, fd);
                 }
  
              else if ( strncmp(comando, "QUIT", 4) == 0)
				 {  
                     Writeline(fd, bye, strlen(bye));
                     free(llist);
                     return;
                 }
		     
			 else if(strncmp(comando, "FIND", 4) == 0)
			    {    
                    if (auth == 0)
					{
                    Writeline(fd, noauth, strlen(noauth));
                    }
                   else {
                         if (llist->next == NULL )
						 {
                            Writeline(fd, noob, strlen(noob));
                         }
   
                         else	if (llist ->next != NULL )
						     {   
                                char * temp = comando+4;
                                short arr;
                                int val = 0;
    
                                do 
                                   {
                                     Writeline(fd, here, strlen(here));
	                                 memcpy(&arr, temp, 2);
                                     Writeline(fd, &arr, 2);
	                                 temp = temp + 2;
                                     val = ntohs(arr);
                                     search_value(llist, val, fd); 
	
                                    } while (val);
                               }
                           }
				 }
			 
			 
				
		   }
           if (next)
               comando = next+2; //move to next command here
           else
               comando = NULL; // could as well have used break here
         }
}    
 
//load the content in a linked list
 
void load_file_in_list(){
  
 llist = (struct NODE *)malloc(sizeof(struct NODE));
 llist->code = 0;
 llist->next = NULL;
 llist->size = 0;
 llist->line = 0;  
   
   FILE * pFile;
   char buffer [100];
   int code;
   char name[1000];
  
   
   pFile = fopen (objs_file , "r");
   if (pFile == NULL) perror ("Error opening file");
   else
   {
     while ( fgets (buffer , 100 , pFile) != NULL )
     {
       
	   sscanf(buffer,"%d %s \n", &code, name);
	   line++; // global variable
	   append_node(llist, name, code, line); /* insert values in the list */
	   size++;
	  }
	   fclose (pFile);
      }
 
}
 
Keywords: [C TCP server] Problem with Linked …
 
Loading Advertisement...
 
[+][-]06.22.2008 at 08:44AM PDT, ID: 21841133

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 08:49AM PDT, ID: 21841146

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 09:09AM PDT, ID: 21841198

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 10:53AM PDT, ID: 21841525

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 11:29AM PDT, ID: 21841647

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 11:47AM PDT, ID: 21841717

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 01:40PM PDT, ID: 21842123

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 02:39PM PDT, ID: 21842316

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 02:58PM PDT, ID: 21842374

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 03:00PM PDT, ID: 21842380

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 03:04PM PDT, ID: 21842395

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 03:15PM PDT, ID: 21842426

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 03:21PM PDT, ID: 21842445

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 03:38PM PDT, ID: 21842500

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 03:42PM PDT, ID: 21842516

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 03:51PM PDT, ID: 21842547

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 03:59PM PDT, ID: 21842581

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 04:08PM PDT, ID: 21842600

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 04:22PM PDT, ID: 21842639

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 04:39PM PDT, ID: 21842680

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 05:56PM PDT, ID: 21842886

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2008 at 09:54PM PDT, ID: 21843569

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.23.2008 at 06:10AM PDT, ID: 21845791

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.23.2008 at 06:41AM PDT, ID: 21846066

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.23.2008 at 07:57AM PDT, ID: 21846884

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.23.2008 at 03:04PM PDT, ID: 21850777

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.23.2008 at 04:05PM PDT, ID: 21851152

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.23.2008 at 10:39PM PDT, ID: 21852730

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 01:04AM PDT, ID: 21853254

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 01:17AM PDT, ID: 21853323

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 01:35AM PDT, ID: 21853431

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 01:38AM PDT, ID: 21853456

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 03:28AM PDT, ID: 21854035

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 03:35AM PDT, ID: 21854093

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 03:44AM PDT, ID: 21854169

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 03:50AM PDT, ID: 21854214

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 05:10AM PDT, ID: 21854778

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 06:28AM PDT, ID: 21855453

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 07:20AM PDT, ID: 21855993

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 07:23AM PDT, ID: 21856016

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 07:30AM PDT, ID: 21856104

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 08:09AM PDT, ID: 21856607

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 08:18AM PDT, ID: 21856709

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.24.2008 at 10:37AM PDT, ID: 21858228

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.25.2008 at 12:59AM PDT, ID: 21863455

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.25.2008 at 01:09AM PDT, ID: 21863513

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.25.2008 at 01:13AM PDT, ID: 21863535

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.25.2008 at 01:47AM PDT, ID: 21863695

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.25.2008 at 01:56AM PDT, ID: 21863738

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.25.2008 at 09:06AM PDT, ID: 21867173

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.