Thread: When send fails, it kills the program without warning.

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    47

    When send fails, it kills the program without warning.

    Hi,

    I have a program (SERVER) that listens on two ports (45113 and 45115), and once they are connected, will send user input to the two sockets.

    Once everything is connected and working, I killed off one of the externally connected programs (TV or MUSIC), and have the server send more data, the server program sends the data and then just stops.

    Since the sending operation is in an endless loop and send doesn't do error checking, shouldn't there be some sort of segmentation fault or some sort of error output? Why does it just stop?

    server.c
    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    #include <libgen.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    // Declare defines.
    #define BUFFER_SIZE          1024
    #define PROGRAM_NAME_SIZE      50
    #define MUSIC_PORT          45113
    #define TV_PORT             45115
    #define MUSIC_IP                "127.0.0.1"
    #define TV_IP                   "127.0.0.1"
    
    // Declare function prototypes.
    int create_and_accept_connection (int port, char *ip);
    void run_server (void);
    
    // Declare global variables.
    char program_name[PROGRAM_NAME_SIZE + 1] = {0};
    
    int main (int argc, char *argv[])
    {
    // Get program name for error reporting.
        strcpy(program_name, basename(argv[0]));
    
    // Check arguments.
        if(argc != 1)
        {
            fprintf(stderr, "Usage: %s\n", program_name);
            exit(EXIT_FAILURE);
        }
    
    // Run the server.
        run_server();
    
    // Exit cleanly.
        exit(EXIT_SUCCESS);
    }
    
    int create_and_accept_connection (int port, char *ip)
    {
    // Decalre variables.
        int option_value = 1;
        int serverSocket = {0};
        int incomingSocket = {0};
        socklen_t addr_size = {0};
        struct sockaddr_in serverAddr = {0};
        struct sockaddr_storage serverStorage = {0};
    
    // Create the socket.
        if((serverSocket = socket(PF_INET, SOCK_STREAM, 0)) == -1)
        {
            fprintf(stderr, "%s: create_and_accept_connection error: socket failed (%s)\n", program_name, strerror(errno));
            exit(EXIT_FAILURE);
        }
    
    // Configure settings of the server address.
        serverAddr.sin_family = AF_INET;
        serverAddr.sin_port = htons(port);
        serverAddr.sin_addr.s_addr = inet_addr(ip);
    
    // Set all bits of the padding field to 0.
        memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
    
    // Kill "Address already in use" error message.
        if(setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &option_value, sizeof(int)) == -1)
        {
            fprintf(stderr, "%s: run_server error: setsockopt failed (%s)\n", program_name, strerror(errno));
            exit(EXIT_FAILURE);
        }
    
    // Bind the address struct to the socket.
        if(bind(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) == -1)
        {
            fprintf(stderr, "%s: run_server error: bind failed (%s)\n", program_name, strerror(errno));
            exit(EXIT_FAILURE);
        }
    
    // Listen on the socket.
        if(listen(serverSocket, 0) == -1)
        {
            fprintf(stderr, "%s: run_server error: bind failed (%s)\n", program_name, strerror(errno));
            exit(EXIT_FAILURE);
        }
    
    // Store address sizeof.
        addr_size = sizeof serverStorage;
    
    // Accept call creates a new socket for the incoming connection.
        if((incomingSocket = accept(serverSocket, (struct sockaddr *) &serverStorage, &addr_size)) == -1)
        {
            fprintf(stderr, "%s: run_server error: accept failed (%s)\n", program_name, strerror(errno));
            exit(EXIT_FAILURE);
        }
    
    // Return answered socket.
        return(incomingSocket);
    }
    void run_server (void)
    {
    // Decalre variables.
        int socket_pi = {0};
        int socket_music = {0};
        char buffer[BUFFER_SIZE + 1] = {0};
    
    // Start listening for user TV.
        socket_pi = create_and_accept_connection(TV_PORT, TV_IP);
    
    // Start listening for user MUSIC.
        socket_music = create_and_accept_connection(MUSIC_PORT, MUSIC_IP);
    
    // Loop forever.
        while(1)
        {
    // Get the input to send.
            fgets(buffer, BUFFER_SIZE, stdin);
    // Send code to TV and MUSIC sockets.
            send(socket_pi, buffer, strlen(buffer), 0);
            send(socket_music, buffer, strlen(buffer), 0);
        }
    }
    tv.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <libgen.h>
    #include <errno.h>
    
    // Declare defines.
    #define BUFFER_SIZE          1024
    #define PORT                45115
    #define PROGRAM_NAME_SIZE      50
    #define IP                      "127.0.0.1"
    
    // Declare function prototypes.
    void run_client (void);
    
    // Declare global variables.
    char program_name[PROGRAM_NAME_SIZE + 1] = {0};
    
    int main (int argc, char *argv[])
    {
    // Get program name for error reporting.
        strcpy(program_name, basename(argv[0]));
    
    // Check arguments.
        if(argc != 1)
        {
            fprintf(stderr, "Usage: %s\n", program_name);
            exit(EXIT_FAILURE);
        }
    
    // Run the server.
        run_client();
    }
    
    void run_client (void)
    {
    // Declare variables.
        int len = {0};
        int clientSocket = {0};
        char buffer[1024] = {0};
        socklen_t addr_size = {0};
        struct sockaddr_in serverAddr = {0};
    
    // Create the socket.
        if((clientSocket = socket(PF_INET, SOCK_STREAM, 0)) == -1)
        {
            fprintf(stderr, "%s: run_client error: socket failed (%s)\n", program_name, strerror(errno));
            exit(EXIT_FAILURE);
        }
    
    // Configure settings of the server address struct.
        serverAddr.sin_family = AF_INET;
        serverAddr.sin_port = htons(PORT);
        serverAddr.sin_addr.s_addr = inet_addr(IP);
    
    // Set all bits of the padding field to 0.
        memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
    
    // Set varaiable.
        addr_size = sizeof serverAddr;
    
    // Connect the socket to the server using the address struct.
        while(connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size) == -1)
            ;
    // Read the message from the server into the buffer.
        while((len = recv(clientSocket, buffer, BUFFER_SIZE, 0)) > 0)
    // Print the received message.
            write(0, buffer, (size_t) len);
    
    // Exit cleanly.
        exit(EXIT_SUCCESS);
    }
    music.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <libgen.h>
    #include <errno.h>
    
    // Declare defines.
    #define BUFFER_SIZE          1024
    #define PORT                45113
    #define PROGRAM_NAME_SIZE      50
    #define IP                      "127.0.0.1"
    
    // Declare function prototypes.
    void run_client (void);
    
    // Declare global variables.
    char program_name[PROGRAM_NAME_SIZE + 1] = {0};
    
    int main (int argc, char *argv[])
    {
    // Get program name for error reporting.
        strcpy(program_name, basename(argv[0]));
    
    // Check arguments.
        if(argc != 1)
        {
            fprintf(stderr, "Usage: %s\n", program_name);
            exit(EXIT_FAILURE);
        }
    
    // Run the server.
        run_client();
    }
    
    void run_client (void)
    {
    // Declare variables.
        int len = {0};
        int clientSocket = {0};
        char buffer[1024] = {0};
        socklen_t addr_size = {0};
        struct sockaddr_in serverAddr = {0};
    
    // Create the socket.
        if((clientSocket = socket(PF_INET, SOCK_STREAM, 0)) == -1)
        {
            fprintf(stderr, "%s: run_client error: socket failed (%s)\n", program_name, strerror(errno));
            exit(EXIT_FAILURE);
        }
    
    // Configure settings of the server address struct.
        serverAddr.sin_family = AF_INET;
        serverAddr.sin_port = htons(PORT);
        serverAddr.sin_addr.s_addr = inet_addr(IP);
    
    // Set all bits of the padding field to 0.
        memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
    
    // Set varaiable.
        addr_size = sizeof serverAddr;
    
    // Connect the socket to the server using the address struct.
        while(connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size) == -1)
            ;
    
    // Read the message from the server into the buffer.
        while((len = recv(clientSocket, buffer, BUFFER_SIZE, 0)) > 0)
    // Print the received message.
            write(0, buffer, (size_t) len);
    
    // Exit cleanly.
        exit(EXIT_SUCCESS);
    }

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    676
    Linux ou outro Unix?

    man 7 signal, veja SIGPIPE.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    47
    Yes, sorry, it is linux.

    Ty for the lead. Found the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program works for stdin but fails for a file.
    By felixthunder in forum C Programming
    Replies: 8
    Last Post: 03-22-2020, 08:21 AM
  2. My program fails these two tests
    By amirghannadan in forum C Programming
    Replies: 1
    Last Post: 10-19-2015, 11:15 PM
  3. simple MPI program fails
    By dayalsoap in forum C++ Programming
    Replies: 10
    Last Post: 08-05-2011, 09:03 AM
  4. program that kills itself
    By sangi in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2004, 07:00 AM
  5. A program that kills itself...and questions
    By Shadow in forum C Programming
    Replies: 5
    Last Post: 07-25-2002, 07:08 PM

Tags for this Thread