Monday, May 24, 2010

C++ send a remote command?

Using winsock, I am trying to make a server that you can send a command to, and it will execute that command. For example, if you connect to it using telnet and send the letter 'N', the notepad will open up. Here's some of the code:





SOCKET sock = (SOCKET)lpParam;


char szRecvBuff[1024],


szSendBuff[1024];


int ret;





while (true)


{


// Receive data


ret = recv(sock, szRecvBuff, 1024, 0);


// Check the recieved data


if (ret == 0)


break;


else if (ret == SOCKET_ERROR)


{


cout %26lt;%26lt; "Recieve data failed!" %26lt;%26lt; endl;


break;


}


szRecvBuff[ret] = '\0';


cout %26lt;%26lt; szRecvBuff %26lt;%26lt; endl;


while(true)


{


if (szRecvBuff == "N")


{


system("START notepad.exe");


cout %26lt;%26lt; "notepad.exe started" %26lt;%26lt; endl;


}


}





The received data ("N") appears in the console window, but the message "notepad.exe started" never shows up, and the notepad doesn't start up. What is the problem?

C++ send a remote command?
... first :





why you use a loop to check che command?





2nd .. you can't use the operator == to compare a buffer with another .. it's compare only the address of array and the result is false ..





use strcmp or szRecvBuff[0] == 'N' ...





3th .. use CreateProcess api to run a process under windows.. system isn't standard, and the function lock your code until the process run..


No comments:

Post a Comment