I am connecting to an arbitrary FTP server, such as ftp.freebsd.org. The initial response of the server is "220 Welcome to freebsd.isc.org.". If I just read the data without looping, the string prints out fine.
Would the solution of this be to put a timeout on the socket or something? Why isn't read() returning 0?
Thanks very much. Here is code.
- Code: Select all
//reads fully from a file or stream
void *readFully(int fd)
{
//will hold the bytes read from the socket
//data size is the size of the final data
ssize_t bytesRead, dataSize = BUF_LEN;
//will be the buffer for reading in the data
char buf[BUF_LEN];
//zero out the buffer
bzero(buf, BUF_LEN);
void *data = NULL;
data = chk_realloc(data, BUF_LEN);
//read reply from server
while ((bytesRead = read(fd, buf, BUF_LEN)) != 0) { //hangs here
//if read failed, print error and exit
if(bytesRead < 0)
{
perror("Error reading from server");
exit(1);
}
//zero out the buffer
bzero(buf, BUF_LEN);
//copy the data into the final pointer
memcpy(data, buf, BUF_LEN);
//increase the data size
dataSize += BUF_LEN;
//realloc the data
data = chk_realloc(data, dataSize);
}
return data;
}
