开发者

How you pass path from client to server?

开发者 https://www.devze.com 2023-01-26 11:17 出处:网络
Hello i amtrying to make a TCP client/server that i want these things. The client will give the filename or the path of filename of a file.

Hello i am trying to make a TCP client/server that i want these things. The client will give the filename or the path of filename of a file. The server will find that file and give these details: permissions,size,owner,group of owner,date modified/created,number of words,id and priority of user and send these to client of -1 if something goes wrong.The client will print that details.I have done a lot of this things but i have a huge problem so i cant continue,my problem is that server cant recognize path of file but i tried with naming the file and communication its OK.What i am doing wrong? Thank you in advance

    CLIENT
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
        #include <netdb.h> 

        void error(char *msg)
        {
            perror(msg);
            exit(0);
        }

        int main(int argc, char *argv[])
        {
            int sockfd, portno, n;
            struct sockaddr_in serv_addr;
            struct hostent *server;

            char buffer[1024];
            if (argc < 3) {
               fprintf(stderr,"usage %s hostname port\n", argv[0]);
               exit(0);
            }
            portno = atoi(argv[2]);
            sockfd = socket(AF_INET, SOCK_STREAM, 0);
            if (sockfd < 0) 
                error("ERROR opening socket");
            server = gethostbyname(argv[1]);
            if (server == NULL) {
                fprintf(stderr,"ERROR, no such host\n");
                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,&serv_addr,sizeof(serv_addr)) < 0) 
                error("ERROR connecting");
            printf("Please enter the filename or path of filename: ");
            bzero(buffer,1024);
            fgets(buffer,1024,stdin);
            n = write(sockfd,buffer,strlen(buffer));
            if (n < 0) 
                 error("ERROR writing to socket");
            bzero(buffer,1024);
            n = read(sockfd,buffer,1024);
            if (n < 0) 
                 error("ERROR reading from socket");
            printf("%s\n",buffer);
            return 0;



SERVER
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/resource.h>
#include <sys/time.h> 

void error(char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{    
     FILE *fp;
     int sockfd, newsockfd, portno, clilen,i;
     char buffer[1024],filename[1024];
     char * pPath;
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     int which = PRIO_PROCESS;
     id_t pid;
     int ret;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("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("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd开发者_如何学JAVA, 
                 (struct sockaddr *) &cli_addr, 
                 &clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");
     bzero(buffer,1024);
     n = read(newsockfd,buffer,1024);
     if (n < 0) error("ERROR reading from socket");
     printf("Here is the message: %s\n",buffer);

     system("ls -al 1.txt > ls.txt");
     system("wc -w 1.txt > wc.txt");



     /* pPath = getenv ("PATH");
     if (pPath!=NULL)
     printf ("The current path is: %s\n",pPath);

     system("touch path.txt");
     fp=fopen("path.txt","w");
      if (fp==NULL) exit(1);
 fprintf(fp,pPath);
     fclose(fp); */


     pid = getpid();
     ret = getpriority(which, pid);
     printf("priority %d user id %d ret %d",which,pid,ret);

system("paste ls.txt wc.txt user.txt > info.txt");

    fp=fopen("info.txt","r");

    if (fp==NULL) exit(1);

    for(int i=0;i<1000;i++){

            fscanf(fp,"%c",&buffer[i]);
    }
    fclose(fp);        

     n = write(newsockfd,buffer,1024);
     if (n < 0) error("ERROR writing to socket");
     return 0; 
}
    }`

Thank you for the fast reply.To be specific,professor didn't ask us to make this txt files i created but i created cause i couldn't find a solution and i couldn't write everything to buffer.It should be like this:

buffer i coming with path from client find path-file from buffer(this is where i have problem) ls -al write to buffer(i wrote it to .txt) wc write to buffer(i wrote it to .txt) path write to buffer(haven't done this yet) user info write to buffer(haven't done this yet) buffer send to client client prints buffer with all these info

I tried sprintf but i didn't understand exactly how you use it,but this commands looks better than mine thanks:).No is passed through buffer if i understand well.


You're probably going to need to pass the path into your system calls. Allocate a buffer (keep in mind that you should be doing checks on your buffers for security, but I understand this is probably homework) and use sprintf (http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/):

char lsbuf[1024];
sprintf(lsbuf,"ls -al %s > ls.txt",buffer);
system(lsbuf);

The proper way to do things (and the way your instructor most likely intends you to do them) would be to do the work of ls, wc, etc within the code. Calling system like this leaves you open to a whole new class of security holes.


Does the path passed to the server contain a trailing '\n'? If so you should remove it (for example by placing \0 character)


In your current code, nothing is really done with the input by the clients, and the behaviour does not match your description of it.
What is this supposed to do?:

  for(int i=0;i<1000;i++){
      fscanf(fp,"%c",&buffer[i]);
  }
  fclose(fp);    

This will look for each character in the input (seeing as you use 1000 here, and the buffer can be 1024 characters, you are missing the last 24 characters) and return how many times it occurs in "info.txt". Note that you will also do a search for the NUL character ('\0'). Besides these carelessnesses it doesn't really do anything.

I assume you just want to find the string in the file, and work with the line number? You need a different method of searching strings. There are some clever and less clever algorithms for that. One of the more 'brute' algorithms is:

  1. Search for the first character
  2. Keep some flag as 'true' as long as all next characters match the expected character
  3. If some character violates the match, go to the next search of the first character

Note that there are more efficient algorithms than this.

I did not scrutinize your network setup, assuming it works. It would be a good idea to either split the code up into several paragraphs using some more whitespace, or put the network setup into a different function.


First, use of bzero should be avoided since that function is deprecated. Use memset instead.

The function fgets, reads until a newline or the End-of-File is reached. Since a newline is a valid character, it is added to the string and a null character is appended.

If you type in "mytext" and then enter, the buffer will have "mytext\n\0".

Based on the current code, you are not even using the buffer from the client so it obvious it isn't working (not that it will due to the fgets() behavior due to an appended newline).


Sorry for replying like this but i didn't have an account,i am new to this(i didn't have problem with my projects for 4 years:P).I insert

char lsbuf[1024];
sprintf(lsbuf,"ls -al %s > ls.txt",buffer);
system(lsbuf);

and is working like a charm almost...It accepts the path and do ls but doesn't save the ls to ls.txt(creates empty file) and doesn't send it to buffer.Also i tried replaced memset and is working!!

0

精彩评论

暂无评论...
验证码 换一张
取 消