Quote:
This is not what you want to malloc. argv is an array of char *, so sizeof(argv[1]) is sizeof(char *). Try malloc(strlen(argv[1] + 1)). The +1 is to leave room for the null byte.
This is the type of stuff I'm looking for. It's so obvious now that you mention it, my brain was so wrapped up in trying to get the socket working that stuff like this flies right by me, I've been having problems with this earlier and ended up hard-coding it in but manged to (somehow) get it working before posting the code this time. Thanks for pointing it out.
Quote:
You need to set readBuffer[readBytes] = '\0', not readBytes + 1. Remember, arrays start at 0 in C, so if you read 4 bytes, they are buf[0] through buf[3], and buf[4] is where you want your null.
I've known about this for so long and still have trouble with it, it's embarrassing.
Quote:
Also, you never check readBytes to see if recv actually worked. If it fails, readBytes will be -1, and you need to print an appropriate error message and possibly exit.
Yeah I probably should do that, you know because it seems like I'm getting errors, maybe I should check for them :wink:.
Quote:
Instead of your complicated find_substr function, can you just use strstr? Something like strstr(string, substring) - string should give you the index of the substring.
I figured there must have been a standard function for this already, the one I'm using is my own makeshift one. I've read about strstr() before but saw that it returned a character pointer whereas I believe I wanted the integer position. You seem to know what you're talking about, I'll look into it.
Quote:
Lastly, socket is a function name, sock is the file descriptor of your socket. Try close(sock) instead of close(socket). If you didn't get a compiler warning for that, crank up the warning settings. Try adding -Wall to your gcc command.
Yeah all the warnings are off at the moment, it probably wouldn't even let me compile this abomination otherwise :o. You're correct on that point as well, it should be sock, not socket.
I'll make the changes you proposed and return back with the news. Thanks for your input anduril462!