Ok, I haven't stopped working on this....
I made a new program to search through a text file and output the line it finds the answer into an array of chars...
The test program I made worked before I put it into the socket code. Basically, what my function does is:
- Get given a string as a char*
- Checks the file for that string in each line
- Returns the contents of the first line that string was found in as a char*
This was the code for my program:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int findName(char* clientSearch, char* serverResult);
int main()
{
char search[256] = {"Billy"};
char result[256];
findName(search, result);
printf("The string is: %s", result);
return 1;
}
int findName(char* clientSearch, char* serverResult)
{
FILE *filename=fopen("NAMES.TXT","r");
char temp[256];
bzero(temp,256);
while (filename!=NULL && fgets(temp, sizeof(temp),filename) != NULL)
{
if (strstr(temp, clientSearch))
{
printf("temp is: %s", temp);
strncpy(serverResult,temp, sizeof(temp));
return 1;
}
}
if (filename!=NULL) fclose(filename);
return 0;
}
So this worked with no problems, it would go into my text file, find Billy and print to screen:
Billy Brown 18
or whatever was on that line that Billy was in.
(I understand this algorithm needs work, as it might produce bad results if the name is found somewhere else other than at the start of the line etc etc, but i'm going to fix that later)
So...
I now stuck this into my socket code (the findName function)... but the output I'm getting is gibberish.
It seems like it can't find a match for what is in buffer, because I put some debugging code and asked for it to print to screen when it went into the STRSTR if loop (meaning it found the string in that line of the text file).
Here is the code:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void errormsg(char *message) // Error message function
{
perror(message); // Show user error message
exit(1); // exit
}
int findName(char* clientSearch, char* serverResult)
{
FILE *filename=fopen("NAME.TXT","r");
char temp[256];
while (filename!=NULL && fgets(temp, sizeof(temp),filename) != NULL)
{
if (strstr(temp, clientSearch))
{
printf("temp is: %s", temp);
strncpy(serverResult,temp, sizeof(temp));
}
}
if (filename!=NULL) fclose(filename);
return -1;
}
int main (int argc, char *argv[])
{
int sockd; // file descriptor for socket
int newsockd; // file descriptor for new socket
int portnum; // port number for transmissions
int clientlength; // length of client address
int retval; // return value
char buffer[256];
char searchresult[256];
struct sockaddr_in serv_addr, cli_addr; // structure defined in netinet header file.
if (argc < 2) // Server must be initiated with a port.
{
fprintf(stderr,"Usage:./server Port \n");
exit(1);
}
sockd = socket(AF_INET, SOCK_STREAM, 0); // AF_INET for internet domain, Stream Socket
if (sockd == -1)
error("ERROR: Opening socket failed");
bzero((char *) &serv_addr, sizeof(serv_addr)); // Set all vals in serv_addr to zero
portnum = atoi(argv[1]); // Gets user input and stores as port number
serv_addr.sin_family = AF_INET; // Sets serv_addr in struct sock_addr_in
serv_addr.sin_port = htons(portnum); // change the port number from host to server format
serv_addr.sin_addr.s_addr = INADDR_ANY; // contains IP address of host
// bind socket to address, cast serv_addr to type struct sockaddr
if (bind(sockd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR: Bind failed"); // present error if it returns -1 (fails)
listen(sockd,5); // allow p to 5 connections that can wait while process handles an existing connection.
clientlength = sizeof(cli_addr); // set clientlength variable to the length of client address
// set reference to client address and length of client address
newsockd = accept(sockd, (struct sockaddr *) &cli_addr, &clientlength);
if (newsockd < 0)
error("ERROR: couldn't establish connection with client");
// bzero(buffer,256); // zero the buffer values
retval = read(newsockd,buffer,255); // store the new socket file description into the buffer
if (retval < 0)
error("ERROR: could not read new socket file description");
printf("Message received from client is: %s", buffer); // show users output
findName(buffer,searchresult);
printf("Result is %s", searchresult);
retval = write(newsockd, searchresult, 18);
if (retval < 0)
error("ERROR: could not write to socket");
// Send string to user there ^
return 0;
}
Like I said before, I added in some debug code, just asking it to print out to the screen when it entered the str str if statement and it never got there. It just printed out some gibberish (black squares with question marks inside). I'm pretty sure it's not finding a match for what's in buffer and what's in the text file.
I am sending from the client the word "Billy". It is appearing on the server side "Message recieved from client: Billy" but then it just spits out The result is: gibberish black squares...
Like I said, I'm pretty sure it's something to do with buffer, that it can't find Billy because maybe buffer contains other stuff in it?
Is there something I can do to clear buffer? Or is the problem lying somewhere else?
Any help is GREATLY appreciated!!!!!!!
I really don't have much time I hope someone can help me...