sendfile(string path)
		{
			int length;
		    char buffer[10];
			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 infoBuffer = infofile.str();

			 send(infoBuffer);

			while(!in.read(buffer , 1).eof())
			{
				cout <<::send(sock , buffer , 1 , 0);

			}
			cout << "File sent" << endl;
		}
receivefile(string path)
		{
			char buffer[10];
			string recvBuffer;
			ofstream in(path.c_str() , ios::binary);

			recv(recvBuffer);

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

			while(int i = ::recv(sock , buffer , 1 , 0) > 0)
			{
				cout << i;
				in.write(buffer , 1);
			}
			cout << "File received" << endl;
		}

my problem is that it sends and receives the file but only if i stop the program,if not it just remains stuck in the while loop in receive() function ....why is that ?

The recv() function will return <= 0 if either a socket error occurs or the socket has been closed on the server side. If none of these are true, it will just block, waiting for data. So, I'm guessing your problem is that you didn't close the socket.

If, for some reason, you wish to still have it open after the transfer, you should count the bytes you receive from recv() and exit the loop when you're done, like so:

int cbFileSize = ...;

			...

			int cbTotalReceived = 0;
			while(int i = ::recv(sock , buffer , 1 , 0) > 0)
			{
				cout << i;
				in.write(buffer , 1);
				cbTotalReceived += i;
				if( cbTotalReceived >= cbFileSize ) break;
			}

Also, on a side note, send larger batches of data instead of just 1 byte - it's much more efficient.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.