Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

.Paulo Hnrique (PH)

Servidor controla cliente usando Socket

Recommended Posts

Galera beleza??

 

To com uma duvida de como fazer que meu servidor mande comandos (tipo para desligar, Reiniciar a máquina, coletar o uso de CPU, Memória e Disco; Coletar o número de processos em execução e o número de usuários logados;Parar e iniciar processos no cliente;Fazer o cliente abrir um site remoto) tudo isso usando socket...

 

ajuda muito se me derem uma ideia de como mandar comandos que o cliente execute.. (tipo: mandar ao cliente "init 0", "ls" e etc...)

 

O meu codigo ta ai embaixo, mas somente manda uma mensagem ao servidor e log tudo... alguem me ajudaa???

 

 

SERVIDOR:

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

#include <time.h>
#include <conio.h>
#include <ntdlib.h>



#define MYPORT 999                 //porta de conexao!!!

#define BACKLOG 10



int main()

{

int fd, fd2;
int txt[30];
int tam = 1024;

char *newl = "\n";

char *ipa = "Endereco IP:  ";

char *texto = malloc(tam);

int op;


struct sockaddr_in server;

struct sockaddr_in client;



int sin_size;



if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

    perror("socket");

    exit(1);

}



server.sin_family = AF_INET;

server.sin_port = htons(MYPORT);

server.sin_addr.s_addr = INADDR_ANY;

memset(&(server.sin_zero), '/0', 8);



if (bind(fd,(struct sockaddr *)&server,sizeof(struct sockaddr)) == -1) {

    perror("bind");

    exit(1);

}



if (listen(fd,BACKLOG) == -1) {

    perror("listen");

    exit(1);

}



while(1) {



sin_size = sizeof(struct sockaddr_in);

if ((fd2 = accept(fd, (struct sockaddr *)&client,&sin_size)) == -1) {

    perror("accept");

    exit(1);


}



FILE *file_ptr;

file_ptr = fopen("conexoes.txt", "a");    // Salva as conexoes em um bloco de texto!"

{

time_t timer;

timer=time(NULL);



fputs(ipa, file_ptr);

fputs(inet_ntoa(client.sin_addr), file_ptr);

fputs(newl, file_ptr);

fputs(asctime(localtime(&timer)), file_ptr);

fputs(newl, file_ptr);
fclose(file_ptr);

}


printf("Enviar comando:");
gets(texto);
send(fd2,texto,tam,0);
recv(fd2,texto,tam,0);

printf("\n");



printf("Conexao recebida de: %s \n", inet_ntoa(client.sin_addr) );



time_t timer;

timer=time(NULL);

printf("Log: %s \n", asctime(localtime(&timer)) ); // Escreve quando a conexao eh estabelecida!!!




close(fd2);

}

}

CLIENTE :::

 

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>



#define PORT 999      // Porta para conectar!!!

#define DATASIZE 100



int main(int argc, char *argv[])

{



int sock, numbytes;

char buf[DATASIZE];

struct hostent *h;

struct sockaddr_in their_addr;



if(argc != 2) {

printf("AJUDA!!!: %s <host/ip> \n", argv[0]);

exit(1);

}



if ((h=gethostbyname(argv[1])) == NULL) {

perror("gethostbyname");

exit(1);

}



if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror("socket");

exit(1);

}



their_addr.sin_family = AF_INET;

their_addr.sin_port = htons(PORT);

their_addr.sin_addr = *((struct in_addr *)h->h_addr);



if (connect(sock, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1) {

perror("connect");

exit(1);

}



if ((numbytes=recv(sock, buf, DATASIZE-1, 0)) == -1) {

perror("recv");

exit(1);

}

buf[numbytes] = '/0';

printf("Dados recebidos: %s \n", buf);

close(sock);

return 0;

}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Paulo, o trabalho todo deve ser feito no cliente.

Primeiro você deve estabelecer um protocolo de comunicação.

Então, você estabelece os comandos.

 

Por exemplo:

### cliente ###
while client.ReceiveData():
    case client.CurrData() of:
        1 -> Shutdown()
        2 -> SendCPUInfo()
        3 -> SendMemoryInfo()
        4 -> EndConnection()

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.