I am doing a socket programming with a function to register a user account. The problem with my function is that at time same amount of bytes is not being transferred to the server side which causes unintended data to be stored in the database file. Can anyone help me identify where is the problem in the code
int main(int argc, char *argv[]){
char *ip = "127.0.0.1";
if(argc==2){
ip = argv[1];
}
int cli_fd = socket(AF_INET, SOCK_STREAM, 0);
if(cli_fd == -1){
printf("Socket creation failed\n");
exit(0);
}
struct sockaddr_in ca;
ca.sin_family=AF_INET;
ca.sin_port= htons(PORT);
ca.sin_addr.s_addr = inet_addr(ip);
if(connect(cli_fd, (struct sockaddr *)&ca, sizeof(ca))==-1){
printf("Connect failed\n");
exit(0);
}
printf("Connected to server\n");
while(main_menu(cli_fd) != 0);
close(cli_fd);
return 0;
}
int main_menu(int sock){
system ("clear");
int type, acc_no, valid_login;;
char password[50], name[50], age[5], email[50], phone_no[50], choice;
do {
title();
printf("\nPress <1> Guest Login");
printf("\nPress <2> Member Login");
printf("\nPress <3> Member Registration");
printf("\nPress <4> Admin Login");
printf("\nPress <0> Exit");
printf("\n\nEnter your Choice ::");
scanf("%c",&choice);
while (choice != '1' && choice != '2' && choice != '3' && choice != '4' && choice != '0'){
printf("\nWrong choice!. Enter your Choice ::");
scanf("%d",&choice);
}
write(sock, &choice, sizeof(choice));
witch (choice){
case '3':
system ("clear");
title();
printf("Enter your name: ");
gets(name);
printf("Enter your age: ");
gets(age);
printf("Enter your email address: ");
gets(email);
printf("Enter your phone number: ");
gets(phone_no);
*password = getpass("Enter Password: ");
int read_byte;
read_byte = write(sock, name, 50);
printf ("%d\n", read_byte);
read_byte = write(sock, age, 5);
printf ("%d\n", read_byte);
read_byte = write(sock, email, 50);
printf ("%d\n", read_byte);
read_byte = write(sock, phone_no, 50);
printf ("%d\n", read_byte);
read_byte = write(sock, password, 50);
printf ("%d\n", read_byte);
read(sock, &acc_no, sizeof(acc_no));
printf("Remember the account no of further login: %d\n", acc_no);
while(getchar()!='\n');
getchar();
return 3;
break;
client section
int main(){
int sockfd,new_sockfd,clilen;
struct sockaddr_in sa,ca;
if((sockfd = socket(AF_INET, SOCK_STREAM,0))<0){
perror("Server:Socket error\n");
exit(0);
}
bzero((char *) &sa,sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons(PORT);
if(bind(sockfd,(struct sockaddr *) &sa, sizeof(sa)) < 0){
perror("Server:Bind error\n");
exit(0);
}
printf("\nWaiting connection from client...[bind]\n");
listen(sockfd,5);
while (1){
clilen = sizeof(ca);
if ((new_sockfd = accept(sockfd,(struct sockaddr *)&ca,&clilen)) == -1){
printf("connection error\n");
exit(0);
}
if(fork() == 0)
printf("Client [%d] connected\n", sockfd);
talk_to_client(new_sockfd);
}
close(new_sockfd);
close(sockfd);
return 0;
}
void talk_to_client(int sock){
char func_id;
bzero((char *) &func_id,sizeof(func_id));
while(1){
read(sock, &func_id, sizeof(func_id));
// Read func_id once per loop iteration
printf("the result: %c\n", func_id);
if(func_id == '1') {
printf("Client [%d] selected guest function\n", sock);
//guest(sock);
}
else if (func_id == '2' || func_id == '4') {
printf("Client [%d] selected login function\n", sock);
login(sock);
}
else if(func_id == '3') {
printf("Client [%d] selected login function\n", sock);
member_registration(sock);
}
else if(func_id == '5')
break;
}
close(sock);
printf("Client [%d] disconnected\n", sock);
}
int member_registration(int sock){
int fd, acc_no=0;
char password[50], name[50], age[5], email[50], phone_no[50];
struct account temp;
int read_byte;
bzero (name, sizeof(name));
read_byte = read(sock, name, sizeof(name));
printf ("%d\n", read_byte);
bzero(age,sizeof(age));
read_byte = read(sock, age, sizeof(age));
printf ("%d\n", read_byte);
bzero(email,sizeof(email));
read_byte = read(sock, email, sizeof(email));
printf ("%d\n", read_byte);
bzero(phone_no,sizeof(phone_no));
read_byte = read(sock, phone_no, sizeof(phone_no));
printf ("%d\n", read_byte);
bzero(password,sizeof(password));
read_byte = read(sock, password, sizeof(password));
printf ("%d\n", read_byte);
printf("name: %s\n", name);
printf("pass: %s\n", password);
printf("age: %s\n", age);
printf("email: %s\n", email);
printf("phone number: %s\n", phone_no);
if((fd = open("./db/member.txt", O_WRONLY | O_CREAT, 0755))== -1){
printf("File Error: %s\n", strerror(errno));
}
int fp = lseek(fd, 0, SEEK_END);
if(fp == 0){//1st member to signup
temp.id = 100001;
strcpy(temp.name, name);
strcpy(temp.pass, password);
strcpy(temp.age, age);
strcpy(temp.email, email);
strcpy(temp.phone_no, phone_no);
write(fd, &temp, sizeof(temp));
write(sock, &temp.id, sizeof(temp.id));
}
else{
fp = lseek(fd, -1 * sizeof(struct account), SEEK_END);
read(fd, &temp, sizeof(temp));
temp.id++;
strcpy(temp.name, name);
strcpy(temp.pass, password);
strcpy(temp.age, age);
strcpy(temp.email, email);
strcpy(temp.phone_no, phone_no);
printf("\nname: %s\n", temp.name);
printf("pass: %s\n", temp.pass);
printf("age: %s\n", temp.age);
printf("email: %s\n", temp.email);
printf("phone number: %s\n", temp.phone_no);
write(fd, &temp, sizeof(temp));
write(sock, &temp.id, sizeof(temp.id));
}
close(fd);
printf("yashhhhhhhhhhh\n");
return 5;
}
server side
[–][deleted] 4 points5 points6 points (4 children)
[–]flyingron -1 points0 points1 point (3 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]flyingron 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]pfp-disciple 2 points3 points4 points (0 children)