• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Please need some help

This server program recive a connection from many clients at the same time
at the begning it sends a list of avalible files to the client
the client, then , choose one file and send the file name to the server
the server after receiving the file name call a function (insert name to store the file withen an array of structure called filename, after checking that the file does not exist in the array.
if the file exist then it will send a message that the file is locked.

the probelm is , when the function receive connections from clients
it store the first file name ,and when trying to store the second file , the first file disappear from the list , please help me to solve this problem.... i haven't got alot of points if i do i will make it for 1000 points.


=====================
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
#include <sys/stat.h>

#define MYPORT 3000
#define BACKLOG 10
#define NAME 20
#define MAXDATASIZE 100


//a structure to save the file name
struct filename
{
char fname[64];
};

void insert_name(char name[10], int loop);

struct filename store_file[20];
//static int loop=-1

int main()
{
 char *file,*lock;
 char check;
 char buf[MAXDATASIZE],cmd[100],f_name[100];
 char *file_name;
 time_t name1;
  char data[MAXDATASIZE],mod;
//static int loop=-1;
int sockfd, new_fd,numbytes,len=64,c,test=0,match,val,i,loop,j,zero=0;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
struct stat results;
//struct filename store_file[20];


int sin_size;
FILE *fp, *fd;
//****************************

  int choice = 2;
  int flag = 0;


//****************************
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}

my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(MYPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
perror("bind");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen");
exit(1);
}


while(1)
{
sin_size=sizeof(struct sockaddr_in);
if ((new_fd=accept(sockfd,(struct sockaddr *)&their_addr,&sin_size))==-1)
{
perror("accept");
continue;
}


//increment loop used to store files in array
//loop=loop++;
/*this loop is used to increment the array list ,i read the value from output file*/

fd=fopen("lock","r");
if (fd==NULL)
printf("can't find the file\n");
fscanf(fd,"%d",&loop);
fclose(fd);



printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));

if(!fork()){
//*****************************************************************
//this section will send a list of files to the client
system("ls > list");
fp=fopen("list","r");
if(fp==NULL)
  {
  printf("Can't find the list of files \n");
  return 0;
  }
else
  {

  while(( ! feof(fp)))
          {
               fgets(data,100,fp);

              if((send(new_fd,data,MAXDATASIZE,0))==-1)
                       {
                        perror("send");
                         exit(new_fd);
                         }

     }
    if ((send(new_fd,"#$#",3,0))==-1)
     {
      perror("send");
      exit(new_fd);
     }

  fclose(fp);
}

//*****************************************************************
//this section is going to check if the file is valid or not
// if it is valid it will send the file
//line by line

if((numbytes=recv(new_fd,buf,len,0))==-1)
{
perror("recv");
exit(1);
}
printf("Recived file is: %s \n",&buf);
strcpy(f_name,buf);
printf("The file is: %s \n",&f_name);

//************************************************************
fp=fopen(buf,"r");
if(fp==NULL)
  {
  printf("Can't find the file \n");
  if((send(new_fd,"invalid",20,0))==-1)
    {
      perror("send");
      exit(new_fd);
    }

  return 0;
  }
  else
 {
//inform the client that the file is active
 if((send(new_fd,"valid",20,0))==-1)
    {
      perror("send");
      exit(new_fd);
    }


  printf("the increment = %d\n ",loop);

   insert_name(f_name,loop);

   fd=fopen("lock","w");
   loop++;
   fprintf(fd,"%d",loop);
  fclose(fd);

  sprintf(cmd,"rcp %s %s:Desktop/main2/%s",f_name,inet_ntoa(their_addr.sin_addr),f_name);
  system(cmd);
  printf("file is transmitted completely\n");
  //i=0;

/*  while(i<loop)
  {
   printf("locked files are %s\n",store_file[i].fname);
   ++i;
  }*/

 }
}
while(waitpid(-1,NULL,WNOHANG)>0);

}

}

//a function to store files name
void insert_name(char name[10], int loop)
{

int i=0, check=0;

while(check <=loop)
{
 if(strcmp(store_file[check].fname,name)==1)
 printf("The file is locked by another process\n");
 else
 strcpy(store_file[loop].fname,name);
check++;
}

  }

0
victory323
Asked:
victory323
  • 2
1 Solution
 
owenli27Commented:
So, the requirement is that if the file exist then it will send a message that the file is locked. Otherwise. It will insert name to store the file within an array of structure called filename, after checking that the file does not exist in the array.

Why did you use if(strcmp() == 1), not 0 in below code?

while(check <=loop)
{
if(strcmp(store_file[check].fname,name)==1)
     printf("The file is locked by another process\n");
else
     strcpy(store_file[loop].fname,name);

check++;
}
0
 
loukas34Commented:
I think that the problem is "fork" function.Fork creates a new instance of the program!Every instance has a new empty list.You must use shared memory.
0
 
loukas34Commented:
Or you can use threads.
0
 
jmcgOwnerCommented:
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by loukas34.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now