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>
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;
read(socket, &size, sizeof(int));
write(socket, &verify,sizeof(char));
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;
}
while(recv_size < size)
{
ioctl(socket, FIONREAD, &buffersize);
if(buffersize > 0 )
{
if(read_size = read(socket, videoarray, buffersize) < 0){
printf("%s", strerror(errno));
}
write_size = fwrite(videoarray,1,(buffersize), video);
recv_size += read_size;
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);
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);
if (connect(socket_desc,(struct sockaddr *)&server , sizeof(server)))
{
puts("Connected \n");
}
receive_video(socket_desc,clocation);
close(socket_desc);
}