Ilya
Ilya

Reputation: 29721

Reading from socket to basic_filebuf

It's code from client part (using )

   struct sockaddr_in stSockAddr;
   int Res;
   int SocketFD;
   SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
   if (-1 == SocketFD)
   {
      perror("cannot create socket");
      exit(EXIT_FAILURE);
   }

    memset(&stSockAddr, 0, sizeof(stSockAddr));

    stSockAddr.sin_family = AF_INET;
    stSockAddr.sin_port = htons(8182);
    Res = inet_pton(AF_INET, "127.0.0.1", &stSockAddr.sin_addr);    

And I get responce from server using

   char buffer[4096] = "";
   n = read(aSocketFD, buffer, 4096);  

My questin is:
Can I get responce like std::basic_filebuf? Or can I get responce to FILE *? Socket is a file handler, so I can do it, but how?

Upvotes: 0

Views: 230

Answers (3)

For C code, you can use a descriptor like FILE* calling fdopen(): associates a stream with a file descriptor (POSIX.1 standard).

Upvotes: 1

Michael Wild
Michael Wild

Reputation: 26381

I think you would need to subclass std::streambuf to achieve what you want.

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96316

If you asked this because you want good performance:

On linux you can use sendfile, this works with simple file descriptors (fd):

ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

Some other unix systems also support this API call, but it's not part of POSIX, so it's not portable.

Upvotes: 0

Related Questions