Passwortabfrage bei Socketconnection



  • Hallo erstmal!
    Also ich hab folgends Problem...
    Ich versuche eine Socketverbindung zu erstellen... das klappt auch wunderbar, aber bei der PW abfrage scheitert es...
    Es kommt der Text "Passwort eingeben: " und dann gebe ich ihm das richtige Kennwort ein, aber er geht einfach nur eine Zeile runter...

    Wisst ihr woran das liegt?
    Oder habt ihr ein Beispielscript welches ich verwenden kann?

    Schonmal danke 😃

    while(exit == 0)
     {
      int shell = 0;
      s2=accept(s1,(struct sockaddr *)&sockadd,&len);
      send(s2,"Passwort eingeben: ",19,0);
      ZeroMemory(&SI,sizeof(SI));
      ZeroMemory(&PI,sizeof(PI));
      SI.cb = sizeof(SI);
      SI.dwFlags=STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
      SI.wShowWindow   = SW_HIDE;
    
      char* buffer;
      buffer = new char [1028];
    
      char pass[4];
      strcpy(pass, "test");
      int bytes;
      // ab hier fängt er an das PW abzufragen...
      buffer = recv(s2, buffer, 1027, 0);
    
      if(!strcmp(buffer, pass)) {
    ....
    ....
    //uninteressant ^^
    


  • Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum WinAPI verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • 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...


Anmelden zum Antworten