| | |
Program does not read through correctly
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 11
Reputation:
Solved Threads: 0
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.
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.
c Syntax (Toggle Plain Text)
/*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(); } }
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
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
looks like a buffer overflow in the line
the contents of the file is something that is not under programmatic control; a secure alternative should be used.
C Syntax (Toggle Plain Text)
fscanf(fp, "%s %s %s %s",part1,part2,part3,part4);
•
•
Join Date: Mar 2007
Posts: 11
Reputation:
Solved Threads: 0
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.
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.
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.
C Syntax (Toggle Plain Text)
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.
Last edited by ~s.o.s~; May 7th, 2007 at 1:32 pm. Reason: Fixed code tags, learn to use them.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
...i do get the printf statement in the else but the connection stays open.
C Syntax (Toggle Plain Text)
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...
•
•
Join Date: Mar 2007
Posts: 11
Reputation:
Solved Threads: 0
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
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);
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
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.
•
•
•
•
not sure of this api business, but i am using a stream socket
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.
![]() |
Similar Threads
- Using UTF's, IndexOf's and substrings (Java)
- array program, dont know where to even begin! (C)
- Problems with my bus ticket program. (Python)
- Can anyone suggest why this program doesn't calculate correctly? (Java)
- qbasic program under Windows XP (Legacy and Other Languages)
- error with win2k, memory could not be "read" (Windows NT / 2000 / XP)
- Can't see the program execute (C++)
- substitute for conio.h in linux (C)
- Cant execute a c++ program (C++)
Other Threads in the C Forum
- Previous Thread: 16 bits to 32 bits
- Next Thread: Opening 20,000 notepad files and appending to one document
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char character convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer lazy linked linkedlist linux linuxsegmentationfault list logical_drives match matrix meter microsoft motherboard multi mysql odf open opendocumentformat openwebfoundation pattern pause pdf pointer pointers posix power problem program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system test testautomation unix urboc user voidmain() win32api windows.h






