使用套接字中的read()访问缓冲区

时间:2011-07-28 05:02:30

标签: c sockets

在我的套接字代码中,我有以下结构:

服务器:

         #include <stdio.h>
         #include <stdlib.h>
         #include <string.h>
         #include <sys/types.h>
         #include <sys/socket.h>
         #include <netinet/in.h>
         #define MAXPROFILES  2

        int main(int argc, char *argv[])
     {
        int sockfd, newsockfd, portno, clilen;
        struct sockaddr_in serv_addr, cli_addr;
        unsigned char buf[1024];
        char* pointer = buf;

       struct profile_t
     {
       unsigned char length;
       unsigned char type;
       unsigned char *data;
     };

       typedef struct profile_datagram_t
     {
       unsigned char src[4];
       unsigned char dst[4];
       unsigned char ver;
       unsigned char n;
       struct profile_t profiles[MAXPROFILES];
     } header;


       header outObj;

       int j =0;
       int i =0;


       if (argc < 2) {
       fprintf(stderr,"usage: %s port_number1",argv[0]);
       exit(1);
     }
       sockfd = socket(AF_INET, SOCK_STREAM, 0);
       if (sockfd < 0)
       error("ERROR DETECTED !!! Problem in 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 = htonl(INADDR_ANY);
       serv_addr.sin_port = htons(portno);

        if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
          error("ERROR DETECTED !!! There was a problem in binding");

        listen(sockfd, 10);
        clilen = sizeof(cli_addr);



        printf("Server listening on port number %d...\n", serv_addr.sin_port);

        newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);

        if (newsockfd < 0)
        error("ERROR DETECTED !!! the connection request was not accepted");

        int rc = read(newsockfd,&pointer,100);
        if(rc < 0){
        printf("error");
      }
        else {
        printf("success %d",rc);
      }
        for(int i=0;i<29;i++){
        printf(pointer);
        printf(buf);
      }





 return 0;

}

客户端:

            int main(int argc, char *argv[])

         {
            int sockfd, portno, n;
            struct sockaddr_in serv_addr;
            struct hostent *server;
            unsigned char buf[1024];
            unsigned int srcAddress = 2193598184;
            unsigned int destAddress = 2193598182;

           struct profile_t
       {
            unsigned char length;
            unsigned char type;
            unsigned char *data;
       };

            typedef struct profile_datagram_t
       {
            unsigned char src[4];
            unsigned char dst[4];
            unsigned char ver;
            unsigned char n;
            struct profile_t profiles[MAXPROFILES];
        } header;


           header outObj;

           int j =0;
           int i =0;
      // for loop for doing the malloc so that we can allocate memory to all profiles
           for(i=0;i<MAXPROFILES;i++){
           outObj.profiles[i].data = malloc(5);
        }


          for(i=0;i<4;i++){
          outObj.src[i] = (srcAddress >> (i*8)) & 0xFF;
          outObj.dst[i] = (destAddress >> (i*8)) & 0xFF;
        }
          outObj.ver = 1;
          outObj.n = 2;

          memcpy(buf,&outObj.src,4);
          memcpy(buf+4,&outObj.dst,4);
          memcpy(buf+8,&outObj.ver,1);
          memcpy(buf+9,&outObj.n,2);


          outObj.profiles[0].length = 5;
          outObj.profiles[0].type = 1;
          outObj.profiles[1].length = 5;
          outObj.profiles[1].type = 2;

          for(i=0;i<MAXPROFILES;i++){
          for(j=0;j<5;j++){
          outObj.profiles[i].data[j] = j+1;
        }
      }



            int k = 11;


           for(i=0;i<MAXPROFILES;i++){
            memcpy(buf+k,&outObj.profiles[0].length,1);
            memcpy(buf+k+1,&outObj.profiles[0].type,1);
            memcpy(buf+k+2,outObj.profiles[0].data,5);
            k +=7;
       }


           if (argc < 3) {
           fprintf(stderr,"usage: %s hostname port\n", argv[0]);
           exit(0);
       }
           portno = atoi(argv[2]); //Convert ASCII to integer
           sockfd = socket(AF_INET, SOCK_STREAM, 0); // socket file descriptor


           if (sockfd < 0)
           error("ERROR DETECTED !!! Problem in opening socket\n");

           server = gethostbyname(argv[1]);
           if (server == NULL) {
           fprintf(stderr,"ERROR DETECTED !!!, no such server found \n");
           exit(0);
       }

           bzero((char *) &serv_addr, sizeof(serv_addr)); //clear the memory for server  address

           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);

           printf("Client 1 trying to connect with server host %s on port %d\n", argv[1], portno); 


             if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
             error("ERROR in connection");

             printf("SUCCESS !!! Connection established \n");


             if (write(sockfd, buf, k) < 0)
          {
             error("Write error has occured ");
          }


            return 0;
           }

现在从客户端我对我的unsigned char buf进行memcpy并发送数据。在recv方面,如何访问我的“buf”中的值。我尝试了“*”运算符,但是我收到了警告

arning:传递âprintfâ的参数1使得整数中的指针没有强制转换

我想基本上访问缓冲区内的值,因为我知道我应该能够重新填充我的数据包的大小。我知道这是一个“C”问题而不是“套接字相关”。任何帮助都会非常有用。

2 个答案:

答案 0 :(得分:1)

在服务器端,您只需要反转在客户端执行的操作:

你也没有使用char *pointer = buf获得任何东西,只是直接读入缓冲区。当您循环搜索MAXPROFILES时,只需检查buffer中与返回的size的当前位置,确保您没有读取未收到的数据。

例如:

   int size = read(newsockfd, buffer, sizeof(buffer));

   int src, dst;
   char ver;
   short n;

   memcpy(&src, buf + 0, 4);
   memcpy(&dst, buf + 4, 4);
   memcpy(&ver, buf + 8, 1);
   memcpy(&n,   buf + 9, 2);

......等等......

您在客户端编码结构/数据,因此您必须在服务器端解码它们。一旦你这样做了,那么你可以打印它们或做你喜欢的任何事情:

  printf("src: %d  dst: %d  ver: %c  n: %d\n", src, dst, ver, n);

另外,为简化编码/解码,请使用相同的方法(例如memcpy而不是使用掩码方法逐字节)。例如,在客户端:

  k = 0;
  memcpy(buf + k, &src, sizeof(src));  k += sizeof(src);
  memcpy(buf + k, &dst, sizeof(dst));  k += sizeof(dst);
  ...

这也让我从上面的硬编码数字中解脱了一些。只需确保您的srcdst以及其他所有客户端和服务器上的类型都相同。

答案 1 :(得分:0)

你的意思是gcc在这一行上发出警告:“int rc = read(newsockfd,&amp; pointer,100);”

你的变量“指针”本身是指向buf的指针,所以你不需要使用&amp;指针,只需使用如下:

int rc = read(newsockfd,pointer,100)