Ok let me explain this more...
Im making a sort-of remote computer control. Im not adding GUI yet, but i wanted at least to have control to the CMD. because you can do allot from the CMD. so im doing this with a predefined winsock class i found.
#pragma once
#include "Winsock2.h"
const int STRLEN = 256;
class Socket
{
public:
Socket();
~Socket();
bool SendData( char* );
bool RecvData( char*, int );
void CloseConnection();
char* GetMessage();
protected:
WSADATA wsaData;
SOCKET mySocket;
SOCKET myBackup;
SOCKET acceptSocket;
sockaddr_in myAddress;
};
class ServerSocket : public Socket
{
public:
void Listen();
void Bind( int port );
void StartHosting( int port );
};
class ClientSocket : public Socket
{
public:
void ConnectToServer( const char *ipAddress, int port );
};
Socket::Socket()
{
if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
{
cerr << "Socket Initialization: Error with WSAStartup\n";
system("pause");
WSACleanup();
exit(10);
}
//Create a socket
mySocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( mySocket == INVALID_SOCKET )
{
cerr << "Socket Initialization: Error creating socket" << endl;
system("pause");
WSACleanup();
exit(11);
}
myBackup = mySocket;
}
Socket::~Socket()
{
WSACleanup();
}
bool Socket::SendData( char *buffer )
{
send( mySocket, buffer, strlen( buffer ), 0 );
return true;
}
bool Socket::RecvData( char *buffer, int size )
{
int i = recv( mySocket, buffer, size, 0 );
buffer[i] = '\0';
return true;
}
void Socket::CloseConnection()
{
closesocket( mySocket );
mySocket = myBackup;
}
void ServerSocket::StartHosting( int port )
{
Bind( port );
Listen();
}
void ServerSocket::Listen()
{
if ( listen ( mySocket, 1 ) == SOCKET_ERROR )
{
cerr << "ServerSocket: Error listening on socket\n";
system("pause");
WSACleanup();
exit(15);
}
acceptSocket = accept( myBackup, NULL, NULL );
while ( acceptSocket == SOCKET_ERROR )
{
acceptSocket = accept( myBackup, NULL, NULL );
}
mySocket = acceptSocket;
}
void ServerSocket::Bind( int port )
{
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = inet_addr( "0.0.0.0" );
myAddress.sin_port = htons( port );
if ( bind ( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress) ) == SOCKET_ERROR )
{
cerr << "ServerSocket: Failed to connect\n";
system("pause");
WSACleanup();
exit(14);
}
}
void ClientSocket::ConnectToServer( const char *ipAddress, int port )
{
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = inet_addr( ipAddress );
myAddress.sin_port = htons( port );
if ( connect( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress ) ) == SOCKET_ERROR )
{
cerr << "ClientSocket: Failed to connect\n";
system("pause");
WSACleanup();
exit(13);
}
}
I made this same program before and it worked. But i wanted it to be easier to add commands. So i made a class for it. In this class it would connect to the client. The server sends info to the client, the client takes that and puts it into system(); then it sends back "Done".
This all works for EVERY COMMAND POSSIBLE exept for "taskkill /IM notoepad.exe"
could it possible be because i use a config.ini file to get the ip? may it not have closed correctly? Heres how i get the ip:
char ip[256];
ifstream fin("config.ini");
if(!fin)
sprintf(ip,"127.0.0.1"); // default ip (Loopback)
else
{
fin.getline(ip,256);
fin.close();
}
then i plug that ip so i can connect to it.
I dont know why every command works fine exept one. Its really weird. Thanks for your help again.