网络数据传输过程中接收到的缓冲区的访问冲突错误 [英] access violation error of a received buffer during network transfer of data

查看:134
本文介绍了网络数据传输过程中接收到的缓冲区的访问冲突错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建2个应用程序(服务器和客户端),其中,在接受客户端连接后,服务器立即通过缓冲区发送数据库中存在的信息.虽然客户端收到了缓冲区,但是当我尝试访问缓冲区时,会显示访问冲突错误.

我在这里粘贴代码的发送和接收逻辑:

发送逻辑:

I am building 2 applications(a server & a client) where in, immediately after a client connection is accepted, the server send information present in the database through a buffer. Although the client receives the buffer, but when I am trying to access the buffer, an access violation error is shown.

I am pasting the sending and receiving logic of my code here:

sending logic:

if(LOWORD(lParam)==FD_ACCEPT)
    {
        BOOL b = m_ServerSocket.Accept(*m_pClientSocket);
        if(b==FALSE)
        {
            AfxMessageBox("Unable to accept client's connection");
        }
        else
        {
            AfxMessageBox("Client Connected");
            //send the database info immediately after accepting the clients connection.....
            m_database.Open("AVIFilesDSN");   //database object connecting to DSN
            CAVIFileRecordset* pAviFileRecordset; //pointer to the derived class of CRecordset.
            pAviFileRecordset = new CAVIFileRecordset(&m_database);
            pAviFileRecordset->Open();
            pstrArray = new CStringArray;  //pointer to CStringArray
            while(!pAviFileRecordset->IsEOF())
            {
                //m_FilePath variable(CString) pointing to a field in database table(containing filepaths),
                pstrArray->Add(pAviFileRecordset->m_FilePath);
                pAviFileRecordset->MoveNext();
            }            
            pAviFileRecordset->Close();
            delete pAviFileRecordset;
            m_pClientSocket->Send((void*)pstrArray,MAX_BUFFER_SIZE);
            //delete pstrArray;
        }




接收逻辑:




receiving logic:

int done;
char* pBuffer[MAX_BUFFER_SIZE];  //MAX_BUFFER_SIZE = 4096
pBuffer[MAX_BUFFER_SIZE] = new char;
CStringArray* pStrArray;
pStrArray = new CStringArray;
if(LOWORD(lParam)==FD_READ)
{
done = m_CSoc.Receive((void*)pBuffer,MAX_BUFFER_SIZE);
if(done==SOCKET_ERROR)
{
    AfxMessageBox("recv failed");
    return;
}
//receiving the database info
pStrArray = (CStringArray*)pBuffer;
nTemp1 = pStrArray->GetUpperBound();
nTemp = 0;
while(nTemp<=nTemp1)
{
         //displaying the contents of the string array on a list control
         m_FileListCtrl.InsertItem(nTemp,pStrArray->GetAt(nTemp)); //error occurring here
     nTemp++;
}
AfxMessageBox("Updated Files List");

}



我可以仅将CStringArray指针作为Receive函数中的第一个参数传递.使用CStringArray *转换接收到的缓冲区后,我可以看到数组上的元素数量和数据库中的项目数量相同.但是,当我尝试在列表控件上显示接收到的信息时,我收到了访问冲突错误.



Can I just pass the CStringArray pointer as the 1st argument in the Receive function. After casting the received buffer with CStringArray* I am able to see that the number of elements on the array and the number of items in the database are same. But when I am trying to show the received information on a list control, I am receiving the access violation error.

推荐答案

您不能使用动态分配的对象发送对象通过网络连接的内存.您必须通过访问缓冲区来发送数据并获取它们的实际大小,使用本地缓冲区接收它们,并将接收到的数据复制到新对象.

在您的情况下,您可以在发送方上分配一个缓冲区,将所有带有NULL终止符的字符串复制到此缓冲区,然后发送.在接收端,提取以NULL结尾的字符串,并将每个字符串附加到CString对象.但是,这需要一些其他代码来处理可变长度或使用缓冲区,该缓冲区可以容纳所有可能的字符串(最大字符串数乘以最大字符串长度再加上一个).
You can''t send objects using dynamically allocated memory over a network connection. You must send the data by accessing the buffers and get their actual size, receive them using a local buffer, and copy the received data to a new object.

In your case, you may allocate a buffer on the sender, copy all strings with NULL terminators to this buffer, and send it. On the receive side, extract the NULL terminated strings and append each to a CString object. However, this requires some additional code to handle the variable length or usage of a buffer that can hold all possible strings (max. number of strings multiplied by max. string length plus one).


在接收器端,该行

On the receiver side, the line

pBuffer[MAX_BUFFER_SIZE] = new char;



错误有两个原因:(a)它在无效位置访问数组(可用的最高索引为MAX_BUFFER_SIZE -1),(b)"new char"分配一个"char"对象,该对象创建一个内存泄漏,完全没有必要.这不是C#:-)

如果要使用NULL字符初始化缓冲区的最后一个单元格,请使用



is wrong for two reasons: (a) It accesses the array at an invalid position (the highest index available is MAX_BUFFER_SIZE -1 ), (b) ''new char'' allocates a ''char'' object, which creates a memory leak and is totally unnecessary. This is not C# :-)

If you want to initialize the last cell of your buffer with a NULL character, then use

pBuffer[MAX_BUFFER_SIZE-1] = 0;



下一个错误是当您将缓冲区转换为CStringArray时:



The next error is when you cast your buffer into a CStringArray:

pStrArray = (CStringArray*)pBuffer;



您将必须解析缓冲区,并一一分配字符串数组的元素.简单地将char *转换为CStringArray *是行不通的.



You will have to parse your buffer and assign the elements of your string array one-by-one. Simply casting a char* into a CStringArray* doesn''t work.


这篇关于网络数据传输过程中接收到的缓冲区的访问冲突错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆