hi

i have this small problem in my code which is that when i run the code it enters to the wrong condition

i mean in the if clause where i enter "download" it executes the body of the upload even though the first if clause condition meet and the opposite happens when i enter upload it executes the download if clause body !

so i am stuck ..i tried to put the last charachter to be NULL so it compares string correctly ...still it didnt work .

any help or suggestions ?

thank you

my code

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

int main()
{
int sock;
struct sockaddr_in server_addr;
struct hostent *host;
char send_data[1024];
int bytes_read;
char *name_files[]={"input.txt", "input1.txt"};//list of th file might be requseted by client
char* files_list_out[]={"out.txt","out2.txt"};// array for output files
int addr_len;
FILE * fout ;
char R_buff[1024];
FILE *fin ;
char recv_data[1024];//recieved data
char f_notf[6];
char line[128]; // line length
int count1=0;
int f=0;    
int M=0;
int numbytes ;



host= (struct hostent *) gethostbyname((char *)"127.0.0.1");

//creating socket
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(1);
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);

//chk operation 
printf ("would you like to download or upload ? , plz enter your request \n" );

scanf(" %[^\t\n]s" ,R_buff) ;
 numbytes= sendto(sock, R_buff,  sizeof(R_buff), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr));//sending the chosen file name 
R_buff[numbytes] = '\0';


 //**************start download ****************************

if (strcmp(R_buff,"download" )){
sendto(sock, name_files[0], strlen(name_files[0]), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr));//sending the chosen file name 

  recvfrom(sock,f_notf,1024,0,(struct sockaddr *)&server_addr, &addr_len) ; // recieveing file found or not 
 // printf("%s \n" ,f_notf) ;
if (f_notf =="nfound" ){
printf ("%s " ,f_notf);
}
else{
//writing on file



  recvfrom(sock,recv_data,1024,0,(struct sockaddr *)&server_addr, &addr_len); // recieve file


     fout = fopen("out.txt","w");      

       fprintf(fout,"%s\n",recv_data); 
      // printf ("%s\n " ,recv_data);
       printf("%s " ,f_notf) ;

     fclose(fout); 

}
printf ("\n d" );
}

//**************************start upload****************************
else if (strcmp(R_buff ,"upload" )){
fin=fopen("input.txt","r");//reading file
   if(fin!=NULL){


 while(1){

         int ch = fgetc(fin);// read line char by char 
         if ( ch == EOF )
         {          
            break;   }
         if ( isalpha(ch) )//check if char return true
         {
            R_buff[count1]=ch;
            count1++;        
            M=1;
            }
        else if ( isspace(ch) && M )//skip space
         { R_buff[count1]=' ';count1++;
    }

}

R_buff[count1]='\0';//make last char NULL
}

/* -------------------end of file reading------------------*/
sendto(sock, name_files[0], strlen(name_files[0]), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr));//sending the chosen file name to upload

sendto(sock, R_buff,  sizeof(R_buff), 0,(struct sockaddr *)&server_addr, sizeof(struct sockaddr));//sending the chosen file name 
printf ("\nu" );

}


// ************************end upload*******************************
     return 0; 



}

strcmp() doesn't return a boolean, it returns a relation:

  • <0 - The first string is lexicographically "smaller".
  • 0 - The two strings are equivalent.
  • >0 The first string is lexicographically "larger".

So when you want to test for equality, it should look like this:

if (strcmp(a, b) == 0) {
    /* Equal */
}
else {
    /* Not equal */
}

ok so if use a==b instead of strcmp() this could solve the problem

i will try it

thank you

ok so if use a==b instead of strcmp() this could solve the problem

Um, no. If you use a == b then you'll be comparing the address of two arrays, not the content of two strings. You do want to use strcmp(), but you want to use it correctly (as in my example), and with full understanding of how it works.

problem sovled it looks that my foucs was zero ,,,thanks to the cool errors i had all day

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.