本文介绍了如何美元的TCP / IP的文件传输过程中的网络p $ pvent数据丢失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 //using namespace std;

 //This function is to be used once we have confirmed that an text is    to be sent
 //It should read and output an text file

 long long int receive_text(long long int socket)

 {

     long long int buffersize = 0, recv_size = 0, size = 0, read_size, write_size;

    char textarray[8192000],errno;

    char verify[15] = "text Received";

    FILE *text;

    //Find the size of the text

    read(socket, &size, sizeof(long long int ));

    //Send our verification signal

    write(socket, &verify,sizeof(char));

    //read(socket, &ch, 1);

    //Make sure that the size is bigger than 0

        if(size <= 0 )

    {

            printf("Error has occurred. Size less than or equal to 0 \n");
            return -1;

    }

        text = fopen("/home/sosdt009/Desktop/receivedandroid.txt","w");


        if( text == NULL)

        {

                 printf("Error has occurred. text file could not be opened \n");

                 return -1;

                }

         //Loop we have not received the entire file yet

        while(recv_size < size)

        {

                    ioctl(socket, FIONREAD, &buffersize);

            //We check to see if there is data to be read from the socket

                 if(buffersize > 0 )

            {

                     if(read_size = read(socket, textarray, buffersize) < 0)

                {

                    printf("%s", strerror(errno));

                }

                 //Write the currently read data into our text file

                    write_size = fwrite(textarray,1,buffersize, text);

             /*if(write_size != buffersize)

                {

                     printf("write and buffer sizes are wrong \n");

                }*/


            //Increment the total number of bytes read

                recv_size += read_size;

            //Send our handshake verification info

                 write(socket, &verify, 15*sizeof(char));

        }
        if (read_size == write_size)

            {

                printf("%lld,%lld", read_size, write_size);
                printf("text successfully Received! \n");

                }

        else

            {
                //sleep(1000000);
                printf("%lld,%lld", read_size, write_size);
                printf(" Data Received successfully: \n");

            }


    fclose(text);
return 0;

}

}

得到long long int主(INT ARGC,CHAR *的argv [])

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

{

  long long int  socket_desc , new_socket, c, read_size, buffer = 0;
  struct sockaddr_in server , client;
  char *readin;

  //Create socket

  socket_desc = socket(AF_INET,SOCK_STREAM,0);
  if (socket_desc == -1)

  {

     printf("Could not create socket:");

  }

  //Prepare the sockaddr_in structure

  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons( 6777 );

  //Bind

 if( bind(socket_desc,(struct sockaddr *)&server ,sizeof(server)) < 0)

 {

   puts("bind failed");
   return 1;

 }

 puts("Bind completed");

 //Listen

 listen(socket_desc,8);

 //Accept and incoming connection

  puts("Waiting for incoming connections...");
  c = sizeof(struct sockaddr_in);

 if((new_socket = accept(socket_desc,(struct sockaddr *)&client,(socklen_t *)&c)) )

{

    puts("Connection accepted");


    }

       fflush(stdout);

if (new_socket<0)

{

    perror("Accept Failed");
    return 1;

}

    receive_text(new_socket);

    close(socket_desc);
    fflush(stdout);
    return 0;

}`

我使用的TCP / IP协议从发送Android的一个文本文件到C。当Android是发送8KB数据我只接受4KB的数据。如何解决这个问题?

I am sending a text file from Android to C using tcp/ip protocol. When android is sending 8kb data i am only receiving 4kb data. How to resolve this issue?

推荐答案

如果你要使用的ioctl 调用来检查是否有插座上的数据接收,我会强烈建议你想成功接收之前,使用选择/民调机制。因此,在你的 receive_text 函数,你应该修改code这样的:

If you're going to use ioctl calls to check whether there's data on the socket to be received, I will strongly suggest you to use select/poll mechanism before trying to receive it successfully. Therefore, in your receive_text function, you should modify your code like the following:

// ...
#include <poll.h>
// ...

int receive_text(int socket)
{
  // ...

  struct pollfd fds[1];
  int result;
  int timeout = -1;        // to wait until data received or call interrupted
  int flags = 0;           // no flag
  fds[0].fd = socket;
  fds[0].events = POLLIN;  // to check for incoming data

  // ...

  if((result = poll(fds, 1, timeout)) < 0)
      return -1;           // returns with error

  if(result & POLLIN)      // if data has come ?
  {
      ioctl(socket, FIONREAD, &buffersize);

      if(buffersize > 0)
          recv_size = recv(socket, textarray, buffersize, flags);

          // ...
  }

  // ...

  return 0;
}

结果;你应该查看的文件描述符(插座)的状态首先要知道,如果新的数据已经到来。然后,调用的ioctl 可查看多少数据可以立即读取和最后的阅读(接收)从插座这些字节优雅。

As a result; you should check status of the file descriptor (socket) first to be sure if new data has come. Then, call ioctl to see how much data is immediately available for reading and finally read (receive) those bytes from the socket gracefully.

要对查询机制,看看信息的

To have more information about polling mechanism, have a look: poll - input/output multiplexing

这篇关于如何美元的TCP / IP的文件传输过程中的网络p $ pvent数据丢失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 11:49