Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RemoteCommand prints "Command Sent" suggesting that the command was sent successful, but exitcode is 1 #801

Open
mytechguyri opened this issue Feb 27, 2024 · 0 comments

Comments

@mytechguyri
Copy link

mytechguyri commented Feb 27, 2024

Its not immediately visible to the end user that exit code was 1, to the end user, it simply appears that the command was sent successfully. Here is a screen grab of the behavior I'm observing

`john@wa1okb-r:~$ RemoteCommand 7624 page 0129939 "This is a test message"

Command sent: "page 0129939 This is a test message" to port: 7624

john@wa1okb-r:~$ echo $?

1
`
As you can see, the end user only gets the Command sent: message... It appears in all respects that it sent successfully... yet echo $? shows the return code from the command was 1, indicating it was NOT successful.... and no indication as to why it wasn't successful.

Propose changing the code such that it doesn't print the Command Sent message until after its actually verified that the command was sent successfully, and that the return code is 0

It appears that the "Command sent: " line is printed after the command is successfully written to the socket with the socket.write method. The return code of 1, however, is set later in the CRemoteCommand::send method if there’s an error or no data is read from the socket after sending the command.

I apologize, as I don't know C++ well enough to confidently submit a code change via github... but from what I do understand, i would suggest moving the ::fprintf(stdout, "Command sent: \"%s\" to port: %u\n", command.c_str(), m_port); to the CRemoteCommand::send method, so that it only prints once we know its actually been successful. something like this:

`int CRemoteCommand::send(const std::string& command)
{
sockaddr_storage addr;
unsigned int addrLen;
char buffer[BUFFER_LENGTH];
int retStatus = 0;

if (CUDPSocket::lookup("127.0.0.1", m_port, addr, addrLen) != 0) {
    ::fprintf(stderr, "Unable to resolve the address of the host\n");
    return 1;
}

CUDPSocket socket(0U);

bool ret = socket.open(addr);
if (!ret)
    return 1;

ret = socket.write((unsigned char*)command.c_str(), command.length(), addr, addrLen);
if (!ret) {
    socket.close();
    return 1;
}

std::this_thread::sleep_for(std::chrono::milliseconds(50));

int len = socket.read((unsigned char*)buffer, BUFFER_LENGTH, addr, addrLen);
if (len > 0) {
    buffer[len] = '\0';
    ::fprintf(stdout, "Command sent: \"%s\" to port: %u\n", command.c_str(), m_port);
    ::fprintf(stdout, "%s\n", buffer);
}
else
{
    retStatus = 1;
}

socket.close();

return retStatus;

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant