0

So I'm having an issue with a basic server I wrote with C. Every time I try to send less than 6 bytes to a client, the client never displays the bytes I send. There's no failure on the send() call. I've tested this on CURL and Postman, and can't get a response on either.

I cut my program down to the relevant bits, so the struct you see doesn't do anything here.

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

void exit_with_error(const char *err);
#define MSG_SIZE            1028

typedef struct list_s
{
    struct list_s *next;
    char *title;
    char *description;
    int id;
} list_t;

int main(void)
{
    char *client_name, msg[MSG_SIZE];
    int server_socket, client_socket, r, id_count = 0, v = 1;
    short server_port = 8080;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_len;
    list_t *todo_list = NULL;

    server_socket = socket(PF_INET, SOCK_STREAM, 0);
    if (server_socket < 0)
        exit_with_error("socket() failed");
    if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) < 0)
        exit_with_error("setsockopt() failed");
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(server_port);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(server_socket, (struct sockaddr *)&server_addr,
        sizeof(server_addr)) < 0)
        exit_with_error("bind() failed");

    if (listen(server_socket, 64) < 0)
        exit_with_error("listen() failed");
    printf("Server listening on port %d\n", server_port);
    fflush(NULL);
    while (1)
    {
        client_addr_len = sizeof(client_addr);
        client_socket = accept(server_socket, (struct sockaddr     *)&client_addr,
            &client_addr_len);
        if (client_socket < 0)
            exit_with_error("accept() failed");
        client_name = inet_ntoa(client_addr.sin_addr);
        printf("%s ", client_name);

        if (send(client_socket, "[", strlen("["), 0) < 0)
            exit_with_error("send() failed");
        close(client_socket);
    }

    close(server_socket);
    return (0);
}

void exit_with_error(const char *err)
{
    fprintf(stderr, "%s\n", err);
    exit(1);
}
HoldenGs
  • 87
  • 2
  • 8
  • How are you testing it with curl? What protocol in your URL? – Shawn Aug 10 '18 at 05:55
  • @user3386109 Tried this, didn't change anything – HoldenGs Aug 10 '18 at 05:55
  • HTTP @Shawn . I'm using `curl -D - 'http://localhost:8080/todos' 2> /dev/null | cat -e` – HoldenGs Aug 10 '18 at 05:56
  • 2
    *If you specify URL without protocol:// prefix, curl will attempt to guess what protocol you might want. It will then default to HTTP ...* A single [ is not a valid HTTP response. If you don't redirect stderr to /dev/null do you see a useful error message? – Shawn Aug 10 '18 at 05:59
  • Better test with `netcat`. – alk Aug 10 '18 at 06:00
  • @Shawn So this question is the result of a different issue I had, where I tried to send a proper HTTP response with a single '[' as the content. I ran into the same issue here. Then I tried seeing if I could get a single one to send on it's own, and ran into this issue. I tried with no redirection and I got `curl: (52) Empty reply from server`. However, when I try to send something like '[ something', things work fine. That's why I don't think it's about the response being improperly formatted. – HoldenGs Aug 10 '18 at 06:04
  • A longer string doesn't give you `curl: (56) Recv failure: Connection reset by peer`? – Shawn Aug 10 '18 at 06:09
  • @Shawn Nope. It gets received just fine. – HoldenGs Aug 10 '18 at 06:22
  • Try disabling Nagles algorithm on your server's accepted socket. See [this answer](https://stackoverflow.com/questions/17842406/how-would-one-disable-nagles-algorithm-in-linux) – Andy Brown Aug 10 '18 at 10:34
  • Uh, your HTTP server sends no HTTP headers in its response? A "proper" HTTP 1 response starts with "HTTP/1.x" (where x is 0 or 1), and that is already then more than 6 bytes... – Daniel Stenberg Aug 10 '18 at 12:31
  • @DanielStenberg are you implying that a server isn't supposed to send less than strlen("HTTP/1.x") bytes of data? If so, do you have any idea why that is? Read my previous comment for more context. – HoldenGs Aug 10 '18 at 19:02
  • A server **cannot** send a HTTP response back that is so small yes. Since the response *MUST* be at least a status-line. Which is longer than 6 bytes. – Daniel Stenberg Aug 10 '18 at 20:24
  • @DanielStenberg Then why is it that I can send this response: "[ some". This is not a valid HTTP response, less than the amount of bytes you claim is needed to send a response, yet it STILL sends just fine. – HoldenGs Aug 10 '18 at 20:36
  • ... that's because without status-line and headers, curl tries to consider it to be a "HTTP/0.9" response. But it doesn't know that it is such a "crippled" response until after has read a certain number of bytes (partly because the check is a bit lame). – Daniel Stenberg Aug 10 '18 at 20:39

1 Answers1

0

I figured it out -- @DanielStenberg was right. Apparently, once I send a message with the correct status-line and content-length header, the body data can be sent in a different send() call.

HoldenGs
  • 87
  • 2
  • 8