开发者

HTTP POST/GET via stdio

Is it possible to communicate with server via raw file IO functions like fprintf() and fscanf()?

E.g. this code won't work afaik:

#include <stdio.h>

int main()
{
    FILE* f = fopen("http://example.com/", "r");

    while (!feof(f))
    {
     开发者_运维技巧   char *s = new char[255];
        fscanf(f, "%s", s);
        printf("%s\n", s);
        delete[] s;
    }

    fclose(f);

    return 0;
}


No, this won't work, that's not what stdio is for. But I tell you what you can do:

  • Use libcurl
  • Use asio
  • Use your own sockets

I don't know about win32, but on Unix you could:

  • Get a socket to that host using connect(2)
  • Use fdopen(3) to associate it with a FILE *

Also, those functions aren't "raw" at all; there's quite a lot going on before the really raw stuff is called (the system calls).


Not by STDIO by itself, you have to have something open the socket.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜