简体   繁体   English

uint32在C中的一维数组

[英]1D arrays of uint32 in C

I'm continuously sending arrays of pixel values (160x120) as a bytestream from LabVIEW to a C-program through TCP/IP. 我不断通过TCP / IP将像素值(160x120)的数组作为字节流从LabVIEW发送到C程序。 Then I'm converting the bytes to uint32 values and printing the received data in the console application. 然后,我将字节转换为uint32值,并在控制台应用程序中打印接收到的数据。 The goal here, is that I want to divide the received data in 1D arrays, so every 1D array will consists of (160x120=19200) integer values, so it represents a frame. 这里的目标是我想将接收到的数据划分为一维数组,因此每个一维数组将由(160x120 = 19200)整数值组成,因此它代表一个帧。 In better words, the array has to collect every 19200 integers of the received data and display them in the console application. 用更好的话说,该数组必须收集每19200个接收到的数据的整数,并将其显示在控制台应用程序中。 Then the same array has to collect the next 19200 integers and display them in the console application and so on. 然后,同一数组必须收集下一个19200个整数并将其显示在控制台应用程序中,依此类推。 I appreciate if anyone shows how to do it. 如果有人演示如何操作,我将不胜感激。

WSADATA wsa;
SOCKET s , new_socket;
struct sockaddr_in server , client;
int c;
int iResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
typedef unsigned int uint32_t;
unsigned int i;
size_t len;
uint32_t* p;
uint32_t value;

p = (uint32_t*)((void*)recvbuf);

do
{
    iResult = recv(new_socket, recvbuf, recvbuflen, 0);
    len = iResult/sizeof(uint32_t);

    for(i=0; i < len; i++)
    {
    value = p[i];
    }
    printf("%d\n", value);

} 
while ( iResult > 0 );

closesocket(new_socket);
WSACleanup();

return 0;

This code is making lots of assumptions about the sizes and alignment of things. 这段代码对事物的大小和对齐方式做出了许多假设。 On x86 systems this will work fine, but beware systems that require alignment. 在x86系统上,这可以正常工作,但请注意需要对齐的系统。 It is also assuming that both systems have the same endien-ness. 还假设两个系统都具有相同的优先级。

To receive data chunked into 160x120 uint32_t byte chunks (assuming your DEFAULT_BUFLEN is correct): 要接收分成160x120个uint32_t字节块的数据(假设您的DEFAULT_BUFLEN是正确的):

int receivedCount = 0;
do
{
    iResult = recv( new_socket, 
                    &recvbuf[receivedCount],
                    DEFAULT_BUFLEN-receivedCount,
                    0 );
    if ( iResult == 0 ) break;    //0 == socket closed
    receivedCount += iResult;
    if ( receivedCount < DEFAULT_BUFLEN )
        continue;                 //still receiving full frame
    for(int i=0; i<160*120; i++)
        printf("%d\n",(unsigned int)p[i]);
    receivedCount = 0;
}
while( wantToContinueCapturingFrames );

This will continuously wait for data until a full frame has been received. 这将持续等待数据,直到收到完整的帧为止。 Once a full frame has been received it will print out the data from the frame. 收到完整的帧后,它将打印出该帧中的数据。 If the remote system closes the connection this will exit without printing any partial frame. 如果远程系统关闭连接,它将退出而不打印任何局部框架。

This isn't really sufficient for "real" applications since it is blocking in recv waiting for data. 对于“实际”应用程序,这实际上还不够,因为它阻止了recv等待数据。 There's no provision to (gracefully) abort without the remote system either sending data or closing the connection, but fixing that isn't really what you are asking about here. 没有提供(优雅地)中止而无需远程系统发送数据或关闭连接的规定,但是解决这实际上并不是您要问的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM