windows - how to send an image in winsock2, using c -


I am writing a very simple webserver in C (winsock2).

I have the content of my HTML pages.

Currently, what I am doing is writing the content of a file in four * buffers and sending it using "send ()"

However when I Try reading an image (jpg, bmp), I can not write characters in a buffer, some letters are "zero" (0).

How can I send a full image file?

Thanks

you enter a char * buffer Can store empty characters. You only have to use a counter, remember how many letters were written, instead of computing the number of non-blank characters, instead of re-computing (this is either an integer at the next point of entering the buffer or Can be an indicator).

To send a file, you will do something like this:

  int sendFile (int sock, const char * filename) {FILE * file = fopen (filename, "rb "); If (file == NULL) return -1; If (fseek (file, 0, SEEK_END)! = 0) {fclose (file); Return -1; } Off_t size = ftello (file); If (fseek (file, 0, SEEK_SET)! = 0) {fclose (file); Return -1; } If SendBinaryFileHeaderAndSize (sock, size) <0} {fclose (file); Return -1; } Four buffer [4096]; For (;;) {size_t read = fread (buffer, 1, size (buffer), file); If (read == 0) {int retcode = 0; If (Terror (file)) codec = -1; Fclose (file); Return ratecode; } (Size_t sent = 0; sent & lt; read;) {intrate = send (sock, buffer + sent, read, 0); If (rate at lieutenant; 0) {fclose (file); Return -1; } Assert (ret & lt; = read - sent); Sent + = rate; }}}    

Comments