Search code examples
cnetwork-programmingsend

Server program is stuck at send


I am building a server client model in C. The clients connects to the server and they start exchanging data. However, the user can end the client at any time in the program, but the server is not notified about it. The server keeps sending that data even after the client is closed. I was in the impression that send function will return -1 if the server is unable to send the data, but my server program just stuck at send

if((byteSent = send(new_fd, fileContents,  strlen(fileContents), 0)) == -1){ //

the program just halts at the above line.

How do I overcome this problem?

//Code

   exitT = 0;
    //execution_count = 1;
    for(i=0;i<execution_count;i++)
    {  
        sleep(time_delay);

        //getting the current time on the server machine
        time_t t;
        time(&t);

        char *time=ctime(&t);
        printf("The Execution time at server =  %s\n",time);

        system(exec_command);

       /*Open the file, get file size, read the contents and close the file*/

        // Open the file
        fp = fopen(fileName,"r");

        // Get File Size
        fseek(fp,0,SEEK_END);
        dataLength = ftell(fp);
        rewind(fp);                

        fileContents = (char*)malloc(dataLength+1);
       // Read File
       fread(fileContents,1,dataLength,fp);
       fileContents[dataLength] = '\0';

        // Close file
         fclose(fp);    

       printf("sockfd = %d \n",new_fd);
       // send file length to client
       rc=send(new_fd, &dataLength,  sizeof(dataLength), 0) ;

       printf("length of client data = %d \n",rc);

        printf("sockfd = %d \n",new_fd);
       // send time to client
       rc=send(new_fd, time,  strlen(time), 0) ;

       printf("length of client time = %d \n",rc);

       usleep(20000);

       // Send file contents to Client
       while(dataLength>0){
            printf("sockfd = %d \n",new_fd);
            if((byteSent = send(new_fd, fileContents,  strlen(fileContents), 0)) == -1){
                printf("bytes sent = %d \n",byteSent);
                exitT = 1;
                break;
            }
            dataLength-=byteSent;
       }

       //Delete the log file 
       sprintf(deleteCommand,"rm %s",fileName);
       system(deleteCommand);
       if(exitT == 1)
           break;
     }

      bzero(fileName,sizeof(fileName));
      bzero(exec_command,sizeof(exec_command));
      bzero(deleteCommand,sizeof(deleteCommand));

      //decClientNum();
      kill(parent_id,SIGALRM);
      close(new_fd);  // parent doesn't need this
      printf("STATUS = CLOSED\n");

      exit(0);
  }   

Thanks


Solution

  • I assume you are coding for a Linux or Posix system.

    When a syscall like send fails it returns -1 and sets the errno; you very probably should use errno to find out why it failed.

    You could use strace to find out which syscalls are done by your sever, or some other one. Of course, use also the gdb debugger.

    You very probably need to multiplex inputs or outputs. The system calls doing that are poll, select (and related ppoll and pselect). Read e.g. the select_tut(2) man page.

    You may want to use (or at least to study the source code of) existing event oriented libraries like libevent, libev etc.. (Both Gtk and Qt frameworks provide also their own, which might be used even outside of GUI applications).

    I strongly suggest reading about advanced unix programming and unix network programing (and perhaps also about advanced linux programming).