我在将客户端和密码从客户端传递到服务器以进行身份验证时遇到问题;
服务器端功能
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;
}
答案 0 :(得分:1)
nConnectionDesc = accept(nListenDesc,(struct sockaddr*)&cli,&client_len)
您正在使用client_len
而未初始化它。在致电accept
之前,请尝试:
client_len = sizeof(cli);
答案 1 :(得分:1)
我只是添加到cnicutar并尝试回复您的评论:
char bufer[30]={0};
....
recv(nConnectionDesc,bufer,strlen(bufer),0);
第三个参数必须是缓冲区的大小,而不是内容的长度(这将受到您设置的第一个'\0'
的限制)。由于您bufer
的初始化,strlen(bufer)
将返回0,因此您将永远不会读取任何内容。您应该通过30
而不是strlen(bufer)
。甚至更好:29
,因为您可能希望将最后一个字符设置为'\0'
。我建议你在这里使用一个常数值。
还有其他一行有相同/类似的错误。请注意,带有sizeof(buff)
的{{1}}会返回指针大小。
修改强>
看起来我有点天真...有更多的错误需要指出......让我再看一遍......
char buff[20]
您是如何初始化int sockfd,n;
...
send(sockfd,buff,strlen(buff),0);
的?我不认为你能用这个做任何事情......初步适当!可能有更多的错误..