UART read blocks after reading 33 bytes - c

I have made a C-program flash image to micro controller through uart.In this code,i am following a protocol,all things are going well.But when i load image into micro controller through uart,write is working fine but read blocked after reading 33 bytes.I am writing and reading byte by byte..declaration & definition of function to load image is written below:
Function declaration :
load_RAM_image(file_size, file_buff);
here file_size is unsigned int and its value is 40,980KB and file_buff is unsigned char pointer which points to image buffer.
Function declaration :
#define BYTE_WRITE 1
#define DELAY 10000
int load_RAM_image(unsigned int buff_size, unsigned char *buff)
{
unsigned int count, num_Wbytes, num_Rbytes, byte;
for (count = 0; count < buff_size;) /* increase count value to every time */
{
num_Wbytes = write( serial_fd, buff + count, BYTE_WRITE );
if (num_Wbytes < 0)
{
fputs("write failed!\n", stderr);
return -1;
}
tcdrain(serial_fd);
usleep(DELAY);
num_Rbytes = read( serial_fd, rbuff + count, BYTE_WRITE );
if (num_Rbytes < 0)
{
if (errno == EAGAIN)
{
printf("SERIAL EAGAIN ERROR\n");
return -EAGAIN;
}
else
{
printf("SERIAL read error %d %s\n", errno, strerror(errno));
}
}
printf("wbuf[ %d ] = %02x rbuff = %02x\n\n",
count /*+ byte*/, *(buff + count/* + byte*/), rbuff[count]);
/* Compare the received byte from BAM */
if (strncmp(buff + count, rbuff + count, BYTE_WRITE ) < 0)
{
printf("Error : RAM loading error(W != R)\n");
return -1;
}
count += BYTE_WRITE ;
printf("count = %d\n", count);
}
return 0;
}

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.

Libusb error for Input and Output under Ubuntu Linux - C programming for USB

I have made a C-program that should talk USB. But I have got an error -1 that says I have som Input/Output errors.
My C code is here below. I going to explain how it works. The function getDevices() print out the vendor ID and the product ID and also the device name. Remember those when you running the function getDevices().
Then you have to choose what device you want to connect. When you have choose the device, then you write in the vendor ID and product ID here.
#define USB_VENDOR_ID 1155
#define USB_PRODUCT_ID 14415 /* USB product ID used by the device */
After that, you need to run the function connectUSB(). It simply connect to your USB depening on which USB_VENDOR_ID and USB_PRODUCT_ID you have been written.
I have issues to read the incomming message. I got the error LIBUSB_ERROR_IO = -1 when I try to read.
So why does I get -1 error when I try to read? I have tried with:
Run the C-code as full administration rights, e.g root
Unmout the USB device inside the computer, not plug it out
Tested to have a USB reader software ON meanwhile I use this C-program
If you want to try my code, you have to install LibUSB. From debbased systems such as Debian, Ubuntu, Raspberry etc. it can be done by writing this command in the terminal.
sudo apt-get install libusb-1.0.0-dev libusb-1.0-0
My C-code below.
#include <stdio.h>
#include <stdlib.h>
#include <libusb-1.0/libusb.h>
#include <string.h>
libusb_context *CONTEXT; //a libusb session
libusb_device_handle *DEVICEHANDLE; //a device handle
libusb_device * DEVICE_POINTER; // a pointer to a device
libusb_device **ARRAY_OF_POINTERS_TO_DEVICE; // an array of pointers to devices
ssize_t NUMBER_OF_USB_DEVICES; // Initial zero devices
static uint8_t receiveBuf[64];
uint8_t transferBuf[64];
uint16_t counter = 0;
#define USB_ENDPOINT_IN 0x80 /* endpoint address */
#define USB_ENDPOINT_OUT 0x01 /* endpoint address */
#define USB_TIMEOUT 3000 /* Connection timeout (in ms) */
#define USB_VENDOR_ID 1155
#define USB_PRODUCT_ID 14415 /* USB product ID used by the device */
/*
* Here we are going to read the USB
*/
int readUSB() {
int nread, r, counter = 0;
nread = 4;
int transfered;
unsigned char data[4];
r = libusb_bulk_transfer(DEVICEHANDLE, USB_ENDPOINT_IN, data, nread, &transfered, USB_TIMEOUT);
if (LIBUSB_ERROR_TIMEOUT == r) {
printf("LIBUSB_ERROR_TIMEOUT = %d\n", r);
return -1;
}else if(LIBUSB_ERROR_PIPE == r){
printf("LIBUSB_ERROR_PIPE = %d\n", r);
return -1;
}else if(LIBUSB_ERROR_OVERFLOW == r){
printf("LIBUSB_ERROR_OVERFLOW = %d\n", r);
return -1;
}else if(LIBUSB_ERROR_NO_DEVICE == r){
printf("LIBUSB_ERROR_NO_DEVICE = %d\n", r);
return -1;
}else if(LIBUSB_ERROR_IO == r){
printf("LIBUSB_ERROR_IO = %d\n", r);
return -1;
} else {
printf("r = %d, %d receive %d bytes from device: %s\n", r, ++counter, nread, data);
return 0;
}
return 0;
}
/*
* Here are we going to write to the USB
*/
int writeUSB() {
int n, ret;
uint16_t count = 0;
//count up
n = sprintf(transferBuf, "%d\0", count++);
//write transfer
//probably unsafe to use n twice...
ret = libusb_bulk_transfer(DEVICEHANDLE, USB_ENDPOINT_OUT, transferBuf, n,
&n, USB_TIMEOUT);
//Error handling
switch (ret) {
case 0:
printf("send %d bytes to device\n", n);
return 0;
case LIBUSB_ERROR_TIMEOUT:
printf("ERROR in bulk write: %d Timeout\n", ret);
break;
case LIBUSB_ERROR_PIPE:
printf("ERROR in bulk write: %d Pipe\n", ret);
break;
case LIBUSB_ERROR_OVERFLOW:
printf("ERROR in bulk write: %d Overflow\n", ret);
break;
case LIBUSB_ERROR_NO_DEVICE:
printf("ERROR in bulk write: %d No Device\n", ret);
break;
default:
printf("ERROR in bulk write: %d\n", ret);
break;
}
return -1;
return 0;
}
/*
* This will connect to our USB device
*/
int connectUSB() {
int r;
r = libusb_init(&CONTEXT);
if(r > 0){
printf("Libusb_init error %d\n", r);
return -1;
}
//libusb_set_debug(CONTEXT, 0);
//Open Device with VendorID and ProductID
DEVICEHANDLE = libusb_open_device_with_vid_pid(CONTEXT, USB_VENDOR_ID, USB_PRODUCT_ID);
if (DEVICEHANDLE == NULL) {
perror("DEVICEHANDLE == NULL");
return -1;
}
//Claim Interface 0 from the device
r = libusb_claim_interface(DEVICEHANDLE, 0);
if (r == LIBUSB_ERROR_NOT_FOUND) {
fprintf(stderr, "LIBUSB_ERROR_NOT_FOUND = %d\n", r);
return -1;
}else if(r == LIBUSB_ERROR_BUSY){
fprintf(stderr, "LIBUSB_ERROR_BUSY = %d\n", r);
return -1;
}else if(r == LIBUSB_ERROR_NO_DEVICE){
fprintf(stderr, "LIBUSB_ERROR_NO_DEVICE = %d\n", r);
return -1;
}
printf("Interface claimed\n");
return 0;
}
/*
* This will show all the devices for USB and send it thru sockets
*/
int getDevices() {
/*
* Special local device handle for only get the name of the USB device
*/
libusb_device_handle *DEVICEHANDLE_NULL; //a device handle
/*
* Compute the number of USB
*/
int returnValue = libusb_init(NULL);
NUMBER_OF_USB_DEVICES = libusb_get_device_list(NULL,
&ARRAY_OF_POINTERS_TO_DEVICE);
/*
* Create our list of Vendor, Product and device
* Vendor and product are important for connect the USB and device is important for the user to see which USB to connect
*/
uint16_t vendor[NUMBER_OF_USB_DEVICES];
uint16_t product[NUMBER_OF_USB_DEVICES];
char device[NUMBER_OF_USB_DEVICES][256 * 2];
/*
* Loop thru all USB devices
*/
ssize_t deviceIndex = 0;
while (deviceIndex < NUMBER_OF_USB_DEVICES) {
/*
* Get the description of the USB device
*/
DEVICE_POINTER = ARRAY_OF_POINTERS_TO_DEVICE[deviceIndex];
struct libusb_device_descriptor deviceDescriptor;
returnValue = libusb_get_device_descriptor(DEVICE_POINTER,
&deviceDescriptor);
if (returnValue != LIBUSB_SUCCESS)
break;
/*
* Open the USB device with NULL. It's only because we want the name of the USB device
*/
DEVICEHANDLE_NULL = NULL;
returnValue = libusb_open(DEVICE_POINTER, &DEVICEHANDLE_NULL);
if (returnValue != LIBUSB_SUCCESS) {
/*
* There was an error. Not success.
*/
if (DEVICEHANDLE_NULL != NULL) {
libusb_close(DEVICEHANDLE_NULL);
DEVICEHANDLE_NULL = NULL;
}
/*
* Write as there was no info at all to display
*/
product[deviceIndex] = 0;
vendor[deviceIndex] = 0;
memcpy(device[deviceIndex], "-", 256 * 2 * sizeof(char));
deviceIndex++;
continue;
}
/*
* Get the string associated with iManufacturer index.
*/
const int STRING_LENGTH = 256;
unsigned char stringManufacturer[STRING_LENGTH];
unsigned char stringProduct[STRING_LENGTH];
char stringDeviceName[STRING_LENGTH * 2];
if (DEVICEHANDLE_NULL != NULL && deviceDescriptor.iManufacturer > 0) {
returnValue = libusb_get_string_descriptor_ascii(DEVICEHANDLE_NULL,
deviceDescriptor.iManufacturer, stringManufacturer,
STRING_LENGTH);
if (returnValue < 0)
break;
}
/*
* Get string associated with iProduct index.
*/
if (DEVICEHANDLE_NULL != NULL && deviceDescriptor.iProduct > 0) {
returnValue = libusb_get_string_descriptor_ascii(DEVICEHANDLE_NULL,
deviceDescriptor.iProduct, stringProduct, STRING_LENGTH);
if (returnValue < 0)
break;
}
/*
* Combine manufacturer and product
*/
strcpy(stringDeviceName, (char*) stringManufacturer);
strcat(stringDeviceName, " "); // a space only
strcat(stringDeviceName, (char*) stringProduct);
//printf("%s\n", stringDeviceName);
/*
* Save them all into arrays
*/
product[deviceIndex] = deviceDescriptor.idProduct;
vendor[deviceIndex] = deviceDescriptor.idVendor;
memcpy(device[deviceIndex], stringDeviceName, 256 * 2 * sizeof(char));
/*
* Close and try next one.
*/
if (DEVICEHANDLE_NULL != NULL) {
libusb_close(DEVICEHANDLE_NULL);
DEVICEHANDLE_NULL = NULL;
}
/*
* Next USB device
*/
deviceIndex++;
}
/*
* Print our result what we found and send them to socket
*/
for (int i = 0; i < 11; i++) {
printf("Name: %s\n", device[i]);
printf("Vendor: %u\n", vendor[i]);
printf("Product: %u\n\n", product[i]);
}
libusb_exit(NULL);
return 0;
}

recv() on socket by dynamically allocating space

I'm trying to get the source code of my website using c, I'm able to connect and everything but when I implement the recv() code, it only receives the last few bytes of the source code. I'd like to dynamically allocate space for the buffer to receive more using the C functions malloc and realloc.
This is the code I have so far:
char *buffer = NULL;
unsigned int i = 0;
unsigned long LEN = 200;
unsigned long cur_size = 0;
buffer = (char*)malloc(sizeof(char)*LEN);
do
{
if( status >= LEN )
{
cur_size += status;
buffer = (char*)realloc(buffer, cur_size);
}
status = recv(cSocket, buffer, LEN, 0);
if( status == 0 )
{
printf("Bye\n");
}
else if( status > 0 )
{
printf("%d\n", status);
}
else
{
printf("socket error=%d\n", WSAGetLastError());
break;
}
}while( status > 0 );
printf("%s\n", buffer);
It still doesn't print the whole source code. How should I go about this?
Pseudocode:
buffer = 'len chars';
loop:
if( status >= buffer ) buffer = 'resize to status chars';
status = recv(sock, buffer, len, 0);
end loop
As you resize the buffer in advance this needs to be reflected by its size. Which currently is not the case.
To fix this you could, for example, initialise cur_size with LEN by changing
unsigned long cur_size = 0;
to
unsigned long cur_size = LEN;
Assuming the fix above, you want to append to the buffer and not overwrite it with every call to recv().
To do so change this line
status = recv(cSocket, buffer, LEN, 0);
to be
status = recv(cSocket, buffer + cur_size - LEN, LEN, 0);
A more straight forward approach would be to not track the size of the buffer, but the number of bytes received and just always increase the buffer by a constant size.
Also the two calls to allocate memory can be replaced by one:
char *buffer = NULL;
unsigned long LEN = 200;
unsigned long bytes_received = 0;
unsigned long cur_size = 0;
int status = 0;
do
{
if (bytes_received >= cur_size)
{
char * tmp;
cur_size += LEN;
tmp = realloc(buffer, cur_size);
if (NULL == tmp)
{
fprintf(stderr, "realloc error=%d\n", WSAGetLastError());
break;
}
buffer = tmp;
}
status = recv(cSocket, buffer + bytes_received, LEN, 0);
if (status == 0)
{
printf("Bye\n");
}
else if (status > 0)
{
bytes_received += status;
printf("%d\n", status);
}
else /* < 0 */
{
fprintf(stderr, "socket error=%d\n", WSAGetLastError());
}
} while (status > 0);
printf("%s\n", buffer);
Well, after a bit of research, I came across this website and finally found what I was looking for.
Binary tides
Although it uses linux's fcntl, the windows equivalent is ioctlsocket which is used to set the socket's non-blocking mode.
To see the exact function, visit the website. I modified the version and set my socket to blocking mode.
int total_recv(SOCKET s)
{
int size_recv = 0, total_size = 0, block = 00;
char chunk[BUFLEN];
ioctlsocket(s, FIONBIO, (unsigned long*)&block); // set mode to block
// not necessary but clarification of function, mode is block by
// default
while( 1 )
{
memset(chunk, 0, BUFLEN);
if( ( size_recv = recv(s, chunk, BUFLEN, 0) ) == SOCKET_ERROR )
{
printf("Error receiving\n");
}
else if( size_recv == 0 )
{
break;
}
else
{
total_size += size_recv;
// i used file since console wouldn't show full source code
FILE *fp = NULL;
fp = fopen("source.txt", "a");
fprintf(fp, chunk);
fclose(fp);
}
}
return total_size;
}

Printing Partition Table - C program

I am trying to print a partition table using C programming language, everything seems to work fine: Opening and reading, but I don't understand why it is printing garbage values.
Here is the code:
struct partition
{
unsigned char drive;
unsigned char chs_begin[3];
unsigned char sys_type;
unsigned char chs_end[3];
unsigned char start_sector[4];
unsigned char nr_sector[4];
};
int main()
{
int gc = 0, i = 1, nr = 0, pos = -1, nw = 0;
int fd =0;
char buf[512] ;
struct partition *sp;
printf("Ok ");
if ( (fd = open("/dev/sda", O_RDONLY | O_SYNC )) == -1)
{
perror("Open");
exit(1);
}
printf("fd is %d \n", fd);
pos = lseek (fd, 0, SEEK_CUR);
printf("Position of pointer is :%d\n", pos);
if ((nr = read(fd, buf, sizeof(buf))) == -1)
{
perror("Read");
exit(1);
}
close(fd);
printf("Size of buf = %d\n and number of bytes read are %d ", sizeof(buf), nr);
if ((nw = write(1, buf, 64)) == -1)
{
printf("Write: Error");
exit(1);
}
printf("\n\n %d bytes are just been written on stdout\n", nw,"this can also be printed\n");
printf("\n\t\t*************Partition Table****************\n\n");
for (i=0 ; i<4 ; i++)
{
sp = (struct partition *)(buf + 446 + (16 * i));
putchar(sp -> drive);
}
return 0;
}
It is printing garbage instead of partition table.
I might have some basic understanding issues but I searched with Google for a long time but it did not really help. I also saw the source code of fdisk but it is beyond my understanding at this point. Could anyone please guide me? I am not expecting someone to clear my mistake and give me the working code. Just a sentence or two - or any link.
Try this:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct partition
{
unsigned char boot_flag; /* 0 = Not active, 0x80 = Active */
unsigned char chs_begin[3];
unsigned char sys_type; /* For example : 82 --> Linux swap, 83 --> Linux native partition, ... */
unsigned char chs_end[3];
unsigned char start_sector[4];
unsigned char nr_sector[4];
};
void string_in_hex(void *in_string, int in_string_size);
void dump_partition(struct partition *part, int partition_number);
void dump_partition(struct partition *part, int partition_number)
{
printf("Partition /dev/sda%d\n", partition_number + 1);
printf("boot_flag = %02X\n", part->boot_flag);
printf("chs_begin = ");
string_in_hex(part->chs_begin, 3);
printf("sys_type = %02X\n", part->sys_type);
printf("chs_end = ");
string_in_hex(part->chs_end, 3);
printf("start_sector = ");
string_in_hex(part->start_sector, 4);
printf("nr_sector = ");
string_in_hex(part->nr_sector, 4);
}
void string_in_hex(void *in_string, int in_string_size)
{
int i;
int k = 0;
for (i = 0; i < in_string_size; i++)
{
printf("%02x ", ((char *)in_string)[i]& 0xFF);
k = k + 1;
if (k == 16)
{
printf("\n");
k = 0;
}
}
printf("\n");
}
int main(int argc, char **argv)
{
int /*gc = 0,*/ i = 1, nr = 0, pos = -1/*, nw = 0*/;
int fd = 0;
char buf[512] ;
struct partition *sp;
int ret = 0;
printf("Ok ");
if ((fd = open("/dev/sda", O_RDONLY | O_SYNC)) == -1)
{
perror("Open");
exit(1);
}
printf("fd is %d\n", fd);
pos = lseek (fd, 0, SEEK_CUR);
printf("Position of pointer is :%d\n", pos);
if ((nr = read(fd, buf, sizeof(buf))) == -1)
{
perror("Read");
exit(1);
}
ret = close(fd);
if (ret == -1)
{
perror("close");
exit(1);
}
/* Dump the MBR buffer, you can compare it on your system with the output of the command:
* hexdump -n 512 -C /dev/sda
*/
string_in_hex(buf, 512);
printf("Size of buf = %d - and number of bytes read are %d\n", sizeof(buf), nr);
/*if ((nw = write(1, buf, 64)) == -1)
{
printf("Write: Error");
exit(1);
}
printf("\n\n%d bytes are just been written on stdout\nthis can also be printed\n", nw);
*/
//printf("\n\t\t*************Partition Table****************\n\n");
printf("\n\t\t*************THE 4 MAIN PARTITIONS****************\n\n");
/* Dump main partitions (4 partitions) */
/* Note : the 4 partitions you are trying to dump are not necessarily existing! */
for (i = 0 ; i < 4 ; i++)
{
sp = (struct partition *)(buf + 446 + (16 * i));
//putchar(sp->boot_flag);
dump_partition(sp, i);
}
return 0;
}

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