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.

/*server.c - demonstration server program. Written in C Nov '94 from
  Pascal version of Nov/Dec '93
  revised Jan 2005 to use stdio.h and to count number of clients
  */
   
  #include <stdio.h>
  #include "sockets.c.h"
  #define TRUE 1
   
  socket_id   server_socket;
  int count1,count2;
   
  int authentication(char box[],char id[], socket_id sock);
  int query_dealing(char sending_data[],socket_id sock);
  int viewing_data (char data[],socket_id sock);
  int clientcount;
   
   
  void serve_client(socket_id sock)
  {
  /*procedure to implement the service */
   
     char message[500], box[10],id[50];
     char query[256];
     char data[256];
     char sending_data[256];
   
     /* set up data to serve to client */
     sprintf(message,"Hello and welcome to NuTV's online service ",clientcount);
     /* now send message to client */
     send_data(sock,message,strlen(message)+1);
     count1 = recv_data(sock,box);
     send_data(sock,"+OK",4);
     count2 = recv_data(sock,id);
     send_data(sock,"+OK",4);
   
     authentication(box,id,sock);
     //char Q;
   
     //   recv_data(sock,sending_data);
   
     //   if (sending_data[0]==Q)
     // {
     // query_dealing(sending_data,sock);
     // }
     // else
     // {
     //  viewing_data (sending_data,sock);
     // }
   
   
   
       close_socket(sock);
   
  }
   
  int authentication(char box[],char id[],socket_id sock)
  {
    printf("entering auth\n");
   
    #define THEFILE "/usr/users/students/group_r/r020032/sockets1/customeraccounts.txt"
   
    char  part1[10];
    char  part2[6];;
    char  part3[256];
    char  part4[6];
   
    /* pointer for the file */
          FILE *fp;
   /* file reading array */
   
    /* open file for reading */
   
   
    fp=fopen(THEFILE,"r");
   
    if(fp!= NULL)
   
      {
        printf("scanning\n");
        /* scans in the first 2 tab fields from the customeraccounts file and stores them in the a\
  rrays part1 & part2 */
   
        fscanf(fp, "%s %s %s %s",part1,part2,part3,part4);
        printf("This the settop number %s and this is the customer ID number %s\n",part1,part2);
      } else {
       printf("Cannot open the file.\n");
   
     }
    if (fclose(fp)!=0)
   
  {
    printf("the file could not be closed.\n");
  }
    printf("we are here now\n");
  if ((strcmp(box,part1)==0)&& (strcmp(id,part2)==0))
      {
        printf("entering authorise section");
        printf("your set top box number is %d char long and is %s\n",count1,box);
        printf("your id number is %d char long and is %s\n",count2,id);
        printf(" Congraulations you have been authenticated");
        send_data(sock,"+OK",4);
      }
   
    else
      {
        printf(" your numbers did not match!\n");
        send_data(sock,"-ERR",5);
      }
  }
   
  // int query_dealing(char sending_data[],socket_id sock)
  // {
   
  //   printf("hi i am in query_handling\n");
   
     // recieves the query from the client. Opens the viewing information folder and uses the info\
   sent to send back the details wanted
  //
  //     if (data[1]==1)
  //       {
  //       }
  //    else if (data[1]==2
  //       {//       }
  //     else
  //       {
  //       }
  //}
   
  //int viewing_data(char data[],socket_id sock)
  // {
   
  //   printf("hi i am in data");
            //  recv_data(sock,data);
     // gets the viewing data from the client and writes it to a new file
  // }void accept_clients()
  {  int  process;
     socket_id  new_socket;
     host_name  client_host;
   
     /* look for and deal with clients */
     while (TRUE)
     {
        printf(".... server waiting to accept connection\n");
        new_socket = accept_connection(server_socket, client_host);
        if (new_socket<0)
        {
           printf("failed to accept a connection\n");
           exit(1);
        }
        else
        { /* client connection accepted, now create service process */
           printf("server: connection accepted from %s \n",client_host);
           clientcount++;
           process = fork();
           if (process == 0) /* child server process */
           {
              close_socket(server_socket);
              serve_client(new_socket);
              exit(0);
           }
           else
           { /*parent server process*/
              close_socket(new_socket);
           }        }
        } /*client processed*/
     } /*loop forever */
  } /*accept & serve clients */
   
   
  main()
  {  port_number   port;
     int   ok;
   
   
     printf("server ...\n");
     printf("please enter port number\n");
     scanf("%d",&port);
   
   
     server_socket = create_socket();printf("server: socket %d created, trying to bind name ...",server_socket);
   
     ok=bind_socket(server_socket, port);
     if (ok < 0)
     {
        printf("failed to bind name to socket(ok =%d)",ok);
     }
     else
     {
        printf("server: name bound to socket, now listen ...\n");
        listen(server_socket,5);
        accept_clients();
     }
  }

looks like a buffer overflow in the line

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.

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.

if (status==1)
     {
       printf("you should continue\n");
       printf("true = %d",status);
     }
   else
     {
       printf(" i am here but not closing\n");
       close_socket(sock);
     }

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.

...i do get the printf statement in the else but the connection stays open.

if (status==1)
     {
       printf("you should continue\n");
       printf("true = %d",status);
     }
   else
     {
       printf(" i am here but not closing\n");
       close_socket(sock);
     }

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.

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.