Hello I am working on a project called a Remote Backup system. It's a TCP client&sever program written in C language. I was tasked with the server part. Below is my code, how would I probably get the username and password part to implement into the main class? So that it works like the user enters the username and password if it's wrong they don't log in but if it's right they log in and it shows the correct username and password and filepath.
Also I know my code has a few warnings they will be addressed later
Thank you for the help!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/stat.h>
#include <time.h>
#define BACKLOG 5
void username()
{
char username[20];
char password[20];
char cwd[1024];
chdir("/home/users/cprograms/JohnS&JosephL");
getcwd(cwd, sizeof(cwd));
printf("Username:");
gets(username);
printf("Password:");
int p=0;
while(password[p-1]!='\r');
password[p-1]='\0';
if((strcmp(username,"RemoteBackup")==0 && strcmp(password,"ICSI333")==0))
{
printf("\n Logging into", username, password, cwd);
}
else
{
printf("Wrong no log in! \n");
}
}
int main(int argc, char *argv[])
{
int sockfd, new_fd, ssize_t, sin_size;
char buf[512];
struct addrinfo hints, *servinfo, *cur;
struct sockaddr_storage their_addr;
struct stat st;
FILE *fp;
int fileSize;
char *msg = "OK";
chdir(argv[1]);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
getaddrinfo(NULL, argv[2], &hints, &servinfo);
sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
bind(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);
printf("Listening for new connections.\n");
listen(sockfd, BACKLOG);
while((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) >= 0)
{
sin_size = sizeof(their_addr);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
printf("Connected to a client. \n");
ssize_t = recv(new_fd, buf, sizeof(buf), 0);
char * token = strtok(buf, " ");
if(token != NULL)
{
if(token == "PUSH")
{ //reading first token of client command
token = strtok(NULL, " ");
fp = fopen (token, "w");
token = strtok(NULL, " ");
fileSize = atoi(token);
token = strtok(NULL, " ");
fputs(token, fp);
fclose(fp);
ssize_t = send(new_fd, msg, strlen(msg), 0);
token = NULL;
}
else if(token == "PULL")
{ //first word of recv
token = strtok(NULL, " ");
stat(token, &st);
fileSize = st.st_size;
char *sizeMsg;
sprintf(sizeMsg, "%d", fileSize);
ssize_t = send(new_fd, sizeMsg, strlen(sizeMsg), 0);
fp = fopen (token, "r");
if(fgets(buf, 512, fp) != NULL)
{ //get bytes from file
ssize_t = send(new_fd, buf, strlen(buf), 0);
}
ssize_t = send(new_fd, msg, strlen(msg), 0);
fclose(fp);
}
else
{
printf("Data recieved not in correct format\n");
close(sockfd);
}
}
close(sockfd);
}
freeaddrinfo(servinfo);}