Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I was trying to send a video file from Android to C using TCP/IP protocol. I was having data loss(some bytes were lost in the transmission).

After i used gets() i am able to receive all the data, irrespective of the file size.

[edit] Added code from comment below:[/edit]
C++
int receive_text(int new_socket)
{
	long int buffersize = 0, recv_size = 0, size = 0, read_size, write_size;
	int errno;
	FILE *text;
	char *pBuf,a[50],filename[50],*filename2;
	printf("Enter Filename:\n");
	gets(filename);
	printf("%s",filename);
	strcat(str,filename);
	filename2 = fopen(str,"w");
	printf("Filename2:\n");
	(filename2);	
	text = fopen(str,"w");
 	if (text == NULL)
	{
		puts("Error has occurred. Text file could not be opened \n");
		gets(a);
		return -1;
	}

 	//Loop while we have not received the entire file yet
	
	while (1)
	{ 
		
		ioctl(new_socket, FIONREAD, &buffersize);//We check to see if there is data to be read from the socket
		printf("Buffersize ioctl value is %ld \n",buffersize);
		gets(a); 		
		if (buffersize > 0)
		{
			printf("Buffersize value is: %ld \n", buffersize);
			gets(a);
			pBuf = malloc(buffersize);
			if (!pBuf)
			{
				fprintf(stderr, "Memory Error. Cannot allocate!\n");
				gets(a);
				exit(-1);
			}				
			read_size = recv(new_socket, pBuf, buffersize, 0); 
			printf("Read size value is: %li \n", read_size);
			gets(a);				
			if (read_size < 1)
			{
				printf("%s", strerror(errno));
				gets(a);
				goto free;
			}
 				
			//Write the currently read data into our text file
					
			write_size = fwrite(pBuf, 1, read_size, text);		
			free(pBuf);
			printf("Writesize value is: %li \n", write_size);
			gets(a);	

			//Increment the total number of bytes read
				
			recv_size += read_size;		
			printf("Receivedsize value is: %li \n", recv_size);
			gets(a);
		}
		if((buffersize == 0) && (recv_size != 0))
		{
			goto free;
		}
	}
	free:

	//close(new_socket);
	fclose(text);
	send_text(new_socket);
	printf("Text successfully received: \n");
	gets(a);
	return 0;

}


The code has been put for further reference
Posted
Updated 13-Jul-15 1:25am
v2
Comments
[no name] 13-Jul-15 1:20am    
Question not decipherable.
venkat28vk 13-Jul-15 1:45am    
I am receiving video file from Android to C using TCP/IP protocol. In the initial phases there were data loss when video file was sent, i rectified the error by giving gets() and now there is no data loss. How did gets() solved the error.
[no name] 13-Jul-15 1:48am    
Without your code how do we know what you are talking about.
venkat28vk 13-Jul-15 6:13am    
Just check for the code
Stefan_Lang 13-Jul-15 3:01am    
What you wrote here is like going to the garage and telling the mechanic: "My green car is broken. The red works, though", without bringing either car.

What is the mechanic supposed to do without seeing the broken car or any other information to work with? What are we to do without seeing the code and any information to work with?

Never use gets() for reading binary data: it will swallow '\n' bytes, and you will have trouble interpreting the data if it contains 0-bytes. Even worse, you are risking a buffer overflow if you can't guarantee your buffer is big enough to hold all data up to and including the next occurence of '\n'!

gets() is meant for getting printable text, not binary data. See http://www.cplusplus.com/reference/cstdio/gets/[^]

[edit]
After you posted your code, the question becomes much clearer: You have inserted calls to gets() in addition to your original socket reads, not replaced those calls by gets() as I thought!

Obviously, calling a second - unrelated - reading function does not serve any real purpose. But since that change appeared to make your program work, maybe the calls to gets() have a hidden side effect, e. g. forcing a synchronization of your socket.

I have no experience with socket programming, but I believe it works different on different systems, so you should mention what OS you're working on!

In any case, you really should check for errors, especially after the call to recv. Check the appropriate documentation for socket programming on your OS.
[/edit]

P.S.:
Have a look at this: http://stackoverflow.com/questions/6979769/linux-ioctl-with-fionread-always-0[^] Your problem may be related to what is explained in the second response - that you should use select() rather than ioctl()
 
Share this answer
 
v3
gets() is used to in a string for taking value and as work as scanf() works
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900