Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I would like to receive a file from Java to C via tcp/ip protocol. I am able to receive only an empty file there is no data written in it. Help me with the code to get the issue resolved..
C
int receive_text(int socket)
{ 
	int buffersize,recv_size = 0,read_size = 1, write_size, size; 
	char *pBuf,a[50]; 
	int errno;
	FILE *text; 
	text = fopen("/home/sosdt009/Desktop/receivednew.txt", "w"); 	
	if (text == NULL)	
	{		
		printf("Error has occurred, text file could not be opened \n");
		return -1;
	}
 
	//Loop while we have not received the entire file yet

	 while (read_size > 0)
	{
			
			ioctl(socket, FIONREAD, &buffersize);
			printf("The Buffersize is    :%d\n",buffersize);
			gets(a);
			printf("The size of socket is:%d\n",socket);
			gets(a);
 
		//We check to see if there is data to be read from the socket 

		if (buffersize > 0)
		{
			printf("Buffersize value is  :%d\n", buffersize);
			gets(a);
			pBuf = malloc(buffersize);
			if (!pBuf)
			{
				printf(errno, "Memory Error Cannot Allocate!\n");
				gets(a);
				exit(-1);
			}
			read_size = recv(socket, pBuf, buffersize, 0);
			printf("Read size value is :%d \n",read_size);
			gets(a);
			printf("Buffersize value is:%d \n",pBuf);
			gets(a);
			if (read_size > 0)
			{
				printf("%d\n",strerror(errno));
				printf("Data not written to the file:\n");
				gets(a);
				goto free;
			}
 
			//Write the currently read data into our text file

			write_size = fwrite(read_size,1,sizeof(read_size),text); 
			free(pBuf);
			printf("Write size value is   :%d \n",write_size);
			gets(a);
			printf("Buffer size value is  :%d \n",buffersize);
			gets(a);
 
			//Increment the total number of bytes read

			recv_size += read_size;
			printf("Received size value is:%d \n",recv_size);
			gets(a);
			printf("Read size value is    :%d \n",read_size);		
			gets(a);
		}
	}
	free:
	fclose(text);
	close(socket); 	
	printf("Text Successfully Received:\n");
	gets(a);
	return 0;
}
Posted
Updated 29-Jul-15 1:46am
v3

1 solution

C++
if (read_size > 0)
{
    printf("%d\n",strerror(errno));
    printf("Data not written to the file:\n");
    gets(a);
    goto free;
}

This means, if the socket returns any data you immediately exit. The if statement should be testing if it is equal to zero.
 
Share this answer
 
Comments
venkat28vk 29-Jul-15 8:48am    
I have tried read size equal to zero, but it is exiting out of the loop and no data are displayed. Could you kindly say me another possible way to resolve this.
Richard MacCutchan 29-Jul-15 8:52am    
What do you mean "another possible way"? That is the way, if the recv value is greater than zero then you have received some data and you should save it to the file. If the value is zero then there is no data to receive. However, that does not signal the end of the data, as the socket may simply be waiting for the remote system to do a send. See https://msdn.microsoft.com/en-us/library/windows/desktop/ms740121(v=vs.85).aspx for more details.
venkat28vk 29-Jul-15 9:07am    
is all other part of code is correct, and do i need to change the file mode from w to a for saving the file.
Richard MacCutchan 29-Jul-15 9:09am    
You need to use your debugger to trace what is going on. I don't have the time or resources to go through checking all your code.

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