Ejemplo n.º 1
0
/**
 * \brief write a message to a client
 * \param mt is the minstack_tcp stack
 * \param cid is the client ID to whom send the message, if 0 the message is broadcast to the clients
 * \param message is the message
 * \param len_message is the length of the message
 * \return 0 if OK
 */
int minstack_tcp_write_to_client(minstack_tcp *mt, int cid, char *message,
        int len_message) {
    //without cid we send the message to all the clients
    if (!mt) {
        //printerror("the minstack_tcp is NULL\n");
        return -100;
    }
    if (mt->type != SERVER) {
        printwarning("The minstack is not a server...\n");
        return -1;
    }
    if (mt->status != STARTED) {
        printwarning("The server is not started yet\n");
        return -1;
    }
    if (cid)
        return minstack_tcp_write(mt, cid, message, len_message);
    else {
        int i, retval = 0;
        for (i = 0; i < mt->sockets.connected_client_nb; i++) {
            retval = minstack_tcp_write(mt, mt->sockets.client_socket_fd[0],
                    message, len_message);
            if (retval) {
                printerror("We had a problem sending to the client %d\n",i);
                return retval;
            }
        }
    }
    return 0;
}
Ejemplo n.º 2
0
/**
 * \brief write a message to the server where we are connected
 * \param mt is the minstack_tcp stack
 * \param message is the message
 * \param len_message is the length of the message
 * \return 0 if OK
 */
int minstack_tcp_write_to_server(minstack_tcp *mt, char *message, int len_message) {
    if (!mt) {
        //printerror("the minstack_tcp is NULL\n");
        return -100;
    }
    if (mt->status != STARTED) {
        printwarning("The client is not started yet\n");
        return -1;
    }
    return minstack_tcp_write(mt,mt->listen_socket_fd, message, len_message);
}