?
Solved

Please need some help

Posted on 2003-03-29
5
Medium Priority
?
242 Views
Last Modified: 2010-04-15
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
Comment
Question by:victory323
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 9

Expert Comment

by:owenli27
ID: 8240551
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
 
LVL 1

Accepted Solution

by:
loukas34 earned 160 total points
ID: 8252929
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
 
LVL 1

Expert Comment

by:loukas34
ID: 8252960
Or you can use threads.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10199311
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question