0

How can I send a command and Handle the command in c++ for listener.

I want something like the below code....

switch (args[i][1])
                {
                    case 'command1':  
                        command1function();
                        break;
                    case 'command2':
                        command2function();
                        break;
                    case 'command3':
                        command3function();
                        break;
                    default:
                        return 0;
                }

TCP Listener c++ (Please see the GITHUB link this is not my project however I have been trying for hours now with no luck at all, Any help would be greatly appreciated) https://github.com/GreatBullet/Listener_Project/

void Class_tcp_Listener::Run()
{
    char buf[MAX_BUFFER_SIZE];
    while (true) 
    {
        //create a listening socket
        SOCKET listening = CreateSocket();
        if (listening == INVALID_SOCKET)
        {
            break;
        }
        SOCKET client = WaitForConnection(listening);
        if (client != INVALID_SOCKET)
        {
            closesocket(listening);
            int bytesReceived = 0;
            do
            {
                ZeroMemory(buf, MAX_BUFFER_SIZE);
                bytesReceived = recv(client, buf, MAX_BUFFER_SIZE, 0);
                if (bytesReceived > 0) 
                {
                    if (MessageReceived != NULL)
                    {
                        MessageReceived(this, client, std::string(buf, 0, bytesReceived));
                        std::cout << std::string(buf, 0, bytesReceived) << '\n';
                    }
                }
            } while (bytesReceived > 0);
            closesocket(client);
        }
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
JayFS
  • 1
  • 1
    [You cannot switch-case with string](https://stackoverflow.com/questions/4480788/c-c-switch-case-with-string/4480801), thus make your callback function like: `void Listener_MessageReceived(Class_tcp_Listener* listener, int client, std::string msg) { if(msg == "command1") { command1function(); else if(msg == "command2") ... else { defaultcommand(); } }` – user1810087 Mar 07 '20 at 22:38
  • Thanks for that How would go about sending multiple arguments and separating them ??? – JayFS Mar 07 '20 at 23:13
  • You need to choose a protocol and actually implement it. TCP is not a message protocol. If you need messages, you need to use some protocol (typically layered on top of TCP) that supports them. You are calling a `MessageReceived` function, but your code does not check to see if it actually received a *message*. The `recv` function has no idea what a message is and thus no way to know whether or not it has received a message as opposed to half a message, two messages, a message and half another message, and so on. – David Schwartz Mar 07 '20 at 23:44

1 Answers1

0

You can use std::unordered_map to store function wrappers:

#include <iostream>
#include <string>
#include <unordered_map>
#include <functional>

void command3() {
    std::cout << "command3\n";
}

int main() {
    std::unordered_map<std::string, std::function<void(void)>> commands;
    commands["command1"] = [] {std::cout << "command1\n"; };
    commands["command2"] = [] {std::cout << "command2\n"; };
    commands["command3"] = command3;

    std::string args = "command3";
    auto it = commands.find(args);
    if (it != commands.end()) {
        it->second(); 
    } else {
        std::cerr << "Invalid command: " << args << "\n";
    }
}

P.S. It is possible that pattern matching will be added in future standards.

Dessus
  • 301
  • 2
  • 10