認証のためにクライアントからサーバーにユーザー名とパスワードを渡す際に問題があります。
サーバーサイド機能
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;
}