#include <stdio.h> /* for printf() and fprintf() */
#include <string.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for recv() and send() */
#include <unistd.h> /* for close() */
#define BUFSIZE 4096 /* Size of receive buffer */
void DieWithError(char *errorMessage); /* Error handling function */
char sendBuf[BUFSIZE]; /* Send Buffer */
char rcvBuf[BUFSIZE];
char rcvMsgSize[BUFSIZE];
char srvBuf[BUFSIZE];
char clntBuf[BUFSIZE];
char USER[15];
char PASS[15];
FILE *fileuser;
FILE *filepassword;
int MsgSize;
int result;
char response[3];
void handeltcpclient(int clntSocket)
{
/* Recieve message from client */
memset(&rcvBuf,0,sizeof(rcvBuf));
rcvMsgSize = RecieveMessage(clntSocket, &rcvBuf, sizeof(rcvBuf));
/* Recieve Server Responses */
memset (&clntBuf,0,sizeof(clntBuf));
memset (&srvBuf,0,sizeof(srvBuf));
MsgSize = RecieveMessage(clntSocket,&srvBuf, sizeof(srvBuf));
printf("Recieved: %s" &srvBuf,rcvMsgSize
);
response = strncpy(&srvBuf, 3);
result = strcmp( result, "230");
if( result == 0)
{
sprintf(&clntBuf, "USER %s", USER);
send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
DieWithError("send() sent a different number of bytes than expected");
printf("Recieved: %s" &srvBuf,messageSize
);
/* Opening username.txt to selecting USER */
if (fileuser = (fopen ("usernames.txt", "r")) ==NULL)
{
printf("Error opening file.\n");
return 1;
}
else {
while (fgets(USER, 15, file != NULL))
{
}
/*send USER to server*/
sprintf(clntBuf, "USER %s", USER);
if ( send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf)))
DieWithError("send() sent a different number of bytes than expected");
printf("The attemped username was: %s\n");
return 0;
/* Recieve message from client */
memset(&rcvBuf,0,sizeof(rcvBuf));
rcvMsgSize = RecieveMessage(clntSocket, &rcvBuf, sizeof(rcvBuf));
response = strncpy(&srvBuf, 3);
result = strcmp( result, "331");
if( result == 0)
{
sprintf(&clntBuf, "PASS %s", PASS);
send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
DieWithError("send() sent a different number of bytes than expected");
result = strncpy(&srvBuf, 3);
result = strcmp(result, "230");
if (result == 0)
{
printf("successfully connected.\n");
{
sucess ++;
}
/* Opening passwords.txt to selecting PASS */
(
if (filepassword = (fopen ("passwords.txt", "r"))==NULL)
{
printf("Error opening file.\n");
return 1;
}
(
else {
while (fgets(PASS, 15,file)!=NULL)
{
}
}
/*send PASS to server*/
sprintf(clntBuf, "PASS %s", PASS);
if ( send(clntSocket, &clntBuf, strlen(clntBuf), 0) != (strlen(clntBuf)))
DieWithError("send() sent a different number of bytes than expected");
fclose(file);
printf("The attempted password was: %s\n");
return 0;
}
int RecieveMessage (int s,char *buf, int maxLen)
{
int received = 0;
int rv = 0;
rv = recv(s, buf+received, 5, 0);
while ((received < maxLen) && (rv > 0) && *(buf+received) != '\n')
{
received += rv;
rv = recv(s, buf+received, 5, 0);
}
if (rv < 0)
{
DieWithError("revc() failed");
}
return received;
}
/*end*/
this is the diewitherror.c (error handling file)
#include <stdio.h> /* for perror() */
#include <stdlib.h> /* for exit() */
void DieWithError(char *errorMessage)
{
perror(errorMessage);
exit(1);
}