c - Server to Validate Credentials (Socket Programming) -
first of all, i'm beginner in c. program simulates server client connects via telnet on port 8000 , give username , password , server validates them. username client, have problem validating it. need compare username list of credentials, doesn't seem work. here's code i've written far...
#include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> int main() { char * credentialslist[7][2] = {{"alice","abcdef"}, {"bob","1234567"}, {"cindy","qwerty"}, {"david","abababab"}, {"eve", "cdefgh"}, {"frank","7654321"}, {"george", "12341234"}}; int serversocket=socket(af_inet, sock_stream, 0); int new_socket, i; char *message, client_message[10]; struct sockaddr_in server, client; server.sin_family = af_inet; server.sin_addr.s_addr = inaddr_any; server.sin_port = htons(8000); bind(serversocket, (struct sockaddr *)&server, sizeof(server)); listen(serversocket,2); int c = sizeof(struct sockaddr_in); new_socket = accept(serversocket, (struct sockaddr *)&client, (socklen_t*)&c); message = "welcome! connected server.\n\n"; write(new_socket, message, strlen(message)); message = "please enter valid username: "; write(new_socket, message, strlen(message)); recv(new_socket, client_message, 10, 0); (i=0; i<7; i++) { if (strcmp(credentialslist[i][0], client_message) == 0) { // puts("got you"); } } return 0; }
thanks in advance :)
in c programming string terminated null character (ie;'\0' hex value 0x00). @juhist says end data received using null character, here i'll clear whole client_message[] array null charatcer if receive , lets suppose 8 character 9th character terminated null character ensure that's string
memset(client_message,0x00,sizeof(client_message)); recv(new_socket, client_message, 10, 0); (i=0; i<7; i++) { if (strcmp(credentialslist[i][0], client_message) == 0) { // puts("got you"); } }
Comments
Post a Comment