通过套接字发送大型数组

时间:2015-05-23 12:10:41

标签: c sockets

我正在尝试通过C中的TCP套接字将一个100.000.000字节(可能更多)的char数组从服务器发送到客户端。

我这样做:

char *array;  // global array malloc'd (SIZE)

//#######################
//    server code
//#######################
int i;
int SIZE = 100000000

for (i = 0; i < SIZE; i = i + 4){  
    write(id, &array[i], 4);   // write 4 bytes every time
}

//#######################
//    client code
//#######################
int i;
int SIZE = 100.000.000
for (i = 0; i < SIZE; i = i + 4)
    read(id, array + i, 4);    // read 4 bytes

问题:

1)当我尝试发送更多字节时,传输出现问题。例如,如果我将4更改为100,则表示“断管”。为什么会这样?

2)我知道这不是一种“安全”的读/写方式,因为我没有检查read()和write()返回值。我怎样才能做到这一点?

3)我是否必须使用htonl()和ntohl()函数?

2 个答案:

答案 0 :(得分:4)

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

 //in @param
 //@param fd the socket file descriptor
 //@param array an array of data source to write to send to the connected client 
 //@param SIZE the size of data source to send to the client
 //@param sz_emit the size of data to send in one loop step
 //out @param
 //total length of data emited to the client

 int write_to_client(int fd, char* array, int SIZE, int sz_emit)
 {
   //#######################
   //    server code
   //#######################
   int i=0, sz=0;
   for(i = 0; i < SIZE; i += sz_emit )
   {  
       while(sz_emit-sz)
       { 
         sz+=write(id, array+i+sz, sz_emit-sz);
       }
       sz = 0;
   }
   return i;
 }

//#######################
//    client code
//#######################
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>          
#include <sys/socket.h>

//in @param
//@param fd is the file descriptor of the socket to read from
//@param SIZE the size of datas you want to read from the socket
//@param sz_received the size of byte to read in one loop step
//@param length, the length of data received
//@param read_err if 0 no error if -1 an error occurs use errno from #include <errno.h> to know more about that error 
//out @param
//a pointer to an array of size SIZE containing the data readed
char* receive_from_server(int fd, int SIZE, int sz_received, int* length, int* read_err)
{
  *read_err = 0;
  int i = 0, sz = 0, rt = 0, count=0;
  char *array = (char *)malloc(SIZE);
  memset(array, 0, SIZE);  
  for (i = 0; i < SIZE; i += sz_received)
    {
      while(sz_received-sz)
      { 
        rt = read(id, array + i + sz, sz_received-sz);
        if(rt==-1)
        {
          *read_err=rt;
          printf("an error occurs\n");
          goto l;
        }
        if(!rt)goto l;
        sz+=rt;
        count += sz;   
      }
      sz = 0;
    }
  l: *length = count;
  return array;
}

用法:

//server side
int SIZE = 100000000;
char array_to_send[SIZE]={'r'};
int sz_data_emited = write_to_client(sock, array_to_send, SIZE, 4);
printf("how many byte data emited:%d\n", sz_data_emited);

//client side
int SIZE = 100000000, length = 0, read_err=0;
char*array_received = NULL;
array_received = receive_from_server(sock, SIZE, 4, &length, &read_err);
if(!read_err)printf("get some datas\n");
// free array_received when finished...free(array_received)

一些注意事项:

当你想要转移一个多字节实体时,你需要注意字节顺序,例如short,int,long,utf-16等,但如果你的数据是utf-8或ascii文本,你就不要这样做需要它。

答案 1 :(得分:0)

我认为 Narcisse Doudieu Siewe 答案有一些错误。我想it fails when SIZE isn't multiplicity of sz_emit。 例如,我们在8个字节块中发送的20个字节比最后一个数据块(或数据包)长4个字节。比如果我们尝试发送最后8个字节的块并且只剩下4个字节,而循环将是无限的(8 - 4),并且它永远不会达到sz = 8,因为下一个发送将仅增加0。我写这样的修改(没有经过测试,我会很快测试,并且考虑到这个边界条件也会编写第二种方法)。

/**
 * @param sock_fd - the file descriptor of the socket to write (send) data to
 * @param packetLength - the size of data to send in one packet
 * @param data - binary data to send (unsigned char array)
 * @param dataLength - the size of all binary data to send
 * @return  - status code SUCCESS or FAILURE
 */
result_t send_binary(const sock_fd_t sock_fd, const size_t packetLength, const unsigned char *data, const size_t dataLength) {

    ssize_t leftPacketLength = 0;
    ssize_t offset = 0;
    ssize_t sentPacketLength = 0;

    // send each packet of data in the loop
    for(int leftDataLength=dataLength; leftDataLength>0; leftDataLength -= packetLength) {

        leftPacketLength = (leftDataLength > packetLength) ? packetLength : leftDataLength;
        while(leftPacketLength > 0) {
            sentPacketLength = send(sock_fd, data + offset, leftPacketLength, 0);
            if(sentPacketLength < 0) {
                fprintf(stderr, "%s: Error while sending data to the socket.\n", __func__);
                perror(errno);
                return FAILURE;
            }
            offset += sentPacketLength;
            leftPacketLength -= sentPacketLength;
        }
    }

    if(offset != dataLength)
        return FAILURE;

    return SUCCESS;
}

/**
 * @param sock_fd - the file descriptor of the socket to read (recieve) data from
 * @param packetLength - the size of data to recieve in one packet
 * @param data - binary data received (unsigned char array) - previously allocated
 * @param dataLength - the size of all binary data received - previously defined
 * @return - status code SUCCESS or FAILURE
 */
result_t recv_binary(const sock_fd_t sock_fd, const size_t packetLength, unsigned char *data, const size_t dataLength) {

    ssize_t leftPacketLength = 0;
    ssize_t offset = 0;
    ssize_t recvedPacketLength = 0;


    for(int leftDataLength=dataLength; leftDataLength > 0; leftDataLength -= packetLength) {

        leftPacketLength = (leftDataLength > packetLength) ? packetLength : leftDataLength;
        while(leftPacketLength > 0) {
            recvedPacketLength = recv(sock_fd, data + offset, leftPacketLength, 0);
            if(recvedPacketLength < 0) {
                fprintf(stderr, "%s: Error while receiving data from the socket.\n", __func__);
                perror(errno);
                return FAILURE;
            }
            offset += recvedPacketLength;
            leftPacketLength -= recvedPacketLength;
        }
    }

    if(offset != dataLength)
        return FAILURE;

    return SUCCESS;
}

在传输实际二进制数据之前,还需要通过套接字发送二进制数据发送/接收的大小。需要知道我们需要读取多少字节。