-1

Hey guys I want to send a message of the current date from the server to a client in C. So I would use a command like this for the client;

Terminal

telnet localhost PORT

What should be the command to send the actual message?

int main(int argc, char *argv[])
{
    int socket_desc, client_sock, c, read_size;
    struct sockaddr_in server, client;
    char client_message[2000];
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);

    
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }

    
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(PORT);

    
    if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
    {
        
        perror("bind failed. Error");
        return 1;
    }

    
    listen(socket_desc, 3);
    
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);

    
    client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c);
    if (client_sock < 0)
    {
        perror("accept failed");
        return 1;
    }
    puts("Connection accepted");
    
////////////////////////////////////////////////////
        //the actual message here
////////////////////////////////////////////////////    
    close(socket_desc);

    return 0;
}
  • 2
    `send()` is the function to send data through a socket. – Eugene Sh. Mar 25 '21 at 17:38
  • 1
    You can `send()` whatever you want. Send the raw `time_t` as-is. Send the contents of the `tm`. Send the result of `asctime()`. You are writing the server, pick whatever format you want. – Remy Lebeau Mar 25 '21 at 17:49
  • 1
    Unfortunately this is a corner case. You should not close a socket immediately after sending data. You could sleep (0.1 second) or look for [graceful shutdown](https://stackoverflow.com/a/48586163/3545273) – Serge Ballesta Mar 25 '21 at 17:50

1 Answers1

0

I made the following addiction and it worked

Is this a proper solution? I don't know how to use send() properly yet.

pid_t child_pid = fork();
if (child_pid == 0)
{
    snprintf(client_message, sizeof(client_message), "%s", ctime(&tick));
    write(client_sock, client_message, strlen(client_message));
    shutdown(client_sock, SHUT_RDWR);                                     
    while (read(client_sock, client_message, sizeof(client_message) > 0)) 
        close(client_sock);                                               
}
else if (child_pid > 0)
{
    // parent
    close(client_sock); 
}
else
{
    // a fork error occurred, handle and remember to close(connfd)
}
  • The code in this answer *isn't* correct. The loop will read once, close the socket and then attempt to read again from the closed socket leading to an error. It doesn't make sense. – Some programmer dude Mar 26 '21 at 08:50
  • By closing the socket only when `read` returns `-1` (i.e. there's an error) or `0` (i.e. the connection was closed). – Some programmer dude Mar 28 '21 at 17:34