计算缓冲区TCP中的数据

时间:2017-05-12 18:15:12

标签: c

在C中,如何计算char缓冲服务器中收到的数据? 例如,客户端发送单个消息“1 2 3”,服务器必须计算这些数据并发送到客户端“3”,这是读取数据的数字。

这是客户:

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


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

    int simpleSocket = 0;
    int simplePort = 0;
    int returnStatus= 0;
    char buffsend[256];
    char buffrecv[256];
    char buff[256];
    int i, n, length;
    struct sockaddr_in simpleServer;

    if (3 != argc) {

        fprintf(stderr, "Usage: %s <server> <port>\n", argv[0]);
        exit(1);

    }

    /* create a streaming socket      */
    simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (simpleSocket == -1) {

        fprintf(stderr, "Could not create a socket!\n");
        exit(1);

    }
    else {
        fprintf(stderr, "Socket created!\n");
    }

    /* retrieve the port number for connecting */
    simplePort = atoi(argv[2]);

    /* setup the address structure */
    /* use the IP address sent as an argument for the server address  */
    //bzero(&simpleServer, sizeof(simpleServer)); 
    memset(&simpleServer, '\0', sizeof(simpleServer));
    simpleServer.sin_family = AF_INET;
    //inet_addr(argv[2], &simpleServer.sin_addr.s_addr);
    simpleServer.sin_addr.s_addr=inet_addr(argv[1]);
    simpleServer.sin_port = htons(simplePort);

    /*  connect to the address and port with our socket  */
    returnStatus = connect(simpleSocket, (struct sockaddr *)&simpleServer, sizeof(simpleServer));

    if (returnStatus == 0) {
        fprintf(stderr, "Connect successful!\n");
    }
    else {
        fprintf(stderr, "Could not connect to address!\n");
    close(simpleSocket);
    exit(1);
    }





    /* get the message from the server   */


    do {

        printf("insert num of data or terminate ('0'): ");
        fgets(buffsend,256,stdin);
        n=atoi(buffsend);
        if(n>6) {
          printf("Error\n");

         }
        else {
                length=strlen(buffsend);
                for(i=0;i<n;i++) {
                   printf("insert number: ");
                   length=strlen(buffsend);
                   buffsend[length-1]=' ';
                   fgets(buffsend+length,256-length,stdin); }

                   printf("\n");
                 write(simpleSocket, buffsend, strlen(buffsend));
             }

      } while((n!=0) && (n>0));



    close(simpleSocket);
    return 0;

}

这是服务器:

#include <stdio.h>      
#include <sys/types.h>
#include <sys/socket.h>   
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> 



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

    int simpleSocket = 0;
    int simplePort = 0;
    int returnStatus = 0;
    char buff[256];
    char message[256];
    int n, i, length, conta=0;
    int media=0;
    int varianza=0;
    struct sockaddr_in simpleServer;

    if (2 != argc) {

        fprintf(stderr, "Usage: %s <port>\n", argv[0]);
        exit(1);

    }

    simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (simpleSocket == -1) {

        fprintf(stderr, "Could not create a socket!\n");
        exit(1);

    }
    else {
        fprintf(stderr, "Socket created!\n");
    }

    /* retrieve the port number for listening */
    simplePort = atoi(argv[1]);

    /* setup the address structure */
    /* use INADDR_ANY to bind to all local addresses  */
    memset(&simpleServer, '\0', sizeof(simpleServer)); 
    simpleServer.sin_family = AF_INET;
    simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
    simpleServer.sin_port = htons(simplePort);

    /*  bind to the address and port with our socket  */
    returnStatus = bind(simpleSocket,(struct sockaddr *)&simpleServer,sizeof(simpleServer));

    if (returnStatus == 0) {
        fprintf(stderr, "Bind completed!\n");
    }
    else {
        fprintf(stderr, "Could not bind to address!\n");
    close(simpleSocket);
    exit(1);
    }

    /* lets listen on the socket for connections      */
    returnStatus = listen(simpleSocket, 5);

    if (returnStatus == -1) {
        fprintf(stderr, "Cannot listen on socket!\n");
    close(simpleSocket);
        exit(1);
    }

    while (1)

    {

        struct sockaddr_in clientName = { 0 };
    int simpleChildSocket = 0;
    int clientNameLength = sizeof(clientName);

    /* wait here */

        simpleChildSocket = accept(simpleSocket,(struct sockaddr *)&clientName, &clientNameLength);

    if (simpleChildSocket == -1) {

            fprintf(stderr, "Cannot accept connections!\n");
        close(simpleSocket);
        exit(1);

    }

        /* handle the new connection request  */
    /* write out our message to the client */

        while(read(simpleChildSocket, buff, 256)) {
             printf("Data received: %s\n", buff);
              bzero(buff, 256);
           }


       close(simpleChildSocket);
    }

    close(simpleSocket);
    return 0;

}

在服务器部分,我想计算收到的数据。

2 个答案:

答案 0 :(得分:0)

    while(read(simpleChildSocket, buff, 256)) {

read函数返回从流中读取的字节数。你应该将它存储在某个地方,以便以后可以访问它。

通常,您有两种方法可以判断缓冲区中有多少字节:

1)只需将字节数存储在一个单独的整数中。

2)如果您的数据是文本字符串,请使用零字节终止文本字符串,然后在需要知道其长度时调用strlen

答案 1 :(得分:-1)

我不知道我是否得到了它!

如果我明白了,你是否发送数据大小和空格字符数据?

示例:“3 123” 3 =长度,123 =数据

     //server side
     int received;
     /*while(read(simpleChildSocket, buff, 256))*/ 
     /*Make sure received>0 as well warned David 
       Schwartz*/
     do{
         received = read(simpleChildSocket, buff, 256);
         if ( received > 0 ) 
             printf("Data received: %s\n", buff);
         else if ( received == 0 )
         {
             printf("Connection closed\n");
             break;
         }
         else
         {
             printf("recv failed -1\n");
             break;
         }
         /*int receved=strlen(buff);*/
         char bufftmp[256]={0};
         /*Loop through the buffer until you find space*/
         for(int k=0;k<received;k++)
         { 
             bufftmp[k]=buff[k];
             if(buff[k]==' ')/*is equal space?*/
             {
                 bufftmp[k]='\0';//make space null
                 int numpart=atoi(bufftmp);
                 printf("lenght of message is: %d\n",numpart);/*get only length*/
                 break;
             } 
         } 
         bzero(buff, 256);

     }while(received>0)