Thread: Send a message after sending a file via socket

  1. #16
    Registered User
    Join Date
    Mar 2019
    Posts
    5
    yes, when i did a test of a send of message without the file transfer running perfectly.
    Otherwise, I used the return values: the send step from client does not return any error but the read step of server returns the value -1 ?!!

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Just so we're clear.

    The last part of server.c is
    Code:
    ssize_t result = send(socket,Msg_to_cl,strlen(Msg_to_cl),0);
    if ( result == -1 ) {
      perror("Final send failed");
    } else {
      printf("Sent %zd bytes\n", result);
    }
    And the last part of client.c is
    Code:
    ssize_t result = read(socket, Msg_from_sr, 100);
    if ( result == -1 ) {
      perror("Final read failed");
    } else {
      printf("Received %zd bytes\n", result);
    }
    > if ((socket = envoyer_fich(socket, line_str)) < 0)
    Why do you modify socket here with the assignment?
    What does envoyer_fich actually do?
    Why don't you send desc_line (the actual line length) as a parameter to this function?

    Are you taking into account that send() and recv() can both be partially successful?
    That is, if you send("hello world"), send might return say 7, indicating that "hello w" was sent.
    It is YOUR responsibility to call send("orld") to ensure all the data is sent.

    > while ((desc_line = getline(&line_str, &line_pos, fichier_cl)) != -1)
    Is your JSON reasonably well formatted, or is it some massive file devoid of white-space and newlines?

    Code:
        if (line_str) {
          free(line_str);
        }
        line_pos = 0;
    Not only is this unnecessary, it's actually a bug.
    What will happen on the next call to getline() is that getline will see what it things is a pointer to 0 bytes and try to realloc.
    But your pointer is a stale pointer to freed memory, not a valid pointer to zero bytes.

    You only need ONE free(line_str), and that's OUTSIDE the while loop.

    Also posted here -> Envoyer un message apres le send d'une fichier via socket - C - Programmation - FORUM HardWare.fr
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User
    Join Date
    Mar 2019
    Posts
    5
    yes you're right but the JSON file is sent to the server correctly. I avoided using the socket value as the return value of send_fich but the same problem.
    envoyer_fich:
    Code:
    int envoyer_fich(int socket, char* line)
    {  
      int desc;
      desc = send(socket,line, strlen(line), 0);
      return desc;
    }
    and i have used like this:
    Code:
    if(envoyer_fich(socket,line_str) < 0) 
        {
          printf("ERROR: Failed to send file %s.\n", file_name);
          break;
        }
    Afterthat, From server: send 19 bytes, from client: received 0 bytes

  4. #19
    Registered User
    Join Date
    Mar 2019
    Posts
    5
    Thank you for your response, but i have resolved my problem using a second connection port between server and client

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Message only receive / Not Send (Pthread Issue)
    By sergioms in forum C Programming
    Replies: 3
    Last Post: 07-17-2018, 07:49 AM
  2. Use one socket to send and receive message (using UDP)
    By tanya9x in forum C++ Programming
    Replies: 11
    Last Post: 02-28-2010, 05:58 AM
  3. Send() not "sending" whole message
    By Martin_T in forum C Programming
    Replies: 9
    Last Post: 11-18-2009, 07:37 PM
  4. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 04:46 AM
  5. Who is using a file/How to send a message
    By leonel in forum Windows Programming
    Replies: 0
    Last Post: 01-31-2003, 01:54 PM

Tags for this Thread