Program does not read through correctly

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 11
Reputation: fatboysudsy is an unknown quantity at this point 
Solved Threads: 0
fatboysudsy fatboysudsy is offline Offline
Newbie Poster

Program does not read through correctly

 
0
  #1
May 6th, 2007
Hi there,

here is a copy of my server program. It worked this morning but has now decided to stop and i am after some help with it please.

It stops half way through the authentication(), it must be before it does the stcmp function as i never get that far. it fact it does not even get to the printf statement of "we are here now". but it does read the file and print out the correct numbers stored in it.

I do not know what has happened and hoped someone could shed some light for me.

Also how can i get the program to quit should the authentication method ever work !!!!!

Thanks in advance guys.

  1. /*server.c - demonstration server program. Written in C Nov '94 from
  2.   Pascal version of Nov/Dec '93
  3.   revised Jan 2005 to use stdio.h and to count number of clients
  4.   */
  5.  
  6. #include <stdio.h>
  7. #include "sockets.c.h"
  8. #define TRUE 1
  9.  
  10. socket_id server_socket;
  11. int count1,count2;
  12.  
  13. int authentication(char box[],char id[], socket_id sock);
  14. int query_dealing(char sending_data[],socket_id sock);
  15. int viewing_data (char data[],socket_id sock);
  16. int clientcount;
  17.  
  18.  
  19. void serve_client(socket_id sock)
  20. {
  21. /*procedure to implement the service */
  22.  
  23. char message[500], box[10],id[50];
  24. char query[256];
  25. char data[256];
  26. char sending_data[256];
  27.  
  28. /* set up data to serve to client */
  29. sprintf(message,"Hello and welcome to NuTV's online service ",clientcount);
  30. /* now send message to client */
  31. send_data(sock,message,strlen(message)+1);
  32. count1 = recv_data(sock,box);
  33. send_data(sock,"+OK",4);
  34. count2 = recv_data(sock,id);
  35. send_data(sock,"+OK",4);
  36.  
  37. authentication(box,id,sock);
  38. //char Q;
  39.  
  40. // recv_data(sock,sending_data);
  41.  
  42. // if (sending_data[0]==Q)
  43. // {
  44. // query_dealing(sending_data,sock);
  45. // }
  46. // else
  47. // {
  48. // viewing_data (sending_data,sock);
  49. // }
  50.  
  51.  
  52.  
  53. close_socket(sock);
  54.  
  55. }
  56.  
  57. int authentication(char box[],char id[],socket_id sock)
  58. {
  59. printf("entering auth\n");
  60.  
  61. #define THEFILE "/usr/users/students/group_r/r020032/sockets1/customeraccounts.txt"
  62.  
  63. char part1[10];
  64. char part2[6];;
  65. char part3[256];
  66. char part4[6];
  67.  
  68. /* pointer for the file */
  69. FILE *fp;
  70. /* file reading array */
  71.  
  72. /* open file for reading */
  73.  
  74.  
  75. fp=fopen(THEFILE,"r");
  76.  
  77. if(fp!= NULL)
  78.  
  79. {
  80. printf("scanning\n");
  81. /* scans in the first 2 tab fields from the customeraccounts file and stores them in the a\
  82.   rrays part1 & part2 */
  83.  
  84. fscanf(fp, "%s %s %s %s",part1,part2,part3,part4);
  85. printf("This the settop number %s and this is the customer ID number %s\n",part1,part2);
  86. } else {
  87. printf("Cannot open the file.\n");
  88.  
  89. }
  90. if (fclose(fp)!=0)
  91.  
  92. {
  93. printf("the file could not be closed.\n");
  94. }
  95. printf("we are here now\n");
  96. if ((strcmp(box,part1)==0)&& (strcmp(id,part2)==0))
  97. {
  98. printf("entering authorise section");
  99. printf("your set top box number is %d char long and is %s\n",count1,box);
  100. printf("your id number is %d char long and is %s\n",count2,id);
  101. printf(" Congraulations you have been authenticated");
  102. send_data(sock,"+OK",4);
  103. }
  104.  
  105. else
  106. {
  107. printf(" your numbers did not match!\n");
  108. send_data(sock,"-ERR",5);
  109. }
  110. }
  111.  
  112. // int query_dealing(char sending_data[],socket_id sock)
  113. // {
  114.  
  115. // printf("hi i am in query_handling\n");
  116.  
  117. // recieves the query from the client. Opens the viewing information folder and uses the info\
  118. sent to send back the details wanted
  119. //
  120. // if (data[1]==1)
  121. // {
  122. // }
  123. // else if (data[1]==2
  124. // {// }
  125. // else
  126. // {
  127. // }
  128. //}
  129.  
  130. //int viewing_data(char data[],socket_id sock)
  131. // {
  132.  
  133. // printf("hi i am in data");
  134. // recv_data(sock,data);
  135. // gets the viewing data from the client and writes it to a new file
  136. // }void accept_clients()
  137. { int process;
  138. socket_id new_socket;
  139. host_name client_host;
  140.  
  141. /* look for and deal with clients */
  142. while (TRUE)
  143. {
  144. printf(".... server waiting to accept connection\n");
  145. new_socket = accept_connection(server_socket, client_host);
  146. if (new_socket<0)
  147. {
  148. printf("failed to accept a connection\n");
  149. exit(1);
  150. }
  151. else
  152. { /* client connection accepted, now create service process */
  153. printf("server: connection accepted from %s \n",client_host);
  154. clientcount++;
  155. process = fork();
  156. if (process == 0) /* child server process */
  157. {
  158. close_socket(server_socket);
  159. serve_client(new_socket);
  160. exit(0);
  161. }
  162. else
  163. { /*parent server process*/
  164. close_socket(new_socket);
  165. } }
  166. } /*client processed*/
  167. } /*loop forever */
  168. } /*accept & serve clients */
  169.  
  170.  
  171. main()
  172. { port_number port;
  173. int ok;
  174.  
  175.  
  176. printf("server ...\n");
  177. printf("please enter port number\n");
  178. scanf("%d",&port);
  179.  
  180.  
  181. server_socket = create_socket();printf("server: socket %d created, trying to bind name ...",server_socket);
  182.  
  183. ok=bind_socket(server_socket, port);
  184. if (ok < 0)
  185. {
  186. printf("failed to bind name to socket(ok =%d)",ok);
  187. }
  188. else
  189. {
  190. printf("server: name bound to socket, now listen ...\n");
  191. listen(server_socket,5);
  192. accept_clients();
  193. }
  194. }
Last edited by WaltP; May 6th, 2007 at 11:56 pm. Reason: INLINECODE is used for short text in your post. CODE is for posting blocks of code
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Program does not read through correctly

 
0
  #2
May 6th, 2007
looks like a buffer overflow in the line
  1. fscanf(fp, "%s %s %s %s",part1,part2,part3,part4);
the contents of the file is something that is not under programmatic control; a secure alternative should be used.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 11
Reputation: fatboysudsy is an unknown quantity at this point 
Solved Threads: 0
fatboysudsy fatboysudsy is offline Offline
Newbie Poster

Re: Program does not read through correctly

 
0
  #3
May 7th, 2007
Hi,

Thanks i have have got that bit sorted now.

if i decline the authorisation i want to close the connection, how do i do this ?

this is what i have written, when i enter incorrect details i do get the printf statement in the else but the connection stays open.
  1. if (status==1)
  2. {
  3. printf("you should continue\n");
  4. printf("true = %d",status);
  5. }
  6. else
  7. {
  8. printf(" i am here but not closing\n");
  9. close_socket(sock);
  10. }

Also how can i read several lines of data from a file and store them to send over to my client. I need to search the file for matching criteria...i.e i have a list of tv programmes but i only want whats on on BBC 1.

Cheers for looking, any help would be greatly appreciated.
Last edited by ~s.o.s~; May 7th, 2007 at 1:32 pm. Reason: Fixed code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Program does not read through correctly

 
0
  #4
May 7th, 2007
Originally Posted by fatboysudsy View Post
...i do get the printf statement in the else but the connection stays open.
  1. if (status==1)
  2. {
  3. printf("you should continue\n");
  4. printf("true = %d",status);
  5. }
  6. else
  7. {
  8. printf(" i am here but not closing\n");
  9. close_socket(sock);
  10. }
which is the socket api that you are using?

Also how can i read several lines of data from a file and store them to send over to my client. I need to search the file for matching criteria...
read one line of data by using fgets. read several lines by calling fgets in a loop and buffer the lines that are read into dynamically allocated memory.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 11
Reputation: fatboysudsy is an unknown quantity at this point 
Solved Threads: 0
fatboysudsy fatboysudsy is offline Offline
Newbie Poster

Re: Program does not read through correctly

 
0
  #5
May 7th, 2007
hi not sure of this api business, but i am using a stream socket, writing it in C and using putty(ssh) to connect to a linux machine at uni.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 11
Reputation: fatboysudsy is an unknown quantity at this point 
Solved Threads: 0
fatboysudsy fatboysudsy is offline Offline
Newbie Poster

Re: Program does not read through correctly

 
0
  #6
May 7th, 2007
Ok,

I have used fgets, and managed to read all the parts of my file, 3 lines. When i print it out they are all there. I then send it over to my server program and low and behold i have only 1 line of data.

Where on earth have the other two gone ???

I have used this to send it. line being where it is stored


send_data(local_socket,line,strlen(line)+1);
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Program does not read through correctly

 
0
  #7
May 8th, 2007
Originally Posted by fatboysudsy View Post
I have used fgets, and managed to read all the parts of my file, 3 lines. When i print it out they are all there. I then send it over to my server program and low and behold i have only 1 line of data.
probable reason is that you have failed to account for the null character that is appended by a call to fgets
not sure of this api business, but i am using a stream socket
on a normal call to close(), the protocol stack attempts to gracefully shutdown the connection after ensuring all unsent data is sent. In the case of TCP, the stack also ensures that sent data is acknowledged by the peer. This is done in the background (after the call to close() returns), regardless of whether the socket is blocking or non-blocking.

SO_LINGER affects the behaviour of the close() operation. call setsockopt(..., SO_LINGER,...) with the following values in the linger structure; linger->l_onoff non-zero and linger->l_linger zero. close() returns immediately. The underlying stack discards any unsent data, and, in the case of connection-oriented protocols such as TCP, sends a RST (reset) to the peer (this is termed a hard or abortive close).

** warning ** it is not surprising to find the various implementations interpreting the effect of SO_LINGER differently. the purpose of SO_LINGER is very specific and only a minority of socket applications actually need it. unless you are familiar with the intricacies of TCP and BSD sockets, you could very easily end up using SO_LINGER in a way for which it was not designed. so use this only if the immediate closing of the socket is an absolute requirement; and then test it thoroughly.
Last edited by vijayan121; May 8th, 2007 at 4:45 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC