More C help pls
Hi I still writing my proxy server. I'm having trouble with the send commmand but I think its the way I'm trying to split the string thats causing the problems, because when I run the entire request string through it works.
Currently I'm trying fname = "GET "; temp = "\n\n"; sscanf(client_buffer,"GET http:// % [ ^/ ] % [ ^\n ] ", host, filename); strcat(request, fname); strcat(request, filename); strcat(request, temp); Where client_buffer is a char[1024] got with recv from my client, it holds the requested web page. I am trying to split it so host holds the web address & request holds the requested file (after catination, ie GET /index.html HTML V1 \n\n). But when I pass request to the host (after getting a connection) the program hangs. This does not happen when client_buffer is passed (which defeats the purpose of a proxy). Basically I think I need a different or better way to spilt the string. Please help & also point out any really obvious mistakes I've made. (pls igone the random variable defined for debug purposes that haven't been talken out). The rest of the program: PHP Code:
|
Error
Hi dude...
If you take a closer look at your format string you will see that it's wrong... look: sscanf(client_buffer,"GET <a href="http://%" target="_blank">http://%</a>[/url][^/]%[^\n]", host, filename); a format string should absolutly contain conversions character %s string %d integer etc... you only specify % that's why it fucks up and you close " as well few times in your html code... you cant do it as is... and hummm... If I were you I would rather consider using strtok() function to parse your string in tokens instead... so Good Luck |
tips and tricks
and humm as matter of optimizing your code
do not malloc a buffer to 1024 directly hardcoded use fstat() to read the higher block size for better I/O efficiency declare a struct stat using st_blksize member I recommand using read() and write() instead of recv(), send() respectivly. I had experience writing a TCPIP tunnel software and it decreases overhead time.. nitr0gen |
How would you use strtok, I can only find sketchy details on it.
|
Use the man Luke
man strtok
pretty easy prototype char *strtok(char *s, const char *delim) you specify s as your string and seperate it using delimiter delim i.e. I have a string char buf[] = "this is an example"; char *ptr; ptr = strtok(buf, " "); printf("first token: %s\n", ptr); ptr = strtok(NULL, " "); printf("second token: %s\n", ptr); this following piece of code seperate the string in token with space as delimiter. this would print like first token: this second token: is at the first call you issue your string as first parameter and further calls you specify NULL... hope you got it.. take a look at the man anyways its pretty easy to handle tokens nitr0gen |
Thanks, it all makes sense now. As for the man pages I'm currently sitting at a windows machine, unfortunately.
|
your welcome
no problem
if you have any other question feel free to ask me nitr0gen |
All times are GMT -5. The time now is 08:22 AM. |