언어 설정

Menu
Sites
Language
Socket recv() function or read() function are not working correctly, the process dead.

Hello,

I'm devloping Native App in Gear S3 and using socket to transfer file to Server.

But, some problem happend. When i use recv() function, the process has closed.

and this is my log message.

my code is like this.

static void
send_audio(appdata_s *data){

    appdata_s *ad = data;
 FILE *fp;
 struct sockaddr_in address;
 char buf[1024];
 char buff_audio[1024];
 char buff_rcv[1024];
 unsigned int file_size;
 int sendBytes;

 fp = fopen(ad->file_path,"rb");
 if(fp == NULL)
 {
  dlog_print(DLOG_ERROR,"USR_TAG","FILE Pointer ERROR");
 }

 fseek(fp, 0L, SEEK_END);
 file_size = ftell(fp);
 fseek(fp, 0L, SEEK_SET);

 int SendNum = file_size/sizeof(buff_audio)+1;
 dlog_print(DLOG_INFO,"USR_TAG","file Size: %d",file_size);

 memset(&address,0,sizeof(address));
 address.sin_family = AF_INET;
 address.sin_addr.s_addr = 274187880;
 address.sin_port = htons(8001);

 int sockfd = socket(AF_INET, SOCK_STREAM, 0);

 if(sockfd == -1) {
  dlog_print(DLOG_INFO,"USR_TAG","socket create fail");
  return;
 }

 if(connect(sockfd, (struct sockaddr*)&address, sizeof(address)) == -1){
  dlog_print(DLOG_INFO,"USR_TAG","socket connection fail");
  return;
 }

 //send Start Message
 if(send(sockfd,"TIZEN\n",6,MSG_NOSIGNAL)<=0){
  dlog_print(DLOG_INFO,"USR_TAG","write 1 error");
 }


 //file send
 /*
 while((sendBytes = fread(buff_audio,sizeof(char),sizeof(buff_audio),fp))>0){
   send(sockfd,buff_audio,sendBytes,0);
   dlog_print(DLOG_INFO,"USR_TAG","sendBytes:%d",sendBytes);
 }*/

 memset(buff_rcv, 0x00, 1024);
 if(recv(sockfd,buff_rcv,sizeof(buff_rcv),MSG_NOSIGNAL)==-1){
  dlog_print(DLOG_INFO,"USR_TAG","read 1 error");
 }
 dlog_print(DLOG_INFO,"USR_TAG","recv:%s",buff_rcv);
 close(sockfd);
}

When i delete the recv() function, the process is not closed.

I don't know why the recv function is not working and kill the process.

Plus, I upload my Server's python code's some part.

   elif (data == "TIZEN") | (data == "TIZEN\n"):

        wave_output_filename = ("%s" % (addr,))
        f=open(wave_output_filename+'.wav', 'wb')

        print ("start form tizen...")

        data = sock.recv(BUFSIZE)
        print "filesize:%s" %data

        """
        while (True):
            data = sock.recv(BUFSIZE)
            if (data == "END") | (data == "END\n"):
                print 'END'
                break
            else:
                f.write(data)
                print ('receive data, size: %s' % (len(data),))         
        
        """

        sock.send('ACK: TIZEN')
        print 'ACK: TIZEN'

        f.close()

When i start the Server code and Tizen code, the server console print "start from tizen.." and "ACK:TIZEN" correctly but the TIZEN code not receive the ACK and process dead...

Why this error occur? Using socket in tizen is not working?

I already added a privilege for internet..

Please help me, Thanks in advance.

Edited by: 은성 부 on 18 5월, 2017

Responses

2 댓글
은성 부

i found that the problem is from server. Python Server did not send message this code...i don't know why.

But i modified tizen code for this error, I added setsockopt() function in Tizen code.  So, i set Timeout 5 seconds for recv() function.

So No message recv and  after 5 sencod, the recv function return -1. Then i can prevent unexpected termination of the process.

 

this is modifed code in Tizen:

static void
send_audio(appdata_s *data){

    appdata_s *ad = data;
 FILE *fp;
 struct sockaddr_in address;
 char buf[1024];
 char buff_audio[1024];
 char buff_rcv[1024];
 unsigned int file_size;
 int sendBytes;

 struct timeval recv_limit_time;
 recv_limit_time.tv_sec = 5;
 recv_limit_time.tv_usec = 0;

 fp = fopen(ad->file_path,"rb");
 if(fp == NULL)
 {
  dlog_print(DLOG_ERROR,"USR_TAG","FILE Pointer ERROR");
 }

 fseek(fp, 0L, SEEK_END);
 file_size = ftell(fp);
 fseek(fp, 0L, SEEK_SET);

 int SendNum = file_size/sizeof(buff_audio)+1;
 dlog_print(DLOG_INFO,"USR_TAG","file Size: %d",file_size);

 memset(&address,0,sizeof(address));
 address.sin_family = AF_INET;
 //address.sin_addr.s_addr = 274187880;
 //address.sin_addr.s_addr = 385949356;
 address.sin_addr.s_addr = 167845548;
 address.sin_port = htons(8001);

 int sockfd = socket(AF_INET, SOCK_STREAM, 0);

 if(sockfd == -1) {
  dlog_print(DLOG_INFO,"USR_TAG","socket create fail");
  return;
 }

 setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&recv_limit_time, sizeof(recv_limit_time));
 if(connect(sockfd, (struct sockaddr*)&address, sizeof(address)) == -1){
  dlog_print(DLOG_INFO,"USR_TAG","socket connection fail");
  return;
 }

 //send Start Message
 if(send(sockfd,"TIZEN\n",6,MSG_NOSIGNAL)<=0){
  dlog_print(DLOG_INFO,"USR_TAG","write 1 error");
 }


 //file send
 while((sendBytes = fread(buff_audio,sizeof(char),sizeof(buff_audio),fp))>0){
   send(sockfd,buff_audio,sendBytes,0);
   dlog_print(DLOG_INFO,"USR_TAG","sendBytes:%d",sendBytes);
 }

 memset(buff_rcv, 0x00, 1024);
 if(recv(sockfd,buff_rcv,sizeof(buff_rcv)-1,MSG_NOSIGNAL)==-1){
  dlog_print(DLOG_INFO,"USR_TAG","read 1 error");
 }
 dlog_print(DLOG_INFO,"USR_TAG","recv:%s",buff_rcv);
 close(sockfd);
}

But , I still do not know why the bleow server's pyhton code not working correctly.

 sock.send('ACK: TIZEN')
Shaswati Saha

Did you add the following privileges to the manifest file?
 

<privileges>
   <privilege>http://tizen.org/privilege/network.get</privilege>
   <privilege>http://tizen.org/privilege/network.set</privilege>
   <privilege>http://tizen.org/privilege/network.profile</privilege>
</privileges>