Click here to Skip to main content
15,890,882 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: process communication Pin
Hamid_RT10-Jun-09 23:32
Hamid_RT10-Jun-09 23:32 
General[Message Deleted] Pin
trioum11-Jun-09 0:09
trioum11-Jun-09 0:09 
GeneralRe: process communication Pin
Hamid_RT11-Jun-09 1:39
Hamid_RT11-Jun-09 1:39 
QuestionRe: process communication Pin
David Crow11-Jun-09 3:30
David Crow11-Jun-09 3:30 
QuestionPipe Problem Pin
ssm198411910-Jun-09 17:10
ssm198411910-Jun-09 17:10 
AnswerRe: Pipe Problem Pin
Randor 11-Jun-09 4:17
professional Randor 11-Jun-09 4:17 
GeneralRe: Pipe Problem Pin
ssm198411914-Jun-09 15:33
ssm198411914-Jun-09 15:33 
QuestionError packing files into buffer and sending them over network [modified] Pin
andrew_dk10-Jun-09 16:58
andrew_dk10-Jun-09 16:58 
I have a windows client application that connects to a Linux daemon, both written in C++. One of the functions simply sends files from a particular directory on the Linux server back to the windows client app. The client app then unpacks the buffer and writes the files. This all works perfectly as long as none of the files on the server are larger than around 1mb. If files larger than this are packed the daemon crashes in the call to send the data. I've done a binary dump into a file prior to attempting to send the data (ie. prior to a crash) and all the files are packed in there perfectly

The code to pack the files on the Linux server is (with all error checking removed for clarity):
bool ClassName::PackSoftwareUpdateBuffer(char* baseDirectory, unsigned char* pBuffer)
{
	int return_code;
	char buf[1024];
	dirent entry;
	dirent *result;
	struct stat st;
	DIR* dir;
	Directory *pDir = NULL;
	Directory *pCurr = NULL;
	Directory *pd = NULL;
	Directory *pCurrDir = NULL;
	int fd = 0;
	size_t bytesread;

	do
	{
		if(pCurrDir)
		{
			sprintf(buf, "%s/%s", baseDirectory, pCurrDir->DirectoryName);
			dir = opendir(buf);

			// Add a delimiter to the buffer so we know on the client
			// we have a new directory
			*pBuffer++ = '$';
			strcpy((char*)pBuffer, pCurrDir->DirectoryName);
			pBuffer += 199;
		}
		else
		{
			dir = opendir(baseDirectory);
		}

		for(return_code = readdir_r(dir, &entry, &result); result != NULL && return_code == 0; return_code = readdir_r(dir, &entry, &result))
		{
			// Add the path to the filename so we can 'stat' the file
			if(pCurrDir)
				sprintf(buf, "%s/%s/%s", baseDirectory, pCurrDir->DirectoryName, entry.d_name);
			else
				sprintf(buf, "%s/%s", baseDirectory, entry.d_name);

			stat(buf, &st))

			// Check if this is a directory
			if(S_ISDIR(st.st_mode))
			{
				if(pCurrDir)
				{
					sprintf(buf, "%s/%s", pCurrDir->DirectoryName, entry.d_name);
					pd = new Directory(buf);
				}
				else
					pd = new Directory(entry.d_name);


				if(pCurr)
					pCurr = pCurr->pNext = pd;
				else
					pDir = pCurr = pd;

			}
			else
			{
				// Add the file data to the buffer
				strcpy((char*)pBuffer, entry.d_name);
				SET_BUFFER_UINT32(pBuffer, 50, st.st_size);

				fd = open(buf, O_RDONLY);
				pBuffer += 54;

				read(fd, (void*)pBuffer, st.st_size);
				close(fd);
				pBuffer += st.st_size;
			}
		}

		closedir(dir);

		if(pCurrDir)
			pCurrDir = pCurrDir->pNext;
		else
			pCurrDir = pDir;

		if(!pCurrDir)
			break;

	}while(true);

	DeleteDirectoryStructs(pDir);
	return true;
}


Where a Directory is a struct declared/defined as:
// Directory information (used for software updates)
struct Directory
{
	char DirectoryName[1024];
	Directory *pNext;

	Directory(char* dirName)
	:pNext(NULL)
	{
		strcpy(DirectoryName, dirName);
	}
};


The SET_BUFFER_UINT32 macro simply packs a numeric value into the buffer in network byte order. The pBuffer value passed into the function is a pointer to dynamically allocated memory. This is allocated in 1 mb chunks and always well and truly exceeds the amount of file data packed into the buffer (I have checked and re-checked this as it seemed the obvious reason for the issue). The crash occurs in the following function in the first call to send:

int ClassName::Write(unsigned char *buffer, unsigned int size)
{
	int num = 0;
	char *bufP = (char *)buffer;
	unsigned int total = 0;

	do
	{
		if((num = send(m_socket, (bufP + total), (size - total), 0)) <= 0)
		{
			// Premature EOF or error
			return num;
		}
		total += (unsigned int)num;
	}
	while (total < size);

	return total;
}

The buffer is the same buffer passed into the function to pack the data. If I comment out the call to read in the PackSoftwareUpdateBuffer function no crash occurs. Likewise if I set it so that only files less then one mb are read no crash occurs. In both cases the pBuffer pointer had the file size added to it, the same amount of data was sent if all the files were read, and the files were all successfully recreated on the client (albeit, filled with zeroes for the large files).

Bizarrely if I move the line: pBuffer += st.st_size; to above the call to read no crash occurs?!? This suggests to me there is something elementary I am completely missing. Any pointers much appreciated.

modified on Thursday, June 11, 2009 12:18 AM

AnswerRe: Error packing files into buffer and sending them over network Pin
Ozer Karaagac10-Jun-09 20:00
professionalOzer Karaagac10-Jun-09 20:00 
GeneralRe: Error packing files into buffer and sending them over network Pin
andrew_dk10-Jun-09 20:41
andrew_dk10-Jun-09 20:41 
GeneralRe: Error packing files into buffer and sending them over network Pin
Ozer Karaagac11-Jun-09 0:52
professionalOzer Karaagac11-Jun-09 0:52 
GeneralRe: Error packing files into buffer and sending them over network Pin
andrew_dk11-Jun-09 10:10
andrew_dk11-Jun-09 10:10 
GeneralRe: Error packing files into buffer and sending them over network [modified] Pin
andrew_dk11-Jun-09 11:38
andrew_dk11-Jun-09 11:38 
QuestionMFC Feature Pack Pin
kedanz10-Jun-09 15:48
kedanz10-Jun-09 15:48 
QuestionWhere is printf? Pin
Gjm10-Jun-09 15:05
Gjm10-Jun-09 15:05 
AnswerRe: Where is printf? Pin
«_Superman_»10-Jun-09 15:25
professional«_Superman_»10-Jun-09 15:25 
GeneralRe: Where is printf? Pin
Gjm10-Jun-09 15:32
Gjm10-Jun-09 15:32 
QuestionRe: Where is printf? Pin
David Crow10-Jun-09 15:51
David Crow10-Jun-09 15:51 
GeneralRe: Where is printf? Pin
CPallini10-Jun-09 21:24
mveCPallini10-Jun-09 21:24 
QuestionWrong help file afxhelp.hm path in VC 6.0 SOLVED Pin
Vaclav_10-Jun-09 10:00
Vaclav_10-Jun-09 10:00 
AnswerRe: Wrong help file afxhelp.hm path in VC 6.0 Pin
Randor 10-Jun-09 10:42
professional Randor 10-Jun-09 10:42 
GeneralRe: Wrong help file afxhelp.hm path in VC 6.0 Pin
Vaclav_11-Jun-09 7:27
Vaclav_11-Jun-09 7:27 
QuestionSharing a singleton in an executable and a runtime DLL Pin
Dustin Henry10-Jun-09 6:50
Dustin Henry10-Jun-09 6:50 
AnswerRe: Sharing a singleton in an executable and a runtime DLL Pin
Stuart Dootson10-Jun-09 8:40
professionalStuart Dootson10-Jun-09 8:40 
GeneralRe: Sharing a singleton in an executable and a runtime DLL Pin
Dustin Henry11-Jun-09 11:19
Dustin Henry11-Jun-09 11:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.