Home Forums C Programming Concept utilising file transfer from client to server

Viewing 1 reply thread
  • Author
    Posts
    • #1993
      parita
      Participant

      hey,
      i have designed a code linking the server and the client. my purpose is to transfer a file from server as desired by the client. the code for client side is as under:-


      #include
      #include
      #include
      #include
      #include
      #include
      #include
      #include
      #include
      int main(int argc, char *argv[])
      {
          int sockfd, portno,n;
          struct sockaddr_in serv_addr;
          struct hostent *server;
          char buffer[256];
          if (argc < 3) {
             fprintf(stderr,"usage %s hostname portn", argv[0]);
             exit(0);
          }
          portno = atoi(argv[2]);
          sockfd = socket(AF_INET, SOCK_STREAM, 0);
          if (sockfd < 0)
              ("ERROR opening socket");
          server = gethostbyname(argv[1]);
          if (server == NULL) {
              fprintf(stderr,"ERROR, no such hostn");
              exit(0);
          }
          bzero((char *) &serv_addr, sizeof(serv_addr));
          serv_addr.sin_family = AF_INET;
          bcopy((char *)server->h_addr,
               (char *)&serv_addr.sin_addr.s_addr,
               server->h_length);
          serv_addr.sin_port = htons(portno);
          if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
              ("ERROR connecting");
      n = recv(sockfd,buffer,255,0);
      printf("the list of files:%sn",buffer);
          printf("Please enter the file no:");
          bzero(buffer,256);
          fgets(buffer,255,stdin);
          n = send(sockfd,buffer,strlen(buffer),0);
          if (n < 0)
               ("ERROR writing to socket");
          bzero(buffer,256);
          n = recv(sockfd,buffer,255,0);
          if (n < 0)
               ("ERROR reading from socket");
          printf("%sn",buffer);
      fstream fout;
      fout.open("d.txt",ios::out);
      n=recv(sockfd,buffer,255,0);
      printf("content of buffer:%sn",buffer);
      if (n < 0)
       printf("ERROR reading from socket");
      char write(sockfd,buffer, sizeof(buffer));
      fout.close();
      return 0;
      }

      the code for server side is as under:-
       

      /* A simple server in the internet domain using TCP
      The port number is passed as an argument */
      #include
      #include
      #include
      #include
      #include
      #include
      #include
      #include
      int main(int argc, char *argv[])
      {
      const int N=80;
      char line [N];   
       int sockfd, newsockfd, portno, clilen;
      char *ch;
           struct sockaddr_in serv_addr, cli_addr;
      char buffer[256];
           int n;
           if (argc < 2) {
               fprintf(stderr,"ERROR, no port providedn");
               exit(1);
           }
           sockfd = socket(AF_INET, SOCK_STREAM, 0);
           if (sockfd < 0)
              ("ERROR opening socket");
           bzero((char *) &serv_addr, sizeof(serv_addr));
           portno = atoi(argv[1]);
           serv_addr.sin_family = AF_INET;
           serv_addr.sin_addr.s_addr = INADDR_ANY;
           serv_addr.sin_port = htons(portno);
           if (bind(sockfd, (struct sockaddr *) &serv_addr,
                    sizeof(serv_addr)) < 0)
                    ("ERROR on binding");
           listen(sockfd,5);
           clilen = sizeof(cli_addr);
           newsockfd = accept(sockfd,
                       (struct sockaddr *) &cli_addr,(socklen_t *)&clilen);
           if (newsockfd < 0)
                ("ERROR on accept");
      n=send(newsockfd,"here is the list of files:1 for ankur.txt,2 for a.txt,3 for b.txt",255,0);   
           bzero(buffer,256);
            n = recv(newsockfd,buffer,255,0);
           if (n < 0) ("ERROR reading from socket");
           printf("Here is the message: %cn",buffer[0]);
      //ch=buffer;
      switch (buffer[0])
      {
      case '1':  
       {  n=send(newsockfd,"file exists",18,0);
                     ifstream fin;
                   fin.open("ankur.txt");
              while(fin)
                     {
                      fin.getline(line,N);
                   printf("Read line is :%sn",line);
                     n=send(newsockfd,line,sizeof(line),0);
        }fin.close();
            if (n < 0)
        {
        printf("ERROR writing to socket");
             return 0;
        }
         }
       break;

      case '2':    {  n=send(newsockfd,"file exists",18,0);
                    
                     ifstream fin;
                   fin.open("a.txt");
              while(fin)
                     {
                      fin.getline(line,N);
                   printf("Read line is :%sn",line);
                     n=send(newsockfd,line,sizeof(line),0);
        
        }fin.close();
           if (n < 0) ("ERROR writing to socket");
           return 0;
        }break;
      case '3':{  n=send(newsockfd,"file exists",18,0);
                
                     ifstream fin;
                   fin.open("b.txt");
              while(fin)
                     {
                      fin.getline(line,N);
                   printf("Read line is :%sn",line);
                     n=send(newsockfd,line,sizeof(line),0);
                   
        }fin.close();
           if (n < 0) ("ERROR writing to socket");
           return 0;
        }break;
      default:
      { n = send(newsockfd,"file doesnot exists",18,0);
           }break;
       
      }
      }

      But, i am not able to accomplish the task.
      Please help me with ur assistance.

    • #3247
      MonicaLoche
      Participant

      I guess there is ERROR writing to socket because you might need some socket initialization.

      __________________
      Writing jobs

Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.