LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2010, 09:32 AM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
problem with receiving file c++


Code:
void Wsk::receivefile(string path)
	{
		char* buffer = new char;
		char* recvBuffer = new char[50] ;
		int byte_recv = 0;
		ofstream in(path.c_str() , ios::binary);

		::recv(sock , recvBuffer , 26 , 0);

		string result(recvBuffer);
		if(result.compare(0 , 4 , "info") == 0)
		{
			string name = getFileName(result);
			string size = getFileSize(result);
			cout << "File name is: " << name << endl;
			cout << "Size of file is: " << size << endl;
		}
		delete [] recvBuffer;

		while(((byte_recv = ::recv(sock , buffer , 1 , 0)) > 0))
		{
			cout << byte_recv;
			cout << endl;
			in.write(buffer , 1);
		}
		delete  buffer;
	}
Code:
void Wsk::sendfile(string path)
	{
		int length;
	    char* buffer = new char;
	    const char* infoBuffer ; //aici daca alocam memorie dinamic
		ifstream in(path.c_str() , ios::binary);
		in.seekg(0 , ios::end);
		length = in.tellg();

		in.seekg(0 , ios::beg);
		stringstream infofile;
		string name = splitname(path);
		infofile <<"info" << " " << name << " " << length;
		string result = infofile.str();
		infoBuffer = result.c_str();
		int size = strlen(infoBuffer);

		 ::send(sock , infoBuffer , size , 0);

		while(!in.read(buffer , 1).eof())
		{
			cout <<::send(sock , buffer , 1 , 0);
		}
		delete buffer;
	}
it sends the file and prints "File sent",and in the receiving function,the while reads all the data sent,but it doesnt reaches the line with "File received".....in debug,the line with i step into code,disapear after the file is received....confused
 
Old 03-07-2010, 03:39 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
How does the receiving end know that the end of file has been reached? Isn't it just waiting for more characters?
 
Old 03-07-2010, 04:42 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
vbx_wx -

This is exactly what I was trying to point out in my earlier post: with sockets, there IS NO NOTION OF "bounds":

http://www.linuxquestions.org/questi...0/#post3887246

When you read from a socket, there is NO "built-in" way to tell if you've reached "end-of-string". YOU have to provide it yourself (for example, by having the sender add a '\0' null character, and have the receiver look for it.

When you read from a socket, there is NO "built-in" way to check for end-of-record, or end-of-file. There isn't even a guaranteed way to distinguish between a true "end of file" and a coincidental "communications interrupted". YOU have to provide this yourself (for example, by sending a "length" value before you start sending the data itself.

Please definitely read Beej's Guide:

http://beej.us/guide/bgnet/

http://beej.us/guide/bgnet/output/ht...age/bgnet.html

http://beej.us/guide/bgnet/output/ht...t.html#sendall
- 7.3. Handling Partial send()s
- 7.5. Son of Data Encapsulation
... etc ...

'Hope that helps .. PSM

PS:
Please do click the "thanks" button - for troop, neonsignal, or any of us- if it did help.

Last edited by paulsm4; 03-07-2010 at 04:49 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Postfix: receiving problem Lexus45 Linux - Server 14 02-15-2010 04:01 PM
Problem receiving multicast icedown Linux - Networking 1 03-26-2009 09:09 PM
problem receiving email King4lex Slackware 1 09-20-2004 03:52 PM
receiving a file under instan messenger(Kopete) windowsrefugeeX Linux - Networking 0 04-17-2004 12:15 PM
Postfix No such file or directory error on receiving mail jimieee Linux - Software 1 03-26-2004 06:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration