Click here to Skip to main content
14,488,436 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I would like to send a text file to java from C, and receive the same file from java to C. I have developed the code, but i am facing some issues in the receiving function and i would like to know can two sockets can be established for transferring files each in send and receive function. Could somebody sort out the issue for me.

#include<time.h>
 #include<errno.h>
 #include<stdio.h>
 #include<fcntl.h>
 #include<unistd.h>
 #include<string.h>    
 #include<stdlib.h>

 int send_text(int socket)

{ 
	FILE *text;
	char a[50];
	int size, read_size, stat, packet_index;
	char send_buffer[8008], read_buffer[8008];
	int wrt=0,sock_fd,tsize=0;
	packet_index = 1;
	int i=0; 
	text = fopen("/home/sosdt009/Desktop/character3.txt", "r"); 
	if (text == NULL)
	{
		printf("Error Opening text File:");
		exit(-1);
	} 	
	printf("Getting text Size:\n");
	gets(a);
	fseek(text, 0, SEEK_END);
	size = ftell(text);
	fseek(text, 0, SEEK_SET);
	printf("Total text size: %d \n", size);	
 	gets(a);

	//Send text Size

	printf("Sending text Size:\n",size);
	gets(a);
	send(socket, (void *)&size, sizeof(size), 0);

	/*do
	
	{		
		stat = recv(socket, read_buffer, 1024, 0);
			
	}*/while(stat < 0)
	printf("Socket data:%s \n", read_buffer); 
	gets(a);	
	while(size>tsize) 
 	{
 
		//Read from the file into our send buffer
		read_size = fread(send_buffer,1,sizeof(send_buffer),text);
		printf("The size of send buffer:%c \n",send_buffer);
		gets(a);
		printf("The read size value is :%d \n", read_size);
		gets(a);
		
		//Send data through our socket 
		
		do
		{			
			stat = send(socket, send_buffer, read_size, 0);
			printf("The send size value is: %d \n", size);
			gets(a);
			printf("The read size value is: %d \n", read_size);
			gets(a);

		} while (stat < 0); 
		printf("Packet %d, sent %d bytes.\n", packet_index, read_size);
		gets(a);
	
		//packet_index++;
		//Zero out our send buffer
		
		tsize = tsize+read_size;
		printf("The tsize value is:%d \n",tsize);
		gets(a);
    		memset(send_buffer,0, sizeof(send_buffer));

		if(read_size<=NULL)
		{
			printf("The connection is transferred to received text: \n");
			gets(a);
		}
	}
	fclose(text);
	//close(socket);
	printf("Text successfully send:\n");
	gets(a);
	return 0;	
}

int receive_text(int socket)
{ 
	int buffersize = 77,recv_size=0,read_size = 1, write_size,size; 
	char pBuf[77],a[50]; 
	int errnom,i;
	FILE *text; 
	size_t rec;
	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(errnom, "Memory Error Cannot Allocate!\n");
				gets(a);
				exit(-1);
			}
			
			//read_size = read(socket,pBuf,sizeof(pBuf));
			read_size = fread(pBuf,buffersize,1,text);
			//read_size = recv(socket,*pBuf,buffersize,1);
			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 = write(socket,read_size,1);                //using write function
			write_size = fwrite(pBuf,sizeof(pBuf),1,text); 
			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;
}

int main(int argc,char *argv[])
{
		int socket_desc;
		struct sockaddr_in server;
		char *parray,errnomu;

		//Create socket

		socket_desc = socket(AF_INET,SOCK_STREAM,0);
		if(socket_desc == -1) 
		{
  			  printf("Could not create socket \n");
		}
			  memset(&server,0,sizeof(server));
			  server.sin_addr.s_addr = inet_addr("10.170.0.38");
			  server.sin_family = AF_INET;
			  server.sin_port = htons(6999); 

		//Connect to remote server
			
			  if (connect(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) 
			  {

				 printf(strerror(errnomu));   					
			  	 printf("Connect Error \n");
    			  	 return -1;			
      			  }
			
			 puts("Connected");
			 send_text(socket_desc);
			 receive_text(socket_desc);
			 close(socket_desc);
			 return 0;
			
}
Posted
Updated 19-Aug-15 21:30pm
v4
Comments
CPallini 18-Aug-15 2:48am
   
You should detail the issues you are experiencing on the receive function.
venkat28vk 18-Aug-15 2:51am
   
I am receiving the file sent from java, but the values are displayed as following "
\00\00\00\00\00\00\00c@\00\00\00\00\009z\A2\FD\00\00\B3H\AA\84\00\00\00\94ބ\00\00 \00\00\00\00\00\00\00c@\00\00\00\00\00r\9F\A9\84"
venkat28vk 18-Aug-15 2:52am
   
When i print the statements i am also getting the read size value as -1 in receive function, which indicates no data is written into the file.
CPallini 18-Aug-15 2:55am
   
You are not reading from the socket, the recv call is commented out.
venkat28vk 18-Aug-15 3:13am
   
I have tried out with recv also, but still i am getting the same error. Just to state that i have tried with all options i have put the stmt in the code.
CPallini 18-Aug-15 3:23am
   
By the way your call to recv looks wrong. It should be

read_size = recv(socket, pBuf, sizeof(pBuf), 0);
venkat28vk 18-Aug-15 3:33am
   
I tried as per you said but i am not getting the required output.And moreover in the above syntax i hope the buffer variable should be a pointer and is it really imp to give flag as 0 what if flag value is 1? Say your thoughts on this?
CPallini 18-Aug-15 3:49am
   
What do you get?
*pBuf is plain wrong.
flag = 0 is the default, it works in most cases (on the other hand flag=1 means 'receive out of band data' (see socket.h for details).
venkat28vk 18-Aug-15 4:33am
   
What can i give instead of pBuf to get my required output. And thanks for the answer provided on flag.
CPallini 18-Aug-15 4:44am
   
pBuf is correct (*pBuf is wrong).
venkat28vk 18-Aug-15 4:53am
   
I have tried with pBuf in the recv function, but still i am getting the same output.
venkat28vk 18-Aug-15 4:57am
   
And moreover is it really mandatory to use malloc, because i have commented it.
CPallini 18-Aug-15 5:11am
   
What are you sending?
Could you please try a simpler test (e.g. send just "hello" form Java)?
CPallini 18-Aug-15 5:13am
   
You don't need. pBuf is a statically allocated array.
venkat28vk 18-Aug-15 8:16am
   
I have tried i am just sending a few characters, from java but in the receiving end i am getting like, what i have stated above.
venkat28vk 20-Aug-15 3:18am
   
Are there any other ways to achieve it, because i have tried with all the options. Could you kindly say me how i can resolve this problem.
CPallini 20-Aug-15 3:49am
   
I would reduce the problem: write a simple C program just for receiving (you could also search for an available code snippet) and write a simple Java program just sending data in a loop.
venkat28vk 20-Aug-15 4:18am
   
I am able to receive a file from java to C via tcp/ip and send the same file back to java,i have given the same code but with slight change in function calling and it is working fine with no data loss or any other interruptions.
venkat28vk 20-Aug-15 4:31am
   
And moreover i am able to send a file to java in single directional way via tcp/ip with no data loss and java is able to receive it. I don't understand why when file is send from java to C, i am having data loss
Richard MacCutchan 20-Aug-15 5:30am
   
If you Google for "socket java" and "socket c" you will find many samples. The differences are only in the language. The actual protocol of transferring between the two points is exactly the same and simple to implement. Your code looks over engineered.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100