-1

我在将用户名和密码从客户端传递到服务器进行身份验证时遇到问题;

服务器端功能

        void Start_Server()
        {
                struct sockaddr_in serv;
                struct sockaddr_in cli;
                socklen_t client_len;
                //char username[10];
                char bufer[30]={0};
                char *buf="Connection Successful";
                char cPassword[20];

                pthread_t threadID;
                int nListenDesc;                                        // server socket descriptor
                int nConnectionDesc=-1;                                 // client socket descriptor
                int nRetvalue;                                          //storing return value of authentication        

                nListenDesc=socket(AF_INET,SOCK_STREAM,0);
                serv.sin_family=AF_INET;
                serv.sin_addr.s_addr=inet_addr(IP);                     // assigning ip address 
                serv.sin_port=htons(PORT);                              //assigning port address        
                if(nListenDesc==-1)
                        perror("socket descriptor not created");
                bind(nListenDesc,(struct sockaddr*)&serv,sizeof(serv)); //assigning address to socket
                if((system("mkdir /tmp/monitor/"))==-1)                 // creating directory
                        puts("directory not created");
                listen(nListenDesc,5);
                while(1)
                {   if((nConnectionDesc = accept(nListenDesc,(struct sockaddr*)&cli,&client_len))==-1)
                                perror("error in nConnectionDesc");
                        recv(nConnectionDesc,bufer,strlen(bufer),0);
                        puts(bufer);
                        sscanf(bufer,"%s %s",cUsername,cPassword);
                        printf("%s %s",cUsername,cPassword);
                        if((nRetvalue=authenticate(cUsername,cPassword,nConnectionDesc))==1)    // passing username and password for 
                        {                                                       // authentication
                                puts("user not authenticated ");
                                exit(0);
                        }

                        send(nConnectionDesc,buf,strlen(buf),0);
                        if (pthread_create(&threadID, NULL, &serverThread,(void *) nConnectionDesc) != 0) //creating thread
                        {
                                perror("Thread create error");
                                close(nConnectionDesc);
                                close(nListenDesc);
                                exit(1);
                        }
                        //pthread_detach(pthread_self());
                        printf("Parent ready for another connection\n");


                }



int authenticate(char cUser[20],char cPass[20],int nAsock)
{
        struct loginStruct sAuthent[10];        //creating object of loginStruct
        int nIndex=0;
        int nFiledesc;                          //file descriptor 

        strcpy(sAuthent[0].cUname,"aviral");    // passing value to     
        strcpy(sAuthent[0].cPaswd,"aviral");    // structure variables
        strcpy(sAuthent[1].cUname,"avinash");
        strcpy(sAuthent[1].cPaswd,"avinash");
        for(; nIndex<3;nIndex++)
        {
                if(strcmp(cUser,sAuthent[nIndex].cUname) ==0)   // authenticate username
                {
                        if(strcmp(cPass,sAuthent[nIndex].cPaswd)==0) //authenticate password
                        {
                                puts("cUser good");
                                char cPath[20]="/tmp/monitor/";
                                strcat(cPath,cUser);
                                puts(cPath);
                                nFiledesc=creat(cPath,S_IRWXU);         //creating user file  
                                close(nFiledesc);
                                return 0;
                        }

                }
                else
         {
                //      puts("User not Authenticates");

                }


        }       return 1;

}

客户端

    void Start_Client()
    {
            struct sockaddr_in serv;
            int sock=-1;
            sock=socket(AF_INET,SOCK_STREAM,0);
            memset(&serv,0,sizeof(struct sockaddr_in));
            serv.sin_family=AF_INET;
            serv.sin_addr.s_addr=inet_addr(IP);
            serv.sin_port=htons(PORT);
            if(sock==-1)
                    perror("Socket not created");
            if((connect(sock, (struct sockaddr*)&serv,sizeof(serv)))!=0)

                    perror("connection not established");
            login(sock);
            transact_with_serv(sock);
    }
    int login()
    {
                    char carUser[20];
                    char carPass[20];
                    int sockfd,n;
                    char buff[20];
                    printf("Enter username:");
                    scanf("%s",carUser);
                    printf("Enter Password:");
                    scanf("%s",carPass);
                    sprintf(buff,"%s %s",carUser,carPass);
                    send(sockfd,buff,strlen(buff),0);
                    n=recv(sockfd,buff,sizeof(buff),0);
                    puts(buff);
                    return sockfd;
    }
4

2 回答 2

1

我只是添加到 cnicutar 并尝试回复您的评论:

char bufer[30]={0};
....
recv(nConnectionDesc,bufer,strlen(bufer),0);

第三个参数必须是缓冲区的大小,而不是内容的长度(受'\0'您设置的第一个参数的限制)。由于您对bufer,的初始化strlen(bufer)将返回 0,因此您将永远不会阅读任何内容。你应该通过30而不是strlen(bufer). 甚至更好:29,因为您可能希望将最后一个字符设置为'\0'. 我建议你在这里使用一个常数值。

还有其他行具有相同/相似的错误。请注意,sizeof(buff)withchar buff[20]将返回指针的大小。

编辑:

看来我有点幼稚了……还有很多错误要指出……让我再看一遍……

int sockfd,n;
...
send(sockfd,buff,strlen(buff),0);

你是如何初始化你的sockfd?我不认为你可以用这个做任何事情......适当地初始化它!可能还有比这更多的错误..

于 2012-08-22T19:37:09.907 回答
1
nConnectionDesc = accept(nListenDesc,(struct sockaddr*)&cli,&client_len)

您正在使用client_len而不初始化它。在调用之前accept,请尝试:

client_len = sizeof(cli);
于 2012-08-22T19:15:10.127 回答