Sending large files over socket in c - c

So this code works ok for small files of up to 20KB but when i try to send or receive larger files it stops reading at some point. Two functions for client and server, to send and receive files. I know it might have some leaks and stuff but I will fix that later. At first I want it to work properly. BUF_SIZE=512
// SENDING BYTES
void send_bytes(int connfd, int client_number, char **command_parts)
{
FILE *fp;
fp = fopen(command_parts[1], "r");
int i = 0;
// FINDING BYTE SIZE OF THE FILE
fseek(fp, 0, SEEK_END); /* PUTTING FILE POINTER TO THE END OF THE FILE */
int num_of_bytes = ftell(fp);
fseek(fp, 0, SEEK_SET); /* RESETTING FILE POINTER TO THE BEGINNING OF THE FILE */
char *sendbuff = malloc(num_of_bytes * sizeof(char));
memset(sendbuff, '\0', num_of_bytes);
// SENDING TOTAL BYTES TO SEND
while (write(connfd, itoa(num_of_bytes), 10) < 0)
{
}
printf("\nClient #%d --> Size of file sent: %d\n", client_number, num_of_bytes);
// IF BYTES MORE THAN 512 SENDING IT IN PARTS
if(num_of_bytes > BUF_SIZE)
{
int bytes_left_to_send = num_of_bytes;
char *tempbuff = calloc(BUF_SIZE, sizeof(char));
while(bytes_left_to_send > 0)
{
if(bytes_left_to_send >= BUF_SIZE)
{
// READING 512 BYTES AND PASSING IT TO TEMPBUFF
for (i = 0; i < BUF_SIZE; i++)
{
fread(&tempbuff[i], 1, 1, fp);
}
// SENDING BYTES
while (write(connfd, tempbuff, BUF_SIZE) < 0)
{
}
bytes_left_to_send = bytes_left_to_send - BUF_SIZE;
memset(tempbuff, '\0', BUF_SIZE);
}
else
{
// READING 512 BYTES AND PASSING IT TO TEMPBUFF
for (i = 0; i < bytes_left_to_send; i++)
{
fread(&tempbuff[i], 1, 1, fp);
}
// SENDING BYTES
while (write(connfd, tempbuff, bytes_left_to_send) < 0)
{
}
bytes_left_to_send = bytes_left_to_send - bytes_left_to_send;
memset(tempbuff, '\0', BUF_SIZE);
}
}
}
else
{
// READING EACH BYTE AND PASSING IT TO SENDBUFF
for (i = 0; i < num_of_bytes; i++)
{
fread(&sendbuff[i], 1, 1, fp);
}
// SENDING BYTES
while (write(connfd, sendbuff, num_of_bytes) < 0)
{
}
}
// FREEING MEMORY
fclose(fp);
free(sendbuff);
printf("\nClient #%d --> File sent\n", client_number);
}
// RECEIVING BYTES
void recv_bytes(int connfd, int client_number, char **command_parts)
{
int bytes_read = 0, bytes_read_in_total = 0, i = 0;
char *num_of_bytes = malloc(10 * sizeof(char));
char *recvbuff = NULL;
FILE *fp;
fp = fopen(command_parts[2], "w");
// RECEIVING BYTES TO RECEIVE
while (read(connfd, num_of_bytes, 10) < 0)
{
}
printf("\nClient #%d --> Size of file received: %d\n", client_number, atoi(num_of_bytes));
recvbuff = malloc(atoi(num_of_bytes) * sizeof(char)); /* ALLOCATING recvbuff WITH RECEIVED SIZE */
memset(recvbuff, '\0', atoi(num_of_bytes));
// IF BYTES MORE THAN 512 RECEIVING IT IN PARTS
if(atoi(num_of_bytes) > BUF_SIZE)
{
int bytes_left_to_read = atoi(num_of_bytes);
char *tempbuff = calloc(BUF_SIZE, sizeof(char));
while(bytes_left_to_read > 0)
{
if(bytes_left_to_read >= BUF_SIZE)
{
// READING BYTES
while (bytes_read = read(connfd, tempbuff, BUF_SIZE) < 0)
{
}
bytes_read = strlen(tempbuff);
// WRITING BYTES READ SO FAR TO THE FILE
for (i = 0; i < bytes_read; i++)
{
fwrite(&tempbuff[i], 1, 1, fp);
}
bytes_left_to_read = bytes_left_to_read - bytes_read;
memset(tempbuff, '\0', BUF_SIZE);
}
else
{
// READING BYTES
while (bytes_read = read(connfd, tempbuff, bytes_left_to_read) < 0)
{
}
bytes_read = strlen(tempbuff);
// WRITING BYTES READ SO FAR TO THE FILE
for (i = 0; i < bytes_read; i++)
{
fwrite(&tempbuff[i], 1, 1, fp);
}
bytes_left_to_read = bytes_left_to_read - bytes_read;
memset(tempbuff, '\0', BUF_SIZE);
}
}
}
else
{
// RECEIVING BYTES
while (bytes_read_in_total < atoi(num_of_bytes))
{
while (bytes_read = read(connfd, recvbuff, atoi(num_of_bytes)) < 0)
{
}
bytes_read = strlen(recvbuff);
bytes_read_in_total = bytes_read_in_total + bytes_read;
// WRITING BYTES READ SO FAR TO THE FILE
for (i = 0; i < bytes_read; i++)
{
fwrite(&recvbuff[i], 1, 1, fp);
}
memset(recvbuff, '\0', atoi(num_of_bytes));
}
}
free(recvbuff);
fclose(fp);
printf("\nClient #%d --> File received\n", client_number);
}

Related

Changing STDOUT to file in ncat source code

I managed to compile ncat. I am using -k option to keep server open. Instead of accepting data to STDOUT, my goal is to write to files instead. So far I was able to write to a file instead of STDOUT but my goal is to loop through new files on each new connection. Right now it is appending to the same filename_0 and f++ is not incrementing. Here is what I have so far. The original code will be below. The difference is in the else clause, basically if n is actually greater than 0. On each loop, n is 512 bytes until the last chunk. I just want to be able to have new files from each new connection. filename_0, filename_1, filename_3, etc.
MODIFIED CODE:
/* Read from a client socket and write to stdout. Return the number of bytes
read from the socket, or -1 on error. */
int read_socket(int recv_fd)
{
char buf[DEFAULT_TCP_BUF_LEN];
struct fdinfo *fdn;
int nbytes, pending;
int f = 0;
fdn = get_fdinfo(&client_fdlist, recv_fd);
ncat_assert(fdn != NULL);
nbytes = 0;
do {
int n, s;
n = ncat_recv(fdn, buf, 512, &pending);
if (n <= 0) {
if (o.debug)
logdebug("Closing fd %d.\n", recv_fd);
#ifdef HAVE_OPENSSL
if (o.ssl && fdn->ssl) {
if (nbytes == 0)
SSL_shutdown(fdn->ssl);
SSL_free(fdn->ssl);
}
#endif
close(recv_fd);
checked_fd_clr(recv_fd, &master_readfds);
rm_fd(&client_fdlist, recv_fd);
checked_fd_clr(recv_fd, &master_broadcastfds);
rm_fd(&broadcast_fdlist, recv_fd);
conn_inc--;
if (get_conn_count() == 0)
checked_fd_clr(STDIN_FILENO, &master_readfds);
return n;
}
else {
char filename[20];
snprintf(filename, sizeof(char) * 20, "filename_%i", f);
FILE *fp = fopen(filename, "a");
if (fp == NULL)
{
printf("Could not open file");
return 0;
}
//Write(STDOUT_FILENO, buf, n);
s = fwrite(buf, 1, n, fp);
fclose(fp);
f++;
nbytes += n;
}
} while (pending);
return nbytes;
}
ORIGINAL CODE:
int read_socket(int recv_fd)
{
char buf[DEFAULT_TCP_BUF_LEN];
struct fdinfo *fdn;
int nbytes, pending;
fdn = get_fdinfo(&client_fdlist, recv_fd);
ncat_assert(fdn != NULL);
nbytes = 0;
do {
int n;
n = ncat_recv(fdn, buf, sizeof(buf), &pending);
if (n <= 0) {
if (o.debug)
logdebug("Closing fd %d.\n", recv_fd);
#ifdef HAVE_OPENSSL
if (o.ssl && fdn->ssl) {
if (nbytes == 0)
SSL_shutdown(fdn->ssl);
SSL_free(fdn->ssl);
}
#endif
close(recv_fd);
checked_fd_clr(recv_fd, &master_readfds);
rm_fd(&client_fdlist, recv_fd);
checked_fd_clr(recv_fd, &master_broadcastfds);
rm_fd(&broadcast_fdlist, recv_fd);
conn_inc--;
if (get_conn_count() == 0)
checked_fd_clr(STDIN_FILENO, &master_readfds);
return n;
}
else {
Write(STDOUT_FILENO, buf, n);
nbytes += n;
}
} while (pending);
return nbytes;
}
I was able to figure out using the other functions involved. i passed a pointer into this function to write to it. the handler is a function i added the open() file pointer to.

how to send image files through http?

I'm learning the socket programming and trying to write a simple http server with c. My program can load html/css/javascript files correctly, but the image files can't be loaded. For example, the website icon favicon.ico and <img> of the html file always failed to load. I'm using the code as below to build my simple server:
server.c:
#define CYAN(format, ...) \
printf("\033[1;36m" format "\33[0m\n", ## __VA_ARGS__)
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
socklen_t c_addr_size;
int s_sock;// server socket
int c_sock;// clinet socket
char buf[4096];// user agent
char msg[4096];// file content
char head[1024];// http header
char file[128];// which file requested
void init_server();
void read_request();
void send_file();
int main()
{
init_server();
while (1) {
c_sock = accept(s_sock, NULL, NULL);
if (c_sock != -1) {
int nread = recv(c_sock, buf, sizeof(buf), 0);
read_request();// TODO
CYAN("%d", nread);
CYAN("%s", buf);
send_file();
close(c_sock);
}
}
close(s_sock);
return 0;
}
void init_server()
{
s_sock = socket(AF_INET, SOCK_STREAM, 0);
assert(s_sock != -1);
s_addr.sin_family = AF_INET;
s_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
s_addr.sin_port = htons(8000);
int res = bind(s_sock, (struct sockaddr*)&s_addr, sizeof(s_addr));
if (res == -1) { perror("cannot bind"); exit(-1); }
listen(s_sock, 10);// TODO
c_addr_size = sizeof(c_addr);
}
void read_request()
{
int buf_len = strlen(buf);
int i = 0, j = 0;
for (i = 0; i < buf_len - 10; i ++) {
if (buf[i] == 'G' && buf[i + 1] == 'E' && buf[i + 2] == 'T') {// `GET` keyword
i = i + 4;// skip space
while (buf[i] != ' ') {
file[j] = buf[i];
j ++, i ++;
}
file[j] = '\0';
CYAN("%s", file);
return;
}
}
}
void send_file()
{
if (strcmp(file, "/") == 0) {
sprintf(file, "index.html");
is_html = 1;
} else {
sprintf(file, "%s", file + 1);// skip `/`
}
FILE *fp = fopen(file, "r");
// count file length
int file_len = 0;
while (fgets(msg, 1000, fp)) {// read by lines
file_len += strlen(msg);
}
// send http header
sprintf(head,
"HTTP/1.1 200 OK\n"
// "Content-Type: text/html\n"
"Content-Length: %d\n"
"\n", file_len
);
// send file content
CYAN("%d", fseek(fp, 0, SEEK_SET));
memset(msg, 0, sizeof(msg));
send(c_sock, head, strlen(head), 0);
while (fgets(msg, 1000, fp)) {// read by lines
send(c_sock, msg, strlen(msg), 0);
}
fclose(fp);
}
I'm not familiar with http, and I don't know whether I should change the content of the http header when sending images files. How to correct my code, can anyone help me?
FILE *fp = fopen(file, "r");
// count file length
int file_len = 0;
while (fgets(msg, 1000, fp)) {// read by lines
file_len += strlen(msg);
}
No. No. No. No. Never use text processing on binary data.
File length is easy.
FILE *fp = fopen(file, "rb");
fseek(fp, 0, SEEK_END);
long file_len = ftell(fp);
fseek(fp, 0, SEEK_SET);
Now to send it:
send_helper(c_sock, head, strlen(head), 0); /* send header */
while (file_len > 4096) {
int delta = fread(msg, 4096, 1, fp);
if (delta == 0) {
/* handle error */
fclose(fp);
/* need to refactor here; c_sock is useless and needs to be closed */
return;
}
send_helper(c_sock, msg, delta);
file_len -= delta;
}
if (file_len > 0) {
/* last chunk; fread only returns a short count on an actual error so no loop here */
int delta = fread(msg, 4096, 1, fp);
if (delta == 0) {
/* handle error */
fclose(fp);
/* need to refactor here; c_sock is useless and needs to be closed */
return;
}
send_helper(c_sock, msg, delta);
}
fclose(fp);
Unlike fread, send needs a loop to ensure all bytes are sent.
void send_helper(int c_sock, char *msg, size_t size)
{
while (size > 0)
{
ssize_t delta = sendto(c_sock, msg, size, 0);
/*
* not bothering to handle error well--
* we'll just error a few more times and drop out of loop anyway.
* You probably should come back and fix this later though
*/
if (delta <= 0) return;
size -= (size_t)delta;
msg += (size_t)delta;
}
}

In c socket, why my server can't receive the whole content?

I am new in this field, and writing one server and client, but it really confusing that I can't get all the content, but some small clip.
My server code:
read(connfd, name, 20);
//recv(connfd,name,1024,0);
char* a=name;
while(a[0]!='\n'){
a++;
}
a[0]='\0';
printf("name:%s\n", name);
read(connfd, size, 20);
printf("size:%s\n", size);
recv(connfd,buf,8192,0);
printf("buf:%s\n", buf);
if((stream = fopen(name,"w+t"))==NULL){
printf("The file was not opened! \n");
}
int write_length = fwrite(buf,sizeof(char),8192,stream);
bzero(buf,8192);
if(put){
char *res="OK\n";
write(connfd, res, 1024);
}
fclose(stream);
and my client code is:
char buffer[8192];
bzero(buffer,8192);
char * put="PUT\n";
if ((write(fd, put, 8192)) <= 0) {
if (errno != EINTR) {
fprintf(stderr, "Write error: %s\n", strerror(errno));
exit(0);
}
}
struct stat st ;
stat( put_name, &st );
char str[100];
sprintf(str, "%d", st.st_size);
int len;
char *current=NULL;
len=strlen(put_name);
char sendname[1024];
strcpy(sendname,put_name);
strcat(sendname,"\n");
write(fd, sendname, 10);
strcat(str,"\n");
write(fd, str, 10);
FILE *stream;
if((stream = fopen(put_name,"r"))==NULL)
{
printf("The file was not opened! \n");
exit(1);
}
int lengsize = 0;
while((lengsize = fread(buffer,1,8192,stream)) > 0){
if(send(fd,buffer,8192,0)<0){
printf("Send File is Failed\n");
break;
}
bzero(buffer, 8192);
}
Now, I can send all content, but can receive part of them. for example, on my mac, server can receive name but the str is neglected, when I printf the str in the server, it shows the content of file. and the content of file is not the whole file content. Some content disappear. Could you tell me why?
The read and write functions are not guaranteed to send or receive the entire message with a single call. Instead, you're expected to sit in a loop, writing the message incrementally until everything has been sent and reading everything incrementally until everything has been read. For example, if you know exactly how much has been sent, you can do this:
char recvBuffer[BUFFER_SIZE];
int bytesRead = 0;
while (bytesRead < BUFFER_SIZE) {
int readThisTime = read(file, recvBuffer + bytesRead, BUFFER_SIZE - bytesRead);
if (readThisTime == -1) {
// handle error...
}
bytesRead += readThisTime;
}
If you don't know exactly how much has been sent, try this:
char recvBuffer[BUFFER_SIZE];
int bytesRead = 0;
while (bytesRead < BUFFER_SIZE) {
int readThisTime = read(file, recvBuffer + bytesRead, BUFFER_SIZE - bytesRead);
if (readThisTime == -1) {
// handle error...
}
if (readThisTime == 0) break; // Done!
bytesRead += readThisTime;
}
You are ignoring the return values of send() and recv(). You MUST check return values!
When sending the file, lengsize receives how many bytes were actually read from the file. Your client is sending too many bytes when lengsize is < 8192 (typically the last block of the file if the file size is not an even multiple of 8192).
But more importantly, although the client is telling the server the file size, the server is ignoring it to know when to stop reading. The server is also ignoring the return value of recv() to know how many bytes were actually received so it knows how many bytes can safely be written to the output file.
Try something more like this instead:
common:
int readData(int s, void *buf, int buflen)
{
int total = 0;
char *pbuf = (char*) buf;
while (buflen > 0) {
int numread = recv(s, pbuf, buflen, 0);
if (numread <= 0) return numread;
pbuf += numread;
buflen -= numread;
total += numread;
}
return total;
}
int sendData(int s, void *buf, int buflen)
{
int total = 0;
char *pbuf = (char*) buf;
while (buflen > 0) {
int numsent = send(s, pbuf, buflen, 0);
if (numsent <= 0) return numsent;
pbuf += numsent;
buflen -= numsent;
total += numsent;
}
return total;
}
int readInt32(int s, int32_t *value)
{
int res = readData(s, value, sizeof(*value));
if (res > 0) *value = ntohl(*value);
return res;
}
int sendInt32(int s, int32_t value)
{
value = htonl(value);
return sendData(s, &value, sizeof(value));
}
char* readStr(int s)
{
int32_t size;
if (readInt32(s, &size) <= 0)
return NULL;
char *str = malloc(size+1);
if (!str)
return NULL;
if (readData(s, str, size) <= 0) {
free(str);
return NULL;
}
str[size] = '\0';
return str;
}
int sendStr(int s, const char *str)
{
int len = strlen(str);
int res = sendInt32(s, len);
if (res > 0)
res = sendData(s, str, len);
return res;
}
server:
char buffer[8192];
char *name = readStr(connfd);
if (!name) {
// error handling ...
sendStr(connfd, "Socket read error");
return;
}
printf("name:%s\n", name);
int32_t filesize;
if (readInt32(connfd, &filesize) <= 0) {
// error handling ...
free(name);
sendStr(connfd, "Socket read error");
return;
}
printf("size:%d\n", filesize);
if ((stream = fopen(name, "wb")) == NULL) {
// error handling ...
printf("The file was not opened!\n");
free(name);
sendStr(connfd, "File not opened");
return;
}
while (filesize > 0) {
int numread = readData(connfd, buf, min(filesize, sizeof(buffer)));
if (numread <= 0) {
// error handling ...
close(stream);
free(name);
sendStr(connfd, "Socket read error");
return;
}
printf("buf:%.*s\n", numread, buf);
if (fwrite(buf, 1, numread, stream) != numread) {
// error handling ...
close(stream);
free(name);
sendStr(connfd, "File write error");
return;
}
filesize -= numread;
}
fclose(stream);
free(name);
sendStr(connfd, "OK");
client:
char buffer[8192];
struct stat st;
if (stat( put_name, &st ) != 0) {
// error handling ...
exit(0);
}
if ((stream = fopen(put_name, "rb")) == NULL) {
// error handling ...
printf("The file was not opened!\n");
exit(0);
}
if (sendStr(fd, put_name) <= 0) {
// error handling ...
close(stream);
exit(0);
}
int32_t filesize = st.st_size;
if (sendInt32(fd, filesize) <= 0) {
// error handling ...
close(stream);
exit(0);
}
int lengsize;
while (filesize > 0) {
lengsize = fread(buffer, 1, min(filesize , sizeof(buffer)), stream);
if (lengsize <= 0) {
printf("Read File Failed\n");
// error handling ...
close(stream);
exit(0);
}
if (sendData(fd, buffer, lengsize) <= 0) {
printf("Send File Failed\n");
// error handling ...
close(stream);
exit(0);
}
filesize -= lengsize;
}
close(stream);
char *resp = readStr(fd);
if (!resp) {
// error handling ...
exit(0);
}
if (strcmp(resp, "OK") == 0)
printf("Send File OK\n");
else
printf("Send File Failed: %s\n", resp);
free(resp);

I read more than I write in file

I have a file, partitioned in fixed sized blocks. I am copying a test_file.txt into the 3rd block of the file. I read and copied 18 bytes.
Then I am trying to copy from the file that very same .txt file I just imported to a newly created .txt, but I am writing 256 bytes to the new file. Moreover, when I try to read it, it is full of garbage.
The first function is used to import the .txt and the second one to export it.
void copy_file(int mfs_desc, char* filename, Superblock* s, MDS mds) {
if(mds.size == 0)
return;
char buffer[s->block_size];
int i = 0;
for (; i < s->block_size; ++i) {
buffer[i] = '\0';
}
int source_desc = open(filename, O_RDONLY);
// error handling
if (source_desc == -1) {
perror("opening file in copy file");
exit(1);
}
ssize_t nread;
int total = 0;
off_t seek = lseek(mfs_desc,
sizeof(Superblock) + mds.datablocks[0] * s->block_size,
SEEK_SET);
printf("offset = %d\n", mds.datablocks[0]);
if (seek < 0) {
perror("seek");
exit(1);
}
total = 0;
while ((nread = read(source_desc, buffer, s->block_size)) > 0) {
total += nread;
write(mfs_desc, buffer, s->block_size);
}
printf("read and copied: %d bytes\n", total);
if (close(source_desc) == -1) {
perror("closing file in copy file");
exit(1);
}
}
int copy_file_export(int mfs_desc, char* filename, Superblock s, MDS mds) {
if(mds.size == 0) {
printf("File is empty, abort\n");
return 0;
}
char buffer[s.block_size];
int i = 0;
for (; i < s.block_size; ++i) {
buffer[i] = '\0';
}
int destination_desc = open(filename, O_CREAT | O_WRONLY);
// error handling
if (destination_desc == -1) {
printf("filename = |%s|\n", filename);
perror("opening file in copy file export");
exit(1);
}
ssize_t nread;
int total = 0;
off_t seek = lseek(mfs_desc,
sizeof(Superblock) + mds.datablocks[0] * s.block_size,
SEEK_SET);
printf("offset = %d\n", mds.datablocks[0]);
if (seek < 0) {
perror("seek");
exit(1);
}
for(i = 0; i < mds.size; ++i) {
nread = read(mfs_desc, buffer, s.block_size);
total += nread;
write(destination_desc, buffer, nread);
}
printf("wrote: %d bytes\n", total);
if (close(destination_desc) == -1) {
perror("closing file in copy file");
exit(1);
}
return 1;
}
Output:
import test_file.txt ... / <-- just a command
offset = 2
read and copied: 18 bytes
export test_file.txt ... ../../ <-- just a command
offset = 2
wrote: 256 bytes
What I am doing wrong?
I would replace
write(mfs_desc, buffer, s->block_size);
with
write(mfs_desc, buffer, nread);
In this chunk of code:
while ((nread = read(source_desc, buffer, s->block_size)) > 0) {
total += nread;
write(mfs_desc, buffer, s->block_size);
}
You're very likely handling the last write() incorrectly. You need to write only the bytes you read.
write(mfs_desc, buffer, nread);
Also, these lines are most likely bogus:
char buffer[s->block_size];
char buffer[s.block_size];
You're trying to use a variable sized allocation for an array on the stack. You Can't Do That™. Those allocations have to be fixed (compile time constant) sized.

Unable to send whole file over TCP connection! (UNIX C)

So I programmed a multi threaded web server, here is one function from the program. This function takes output file descriptor (fd), content type, pointer to data to be served (*buf) and size of the data (numbytes). It always gets stuck at 5775 bytes! I've tried using write() instead of send(), but no avail! I tried to send whole buf at a time, and even tried to transfer it in chunks, but wget shows that it gets stck at 5775 bytes! Here is the code:
int return_result(int fd, char *content_type, char *buf, int numbytes)
{
char out_buf[BUF_SIZE], numb[6];
int buf_len, total = 0, buf_size;
long int i = 0;
sprintf(numb, "%d", numbytes);
strcpy(out_buf, "HTTP/1.1 200 OK \nContent-Type: ");
strcat(out_buf, content_type);
strcat(out_buf, "\nContent-Length: ");
strcat(out_buf, numb);
strcat(out_buf, "\nConnection: Close\n \n");
printf("\nSending HTTP Header\n %d bytes sent!",
send(fd, out_buf, strlen(out_buf), 0));
char *start = NULL, *str = NULL, *temp = NULL;
start = buf;
printf("\n Start Pointer Val = %ld", &start);
while (start != NULL) {
printf("\n While Loop");
if (i + 2048 * sizeof(char) < numbytes) {
printf("\n If 1");
str = (char *)malloc(sizeof(char) * 2048);
memcpy(str, start, sizeof(char) * 2048);
i = i + 2048 * sizeof(char);
buf_size = send(fd, str, 2048, 0);
free(str);
printf("\n Sent %d bytes total : %d", buf_size, total =
total + buf_size);
temp = start + sizeof(char) * 2048;
start = temp;
} else {
i = numbytes - i * sizeof(char);
if (i > 0) {
printf("\n If 2");
printf("\n Value of i %d", i);
str = (char *)malloc(sizeof(char) * i);
memcpy(str, start, sizeof(char) * i);
printf("Total bytes finally sent:%d", total =
total + send(fd, str, i, 0));
if (total == numbytes) {
printf("\nTransfer Complete!");
}
free(str);
}
start = NULL;
}
}
printf("out of loop!");
return 0;
}
I'd like to suggest replacing your code with the following writen() function from Advanced Programming in the Unix Environment, 2nd edition:
ssize_t /* Write "n" bytes to a descriptor */
writen(int fd, const void *ptr, size_t n)
{
size_t nleft;
ssize_t nwritten;
nleft = n;
while (nleft > 0) {
if ((nwritten = write(fd, ptr, nleft)) < 0) {
if (nleft == n)
return(-1); /* error, return -1 */
else
break; /* error, return amount written so far */
} else if (nwritten == 0) {
break;
}
nleft -= nwritten;
ptr += nwritten;
}
return(n - nleft); /* return >= 0 */
}
This code is already debugged and known working, and further allows write(2) to write PIPE_BUF bytes at a go for better speed when things are working well.
send(2) should block if it cannot send all the data you have requested, though. I think more interesting would be debugging the version with plain send(2) without any of the surrounding efforts to break things into blocks.
Better than both write(2) and send(2) would be sendfile(2) -- open the file, pass the descriptor and socket to sendfile(2), and let the kernel handle it all for you, using zero-copy mechanisms if possible.
One last point: HTTP uses CRLF, not plain carriage returns. Each \n should be replaced with \r\n.
Try something like this (printf() statements omitted for clarity):
int send_buf(in fd, void *buf, int numbytes)
{
char *start = (char*) buf;
while (numbytes > 0)
{
int sent = send(fd, start, numbytes, 0);
if (sent <= 0)
{
if ((sent == -1) && (errno == EAGAIN))
{
fd_set wfds;
FD_ZERO(&wfds);
FD_SET(fd, &wfds);
if (select(fd + 1, NULL, &wfds, NULL, NULL) == 1)
continue;
}
return -1;
}
start += sent;
numbytes -= sent;
}
return 0;
}
int return_result(int fd, char *content_type, void *buf, int numbytes)
{
char out_buf[BUF_SIZE],
int len = sprintf(out_buf,
"HTTP/1.1 200 OK\r\n"
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
"Connection: Close\r\n"
"\r\n",
content_type,
numb);
if (send_buf(fd, out_buf, len) != 0)
return -1;
if (send_buf(fd, buf, numbytes) != 0)
return -1;
return 0;
}

Resources