Click to See Complete Forum and Search --> : FindNextFile to go through directory


MuffinFlavored
March 7th, 2008, 01:39 AM
I have created a Winsock2 server and client to transfer information.
I want the server to find every file in directory, open it, read the username string, and see if the username sent from the client via a packet is the same as the string from the file.

I get this error:
Unhandled exception at 0x7c901010 in Server.exe: 0xC0000005: Access violation reading location 0x00000fb4.

This error lead me to string.h a few times, then it lead me to osinfo.h.

Here is the function I am having problems with (everything before this is perfect for me), with the error causing line marked.

int check_receive_register_user_data() {
WIN32_FIND_DATA f;

HANDLE h = FindFirstFile("users//*.txt", &f);
if(h != INVALID_HANDLE_VALUE) {
do {
DWORD dwNumRead;

char* filename = (char*)malloc(strlen(f.cFileName) + strlen("users//"));
sprintf(filename, "users//%s", f.cFileName);

h = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

int filesize = GetFileSize(h, NULL);

char* buffer = (char*)malloc(filesize);
ReadFile(h, buffer, filesize, &dwNumRead, NULL);
buffer[filesize] = '\0';

char* username = (char*)malloc(strlen(user.username)+1);
sprintf(username, "%s\n", user.username);

char* email = (char*)malloc(strlen(user.email)+1);
sprintf(email, "%s\n", user.email);

if (strstr(buffer, username) != NULL) {
error("The username is already in use!");
send_string_size("in use username");
send_string("in use username", strlen("in use username"));
return -1;
}

if (strstr(buffer, email) != NULL) {
error("The e-mail address is already in use!");
send_string_size("in use email");
send_string("in use email", strlen("in use email"));
return -1;
}
} while(FindNextFile(h, &f) != 0); <<<< THIS TRIGGERS THE ERROR I AM POSTING ABOUT, EVERYTHING BEFORE THIS IS 100% CORRECT IN TERMS OF VALUE
}

success("The username is free!");
send_string_size("free username");
send_string("free username", strlen("free username")); //Let the server know the username is free.

success("The e-mail address is free!");
send_string_size("free email");
send_string("free email", strlen("free email")); //Let the server know the username is free.

FindClose(h);

return 1;
}


Here is my whole code.

#include "Server.h"

void error(char* string) {
printf("Error! %s\n", string);
}

void success(char* string) {
printf("Success! %s\n", string);
}


int send_string_size(char* string) {
int send_buffer_size = strlen(string);
send_buffer_size_string = (char*)malloc(sizeof(send_buffer_size));
sprintf(send_buffer_size_string, "%d", send_buffer_size);

if (send(client, send_buffer_size_string, sizeof(send_buffer_size_string), 0) == -1) {
error("Unable to send packet size!");
free(send_buffer_size_string);
return -1;
}

return send_buffer_size;
}

int send_string(char* string, int send_buffer_size) {
send_buffer = (char*)malloc(send_buffer_size);
sprintf(send_buffer, "%s", string);

if (send(client, send_buffer, send_buffer_size, 0) == -1) {
error("Unable to send packet!");
free(send_buffer);
return -1;
}

return 1;
}

int receive_string_size() {
char* receive_buffer_size_string = (char*)malloc(4);
int receive_buffer_size;

if (recv(client, receive_buffer_size_string, sizeof(receive_buffer_size_string), 0) == -1) {
error("Unable to recieve packet size!");
return -1;
}

else {
receive_buffer_size = atoi(receive_buffer_size_string);
}

return receive_buffer_size;
}
char* receive_string(int receive_buffer_size) {
receive_buffer = (char*)malloc(receive_buffer_size+1);
if (recv(client, receive_buffer, receive_buffer_size, 0) == -1) {
error("Unable to recieve packet!");
return "error";
}

else {
receive_buffer[receive_buffer_size] = '\0';
}

return receive_buffer;
}

int startup() {
WSADATA WSAData;

if (WSAStartup(0x0202, &WSAData) != 0) {
error("WSAStartup failed!");
return -1;
}

else {
success("WSAStartup succeeded!");
}

SOCKET server = socket(AF_INET, SOCK_STREAM, 0);

sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(80);
serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");

if (bind(server, (LPSOCKADDR)&serveraddr, sizeof(serveraddr)) != 0) {
error("Could not bind!");
return -1;
}

else {
success("Binded!");
}

if (listen(server, 12) != 0) {
error("Could not listen!");
return -1;
}

else {
success("Listening!");
}

if ((client = accept(server, NULL, NULL)) == INVALID_SOCKET) {
error("Could not accept!");
return -1;
}

else {
success("Accepted a user!\n");
}

return 1;
}

bool get_user_status() {
int login_status_string_size = receive_string_size();
char* user_status = (char*)malloc(login_status_string_size);
sprintf(user_status, "%s", receive_string(login_status_string_size));

if (strcmp(user_status, "not logged in") == 0) {
success("The user is not logged in!");
return false;
}

else if (strcmp(user_status, "logged in") == 0) {
error("The user is logged in!");
return true;
}
}
char* receive_function() {
return receive_string(receive_string_size());
}

int receive_register_user_data() {
int username_length = receive_string_size();
user.username = (char*)malloc(username_length);
sprintf(user.username, "%s", receive_string(username_length));

if (strcmp(user.username, "error") == 0) {
error("user.username is error!");
return -1;
}

else {
success("Received username!");
}

int password_length = receive_string_size();
user.password = (char*)malloc(password_length);
sprintf(user.password, "%s", receive_string(password_length));

if (strcmp(user.password, "error") == 0) {
error("user.password is error!");
return -1;
}

else {
success("Received password!");
}

int email_length = receive_string_size();
user.email = (char*)malloc(email_length);
sprintf(user.email, "%s", receive_string(email_length));

if (strcmp(user.email, "error") == 0) {
error("user.email is error!");
return -1;
}

else {
success("Received e-mail address!");
}

return 1;
}
int check_receive_register_user_data() {
WIN32_FIND_DATA f;

HANDLE h = FindFirstFile("users//*.txt", &f);
if(h != INVALID_HANDLE_VALUE) {
do {
DWORD dwNumRead;

char* filename = (char*)malloc(strlen(f.cFileName) + strlen("users//"));
sprintf(filename, "users//%s", f.cFileName);

h = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

int filesize = GetFileSize(h, NULL);

char* buffer = (char*)malloc(filesize);
ReadFile(h, buffer, filesize, &dwNumRead, NULL);
buffer[filesize] = '\0';

char* username = (char*)malloc(strlen(user.username)+1);
sprintf(username, "%s\n", user.username);

char* email = (char*)malloc(strlen(user.email)+1);
sprintf(email, "%s\n", user.email);

if (strstr(buffer, username) != NULL) {
error("The username is already in use!");
send_string_size("in use username");
send_string("in use username", strlen("in use username"));
return -1;
}

if (strstr(buffer, email) != NULL) {
error("The e-mail address is already in use!");
send_string_size("in use email");
send_string("in use email", strlen("in use email"));
return -1;
}
} while(FindNextFile(h, &f) != 0);
}

success("The username is free!");
send_string_size("free username");
send_string("free username", strlen("free username")); //Let the server know the username is free.

success("The e-mail address is free!");
send_string_size("free email");
send_string("free email", strlen("free email")); //Let the server know the username is free.

FindClose(h);

return 1;
}

int store_register_user_data() {
FILE* file;
int id = 0;
char filename[1024];

while (1) {
sprintf((char*)filename, "users/%d.txt", id);

if (fopen(filename, "r") != NULL) {
id++;
continue;
}

else {
break;
}
}

if ((file = fopen(filename, "w+")) == NULL) {
printf("Error opening file %s!", filename);
return -1;
}

else {
if (fprintf(file, "Username: %s\nPassword: %s\nE-Mail Address: %s", user.username, user.password, user.email) == NULL) {
printf("Error writing to file %s!\n", filename);
return -1;
}

else {
printf("Wrote Username: %s\nPassword: %s\nE-Mail Address: %s to file %s!", user.username, user.password, user.email, filename);
}
}

return 1;
}

int cleanup () {
if (shutdown(server, SD_BOTH) != 0) {
printf("Could not shutdown the socket's send and receive functions!\n");
}

else {
printf("Shutdown socket!\n");
}

if (closesocket(server) != 0) {
printf("Could not close the socket!\n");
return -1;
}

else {
printf("Closed socket!\n");
}

if (WSACleanup() != 0) {
printf("Could not empty the loaded WinSock libraries!\n");
return -1;
}

else {
printf("Unloaded WinSock libraries!\n");
}

free(receive_buffer_size_string);
free(send_buffer_size_string);
free(receive_buffer);
free(send_buffer);

return 1;
}

int main() {
if (startup() == 1) {
if (get_user_status() == false) {
if (strcmp(receive_function(), "register") == 0) {
if (receive_register_user_data() == 1) {
if (check_receive_register_user_data() == 1) {
/*if (store_register_user_data() == 1) {
}*/
}
}
}
}
}

cleanup();
system("pause");
return 0;
}


Here is my header file:

#define _WINSOCKAPI_
#undef UNICODE
#include <windows.h>
#include <stdio.h>
#include <winsock2.h>

struct user;
struct message;
struct comment;
struct announcment;

void error(char* string);
void success(char* string);

int receive_string_size();
char* receive_string(int receive_buffer_size);

int send_string_size(char* string);
int send_string(char* string, int send_buffer_size);

int startup();
bool get_user_status();
char* receive_function();
int receive_register_user_data();
int check_register_user_data();
int store_register_user_data();
int cleanup();

SOCKET server;
SOCKET client;

int result;

char* receive_buffer;
char* receive_buffer_size_string;

char* send_buffer;
char* send_buffer_size_string;

struct user {
int id;
char* username;
char* password;
char* email;
char* date;
char* time;
};

struct message {
int id;
char* recipient[16];
char* sender[16];
char* subject;
char* string;
char* date[32];
char* time[16];
};

struct comment {
int id;
char* recipient[16];
char* sender[16];
char* string;
char* date[32];
char* time[16];
};

struct announcment {
int id;
char* creator[16];
char* string;
char* date[32];
char* time[16];
};

user user;


I am compiling with Microsoft Visual Studio 2008.
If any more information is required, I will be more than happy to post.

mgrey
March 7th, 2008, 08:13 AM
Hi there. Just an observation here:
- You call FindFirstFile function to get the search handle to be used in subsequent call to FindNextFile or FindClose
- Later you overwrite this handle with a CreateFile function call
- Then you call FindNextFile function with this overwritten handle (where you've marked the error line)

Well, I haven't written code to see if this is really the problem. Maybe you want to give it a try.

Another word of advise if you don't mind. Check the return value of CreateFile, just in case...

Regards

MuffinFlavored
March 8th, 2008, 08:46 AM
Hi there. Just an observation here:
- You call FindFirstFile function to get the search handle to be used in subsequent call to FindNextFile or FindClose
- Later you overwrite this handle with a CreateFile function call
- Then you call FindNextFile function with this overwritten handle (where you've marked the error line)

Well, I haven't written code to see if this is really the problem. Maybe you want to give it a try.

Another word of advise if you don't mind. Check the return value of CreateFile, just in case...

Regards

You could be right.
I use FindFirstFile to find the first filein the directory, so I can get the filename.
Then, I open it with CreateFile to read the contents into "buffer".
Then I do FindNextFile to see if there is another file.

What I am aiming for is, I have a directory called "users", filled with files of "registered users and e-mail addresses". I want the function to go through every file there to see if the username received from the packet is in one of the files.

FIXED:
For anyone who has this problem, do not use the same handle from FindFirstFile to open a file, use a different handle, as the same handle from FindFirstFile, is needed the same way it is for FindNextFile.

Thank you!

Now I am having another problem,
both buffer and username are the correct values, and strstr returns Bad Ptr.

Revised function:

int check_receive_register_user_data() {
WIN32_FIND_DATA f;

HANDLE h = FindFirstFile("users//*.txt", &f);
HANDLE file;
if(h != INVALID_HANDLE_VALUE) {
do {
DWORD dwNumRead;

char* filename = (char*)malloc(strlen(f.cFileName) + strlen("users//"));
sprintf(filename, "users//%s", f.cFileName);

file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
int filesize = GetFileSize(file, NULL);

char* buffer = (char*)malloc(filesize);
ReadFile(file, buffer, filesize, &dwNumRead, NULL);
buffer[filesize] = '\0';

char* username = (char*)malloc(strlen(user.username)+2);
sprintf(username, "%s", user.username);
username[strlen(user.username)] = '\n';
username[strlen(user.username)+1] = '\0';

if (strstr(buffer, username) != NULL) {
error("The username is already in use!");
send_string_size("in use username");
send_string("in use username", strlen("in use username"));
return -1;
}

char* email = (char*)malloc(strlen(user.email)+1);
sprintf(email, "%s\n", user.email);

if (strstr(buffer, email) != NULL) {
error("The e-mail address is already in use!");
send_string_size("in use email");
send_string("in use email", strlen("in use email"));
return -1;
}
} while(FindNextFile(h, &f) != 0);
}

success("The username is free!");
send_string_size("free username");
send_string("free username", strlen("free username")); //Let the server know the username is free.

success("The e-mail address is free!");
send_string_size("free email");
send_string("free email", strlen("free email")); //Let the server know the username is free.

FindClose(h);

return 1;
}


char* buffer debug value: (Read from the file)

Username: test
Password: test
E-Mail Address: test@aol.com


char* username debug value: (Received from the client)

test\n


The newline byte is shown as a little block in the Text Visualizer when debugging the variable.

The reason I need the new line is, saying a user MuffinFlavored existed, and a user named Flavored wanted to join. It would say Flavored is taken when doing a strstr, since Flavored exists in MuffinFlavored.

So, I want it so search for the username and then a newline break.

What am I doing wrong, strstr returns 0x00000000 <Bad Ptr>

Thank you for solving my first problem! :)

Paul McKenzie
March 8th, 2008, 11:10 AM
char* buffer = (char*)malloc(filesize);
//...
buffer[filesize] = '\0';
This is a memory overwrite. The higest index is buffer[filesize-1]. You may have made the same type of mistake in the rest of your code.

Regards,

Paul McKenzie

MuffinFlavored
March 8th, 2008, 11:40 AM
char* buffer = (char*)malloc(filesize);
//...
buffer[filesize] = '\0';
This is a memory overwrite. The higest index is buffer[filesize-1]. You may have made the same type of mistake in the rest of your code.

Regards,

Paul McKenzie

I have, everywhere.
For some reason, malloc returns excess memory allocated.
For example:

int login_status_string_size = receive_string_size(); The size of the incoming string. In this case, 13
char* user_status = (char*)malloc(login_status_string_size); ÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««þîþîþîþ (More than 13 characters, and I only want to allocate 13 bytes)
sprintf(user_status, "%s", receive_string(login_status_string_size)); receive_string in this case is "not logged in", which is 13 letters, which is I only wanted to allocate 13 bytes of memory.

Paul McKenzie
March 8th, 2008, 12:27 PM
I have, everywhere.
For some reason, malloc returns excess memory allocated.That reason may be for the compiler's debugging library, so as to detect overwrites in memory, or for other reasons. It is an implementation detail that your program should never rely on.

The bottom line is that you don't write out of bounds, since the behaviour is undefined as to what will happen when you do so.

Regards,

Paul McKenzie

MuffinFlavored
March 8th, 2008, 12:58 PM
That reason may be for the compiler's debugging library, so as to detect overwrites in memory, or for other reasons. It is an implementation detail that your program should never rely on.

The bottom line is that you don't write out of bounds, since the behaviour is undefined as to what will happen when you do so.

Regards,

Paul McKenzie

I would not have to write out of bounds if the malloc allocated only what I wanted.

The reason I had to write out of bounds was so that the string would terminate, as I want to use it for exact precise functions like strcmp.

Even when not debugging and compiling for release, malloc returns a pointer of a 32 byte allocated array.

Paul McKenzie
March 8th, 2008, 01:20 PM
I would not have to write out of bounds if the malloc allocated only what I wanted. :confused:

C (which your code seems to be, as opposed to C++) does not work this way! The malloc() call does what it's supposed to do, and that is to allocate x bytes to you. Whatever the implementation does behind the scenes when malloc() is called is not your concern. Why you're even looking at these details is another question. You are responsible to make sure that all of your accesses to memory are legal. This means that if you have allocated an array of x bytes, the only valid locations are bytes 0 to x-1 of that allocated space.
The reason I had to write out of boundsStop right there. There is *never* a reason to write out of bounds in a C program.
so that the string would terminate, as I want to use it for exact precise functions like strcmp.Then you must allocate the space needed for all of your string manipulations. If this means that your strings will contain NULLs or if you need extra space to do whatever, you're the one responsible for allocating enough space for all of these manipulations.
Even when not debugging and compiling for release, malloc returns a pointer of a 32 byte allocated array.Again, why are you looking at implementation details? It's very simple -- you call malloc(x), you only have x bytes that are available to you. I don't care if the compiler's library allocates a million extra bytes or no extra bytes, that is not the concern of the programmer.

This misunderstanding of how to handle dynamically allocated memory is part and parcel of why you're having issues with your code.

Regards,

Paul McKenzie

mgrey
March 8th, 2008, 03:17 PM
Hi MuffinFlavored. In addition to what Paul McKenzie's already said, there are a couple of points in your code.



char* username = (char*)malloc(strlen(user.username)+2);
sprintf(username, "%s", user.username);
username[strlen(user.username)] = '\n';
username[strlen(user.username)+1] = '\0';You can replace the last two lines with strcat(username, "\n"); or you can replace the last three lines with sprintf(username, "%s\n", user.username); if user.username is a null-terminated string. Otherwise, you cannot use strlen on user.username, or use sprintf to copy the content of user.username to username using %s. Question: are you sure user.username is a null-terminated string ?

strstr returns null because it cannot find username in buffer (wow!). You can make your life easier if you drop this new line character in the first place.

This final part is a bit complicated. Here you call send_string function
send_string("in use username", strlen("in use username"));Now, strlen returns the number of characters, excluding the terminal NULL. And the send_string function is like this
int send_string(char* string, int send_buffer_size) {
send_buffer = (char*)malloc(send_buffer_size);
sprintf(send_buffer, "%s", string);
...Only the part relevant to our discussion. In the function, you allocate space for in user username, a total of 16 characters, but not for the terminating-null, because of the return value of strlen. Then you call sprintf to copy string to send_buffer, and you are one character beyond the limits of send_buffer. See the documentation of sprintf; it appends a null character after the last character written. You better allocate a larger buffer here.

Your question was 'why strstr returns null?'. Maybe beacuse LF-CR problems. I recommend a new design without new line character. And you must revise all your dynamic allocation concept.

MuffinFlavored
March 8th, 2008, 07:38 PM
Hi MuffinFlavored. In addition to what Paul McKenzie's already said, there are a couple of points in your code.



char* username = (char*)malloc(strlen(user.username)+2);
sprintf(username, "%s", user.username);
username[strlen(user.username)] = '\n';
username[strlen(user.username)+1] = '\0';You can replace the last two lines with strcat(username, "\n"); or you can replace the last three lines with sprintf(username, "%s\n", user.username); if user.username is a null-terminated string. Otherwise, you cannot use strlen on user.username, or use sprintf to copy the content of user.username to username using %s. Question: are you sure user.username is a null-terminated string ?

strstr returns null because it cannot find username in buffer (wow!). You can make your life easier if you drop this new line character in the first place.

This final part is a bit complicated. Here you call send_string function
send_string("in use username", strlen("in use username"));Now, strlen returns the number of characters, excluding the terminal NULL. And the send_string function is like this
int send_string(char* string, int send_buffer_size) {
send_buffer = (char*)malloc(send_buffer_size);
sprintf(send_buffer, "%s", string);
...Only the part relevant to our discussion. In the function, you allocate space for in user username, a total of 16 characters, but not for the terminating-null, because of the return value of strlen. Then you call sprintf to copy string to send_buffer, and you are one character beyond the limits of send_buffer. See the documentation of sprintf; it appends a null character after the last character written. You better allocate a larger buffer here.

Your question was 'why strstr returns null?'. Maybe beacuse LF-CR problems. I recommend a new design without new line character. And you must revise all your dynamic allocation concept.

Thank you for replying.

I did revise it:

char* stralloc(int size) {
char* test = malloc(size+1);

if (test == NULL) {
exit(-1);
}

return test;
}


The error now is the strstr.

user.username is defined here

int receive_register_input() {
int password_length, email_length;
int username_length = receive_string_size();
user.username = stralloc(username_length);
sprintf(user.username, "%s", receive_string(username_length)); (Should I add \0 here? I thought sprintf did that for me.)

if (strcmp(user.username, "error") == 0) {
error("Receiving the username!");
return -1;
}
...


My error causing code being:

filename = stralloc(strlen(f.cFileName) + strlen("users//"));
sprintf(filename, "users//%s", f.cFileName);

file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
filesize = GetFileSize(file, NULL);

buffer = stralloc(filesize);
ReadFile(file, buffer, filesize, &dwNumRead, NULL);
buffer[filesize] = '\0';

username = stralloc(strlen(user.username));
sprintf(username, "%s\n", user.username);

if (strstr(buffer, username) != NULL) {
error("The username is already in use!");
send_string_size("in use username");
send_string("in use username");
return -1;
}


The \n seems to cause the problem. The \n from sprintf and the \n from the file must not be the same. Anyone know how to fix this?

Paul McKenzie
March 8th, 2008, 08:37 PM
The \n seems to cause the problem. The \n from sprintf and the \n from the file must not be the same. Anyone know how to fix this?Then clear out all of the control characters from the end of each string before you call strstr().

#include <ctype.h>
#include <string.h>

void ClearString(char *str)
{
int i;
int nLen = strlen(str);
int ctrlFound = 1;
for (i = nLen-1; i >= 0 && ctrlFound; --i )
{
if ( !iscntrl(str[i]) )
ctrlFound = 0;
else
str[i] = '\0';
}
}

The above removes any control characters at the end of the string.

Regards,

Paul McKenzie

mgrey
March 8th, 2008, 09:23 PM
sprintf(user.username, "%s", receive_string(username_length)); (Should I add \0 here? I thought sprintf did that for me.)Ok. Consider something like this: sprintf(buffer, "%s", string);
1-string must be null-terminated, 2-buffer must be large enough to take all the characters of string, and the terminating null-char. Yes, sprintf puts a null-char at the end of the buffer. This answers your question, you do not add '\0' there. (be careful if you ever use _snprintf though)
In your case, what does receive_string return ? Is there a null-char at the end of the string returned by it ?

The \n seems to cause the problem. The \n from sprintf and the \n from the file must not be the same. Anyone know how to fix this ?Before trying to fix it yourself, maybe you'd like to see this: http://en.wikipedia.org/wiki/Line_feed. Brings a smile to your face, doesn't it, especially the 'New line in programming languages' part ? :) Do you understand now why I'm recommending you to drop the new line ?

MuffinFlavored
March 9th, 2008, 01:26 AM
I solved everything with some persistance and freenode ##c.

Thank you for all of you help.
If anyone has the same problem of trying to strstr from a file read buffer, the newline character in strstr should be for example
strstr(buffer, "username\r\n") NOT \n\n

For anyone curious, here is the finish code:

#include "Server.h"

void error(char* string) {
printf("Error! %s\n", string);
}

void success(char* string) {
printf("Success! %s\n", string);
}

char* stralloc(int size) {
char* string = malloc(size+1);

if (string == NULL) {
error("Could not allocate memory!");
return -1;
}

return string;
}

int send_string(char* string) {
char* send_buffer_size_string = stralloc(strlen(string));
char* send_buffer = stralloc(strlen(string));
sprintf(send_buffer_size_string, "%d", strlen(string));

if (send(sockets.client, send_buffer_size_string, sizeof(send_buffer_size_string), 0) == -1) {
error("Unable to send packet size!");
free(send_buffer_size_string);
return -1;
}

free(send_buffer_size_string);

sprintf(send_buffer, "%s", string);

if (send(sockets.client, send_buffer, strlen(string), 0) == -1) {
error("Unable to send packet!");
free(send_buffer);
return -1;
}

free(send_buffer);
return 1;
}

int receive_string_size() {
char* receive_buffer_size_string = stralloc(10);
int receive_buffer_size;

if (recv(sockets.client, receive_buffer_size_string, 10, 0) == -1) {
error("Unable to recieve packet size!");
free(receive_buffer_size_string);
return -1;
}

receive_buffer_size = atoi(receive_buffer_size_string);
free(receive_buffer_size_string);
return receive_buffer_size;
}

char* receive_string(int receive_buffer_size) {
char* receive_buffer = stralloc(receive_buffer_size);

if (recv(sockets.client, receive_buffer, receive_buffer_size+1, 0) == -1) {
error("Unable to recieve packet!");
free(receive_buffer);
return "error";
}

receive_buffer[receive_buffer_size] = '\0';

return receive_buffer;
}

int startup() {
WSADATA WSAData;
WORD wVersionRequested = MAKEWORD(2,2);
struct sockaddr_in server_address;

if (WSAStartup(wVersionRequested, &WSAData) != 0) {
error("WSAStartup.");
return -1;
}

else {
success("WSAStartup!");
}

sockets.server = socket(AF_INET, SOCK_STREAM, 0);

if (sockets.server == INVALID_SOCKET) {
error("Socket.");
return -1;
}

else {
success("Created socket!");
}

server_address.sin_family = AF_INET;
server_address.sin_port = htons(80);
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");

if (bind(sockets.server, (LPSOCKADDR)&server_address, sizeof(server_address)) != 0) {
error("Bind.");
return -1;
}

else {
success("Bound!");
}

if (listen(sockets.server, 12) != 0) {
error("Listen.");
return -1;
}

else {
success("Listening!");
}

if ((sockets.client = accept(sockets.server, NULL, NULL)) == INVALID_SOCKET) {
error("Accepting client.");
return -1;
}

else {
success("Accepted client!");
}

return 1;
}

int receive_status() {
int login_status_string_size = receive_string_size();
char* user_status = stralloc(login_status_string_size);
sprintf(user_status, "%s", receive_string(login_status_string_size));

if (strcmp(user_status, "not logged in") == 0) {
success("User is not logged in!");
return 0;
}

else if (strcmp(user_status, "logged in") == 0) {
success("User is logged in!");
return 1;
}

return -1;
}

char* receive_function() {
return receive_string(receive_string_size());
}

int receive_input(int login) {
int username_length, password_length, email_length;

username_length = receive_string_size();
user.username = stralloc(username_length);
sprintf(user.username, "%s\0", receive_string(username_length));

if (strcmp(user.username, "error") == 0) {
error("Receiving the username!");
return -1;
}

password_length = receive_string_size();
user.password = stralloc(password_length);
sprintf(user.password, "%s", receive_string(password_length));

if (strcmp(user.password, "error") == 0) {
error("Receiving the password!");
return -1;
}

if (login == 0) {
email_length = receive_string_size();
user.email = stralloc(email_length);
sprintf(user.email, "%s", receive_string(email_length));

if (strcmp(user.email, "error") == 0) {
error("Receiving the e-mail address!");
return -1;
}
}

return 1;
}

int check_input(int login) {
char* filename;
char* buffer;

char* username;
char* password;
char* email;

char* username_status;
char* password_status;
char* email_status;

int filesize;

DWORD dwNumRead;
WIN32_FIND_DATA f;

HANDLE h = FindFirstFile("users//*.txt", &f);
HANDLE file;

if(h != INVALID_HANDLE_VALUE) {
do {
filename = stralloc(strlen(f.cFileName) + strlen("users//"));
sprintf(filename, "users//%s", f.cFileName);

file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
filesize = GetFileSize(file, NULL);

buffer = stralloc(filesize);
ReadFile(file, buffer, filesize, &dwNumRead, NULL);
buffer[filesize] = '\0';

username = stralloc(strlen(user.username)+2);
sprintf(username, "%s\r\n", user.username);
username[strlen(user.username)+2] = '\0';

if (login == 1) {
if (strstr(buffer, username) != NULL) {
username_status = stralloc("valid username");
sprintf(username_status, "valid username");

if (strstr(buffer, password) != NULL) {
password_status = stralloc("valid password");
sprintf(password_status, "valid username");
}

else {
password_status = stralloc("invalid password");
sprintf(password_status, "invalid username");
}
}

else {
username_status = stralloc("invalid username");
sprintf(username_status, "invalid username");
}

send_check_login_input(username_status, password_status);
}

else {
if (strstr(buffer, username) != NULL) {
username_status = stralloc("in use username");
sprintf(username_status, "in use username");
}

else {
username_status = stralloc("free username");
sprintf(username_status, "free username");
}

email = stralloc(strlen(user.email));
sprintf(email, "%s", user.email);
email[strlen(user.email)] = '\0';

if (strstr(buffer, email) != NULL) {
email_status = stralloc("in use email");
sprintf(email_status, "in use email");
}

else {
email_status = stralloc("free email");
sprintf(email_status, "free email");
}

send_check_register_input(username_status, email_status);
}
} while(FindNextFile(h, &f) != 0);
}

user.id = atoi(f.cFileName)+1;
return 1;
}

int send_check_register_input(char* username_status, char* email_status) {
if (send_string(username_status) == -1) {
error("Username status.");
return -1;
}

else {
success("Sent username status!");
}

if (send_string(email_status) == -1) {
error("E-mail address status.");
return -1;
}

else {
success("Sent e-mail address status!");
}

return 1;
}

int send_check_login_input(char* username_status, char* password_status) {
if (send_string(username_status) == -1) {
error("Sending username status.");
return -1;
}

else {
success("Sent username status!");
}

if (send_string(password_status) == -1) {
error("Sending password status.");
return -1;
}

else {
success("Sent password status!");
}

return 1;
}

int store_register_input() {
HANDLE file;
DWORD dwNumWritten;
char* filename;
char* buffer;

filename = stralloc(strlen("users//.txt") + sizeof(user.id));
sprintf(filename, "users//%d.txt", user.id);

buffer = stralloc(strlen(user.username) + strlen(user.password) + strlen(user.email) + strlen("\r\n\r\n") + strlen("Username: Password: E-Mail Address: "));
sprintf(buffer, "Username: %s\r\nPassword: %s\r\nE-Mail Address: %s", user.username, user.password, user.email);

if ((file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) != 0) {
error("Creating file.");
}

else {
success("Created file!");
}

if (WriteFile(file, buffer, strlen(buffer), &dwNumWritten, NULL) == 0) {
error("Writing to file.");
}

else {
success("Wrote to file!");
}

CloseHandle(file);

return 1;
}

int cleanup() {
int result;

result = shutdown(sockets.server, SD_BOTH);

if (result != 0) {
error("Could not shutdown the socket's send and receive functions!");
}

result = closesocket(sockets.server);

if (result != 0) {
error("Could not close the socket!");
return -1;
}

result = WSACleanup();

if (result != 0) {
error("Could not empty the loaded WinSock libraries!");
return -1;
}

return 1;
}

int main() {
if (startup() == 1) {
if (receive_status() == 0) { //Not logged in

int function_size = receive_string_size();
char* function = stralloc(function_size);
sprintf(function, "%s", receive_string(function_size));
printf("Received function \"%s\"!\n", function);

if (strcmp("register", function) == 0) {
if (receive_input(0) == 1) {
if (check_input(0) == 1) {
store_register_input();
}
}
}

else if (strcmp("login", function) == 0) {
if (receive_input(1) == 1) {
if (check_input(1) == 1) {
send_string("create cookie");
}
}
}
}
}

cleanup();
system("pause");
return 0;
}


Thank you for all the help.