Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

The scenario involved is i have designed a UI for a C code. I should send the data to server, the communication is done via tcp/ip. In the first step, the server has to open the socket to accept the data, so when i run the server code, i am getting the connection accepted and "segmentation fault" error is thrown and connection is automatically closed. How to keep the connection open till the data is sent from the client. I have used TCP as communication protocol.

Also a file named "core" is generated in the folder where source code is there. How to resolve this issue.

 #include "SingleDirectionCServer.h"
 #include<jni.h>
 #include<stdio.h>
 #include<string.h>    
 #include<stdlib.h>
 #include<unistd.h>
 #include<sys/ioctl.h>
 #include<sys/types.h>
 #include<arpa/inet.h>    
 #include<sys/socket.h> 

 //using namespace std;

 //This function is to be used once we have confirmed that an video is to be sent
 //It should read and output an video file

long long int receive_video(long long int socket,const char *file)

 {

	long long int buffersize = 0, recv_size = 0,size = 0, read_size, write_size;

	char videoarray[10240],verify = '1',errno;

	FILE *video;

	//Find the size of the video

	read(socket, &size, sizeof(int));

	//Send our verification signal

	write(socket, &verify,sizeof(char));

	//Make sure that the size is bigger than 0

	if(size <= 0 )

 	{

        	printf("Error has occurred. Size less than or equal to 0 \n");
        	return -1;

 	}

		video = fopen(file, "w");

		if( video == NULL) 

	{

	        printf("Error has occurred. video file could not be opened \n");
	        return -1;

        }

		//Loop while we have not received the entire file yet

		while(recv_size < size) 

        {

                ioctl(socket, FIONREAD, &buffersize); 

        	//We check to see if there is data to be read from the socket    

	        if(buffersize > 0 ) 

	{

       		 if(read_size = read(socket, videoarray, buffersize) < 0){
       		 printf("%s", strerror(errno));

        }

       		 //Write the currently read data into our video file

        	write_size = fwrite(videoarray,1,(buffersize), video);

       /* if(write_size != buffersize) 
     
        {

          printf("write and buffer sizes are wrong \n");

        }

        if(read_size != write_size) 

        {

            printf("error in read write \n");

        }  */

        //Increment the total number of bytes read

        recv_size += read_size;

        //Send our handshake verification info

        write(socket, &verify, sizeof(char));

      }
  }

	fclose(video);
	printf("video successfully Received! \n");
	return 0;
 }

JNIEXPORT jint JNICALL Java_SingleDirectionCServer_input
  (JNIEnv *env, jobject obj, jint port, jstring file)
	{

		long long int socket_desc;
		struct sockaddr_in server;
		char *parray,errno;
		const char *clocation = (*env)->GetStringUTFChars(env, file, NULL);

		//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 = htonl(INADDR_ANY);	  
			  server.sin_family = AF_INET;
			  server.sin_port = htons(port); 

		//Connect to remote server

			  if (connect(socket_desc,(struct sockaddr *)&server , sizeof(server))) 

		    	  {


			//   printf(strerror(errno));
   				
			//printf("Connect Error \n");

    			 //return 1;
		
			puts("Connected \n");
       }

			 

			 receive_video(socket_desc,clocation);

			 close(socket_desc);

                         //return 0;
	}
Posted
Updated 21-Sep-15 19:10pm
v3
Comments
Kornfeld Eliyahu Peter 21-Sep-15 6:09am    
Forget the 'core' file - go and debug your application!
venkat28vk 21-Sep-15 6:18am    
But when i run C program, i am able to send and receive files of any size. But when compared with java code in UI Creation, i am facing lot of issues.
barneyman 21-Sep-15 6:09am    
that's a core dump ... your program is crashing

you're going to have to debug the program or examine the core dump
venkat28vk 21-Sep-15 6:18am    
Hi,
But when i am running C the code in my system, i am not facing any errors. How can i overcome this.
Jochen Arndt 21-Sep-15 6:20am    
In addition to the other comments:
Concentrate on the code after accepting the connection (guess: your receive routine) and check for invalid pointers and buffer overruns (these are the most frequent sources for segmentation fault errors).

Whe you receive segmentation fault, you programs has crashed (see, for instance Segmentation fault[^]) and it doesn't matter that 'it is running fine on another system'. Either it is receiveing a different kind of inputs or it is trying to access a resource it couldn't acquire or whatever, you are experiencing a program crash.
You might:
  • As suggested, inspect your code for pitfalls.
  • Post your code here (might be we are ablte to spot the mistake).
  • If possible, debug it on the system wherein it crashes.
 
Share this answer
 
Comments
Philippe Mori 21-Sep-15 12:19pm    
Write your code in the question properly formatted.
venkat28vk 21-Sep-15 9:06am    
I have used java code to create a UI and i have linked it with C code.
In this line
if(read_size = read(socket, videoarray, buffersize) < 0)

you are trying to read buffersize bytes into videoarray. This will fail when buffersize is greater than the size of your array (10240).

To avoid this, allocate videoarray dynamically using an additional variable to track the size. When buffersize becomes greater than that size, reallocate the array.
 
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