0
我試圖從客戶端獲取數據塊(800字節)多次。在我試圖獲得沒有前綴長度信息的數據塊之前,我開始發送帶有數據塊的前綴長度信息的數據塊到服務器。UDP套接字中的訪問衝突讀取位置recvfrom
在這兩種情況下,我只在服務器端收到數據塊一次。通過調試我的服務器端我發現,當我while(1)
環路嘗試執行第二次,我得到一個調試錯誤說
First-chance exception at 0x012e2e53 in UDP_server.exe: 0xC0000005: Access violation reading location 0xbd63841d.
Unhandled exception at 0x012e2e53 in UDP_server.exe: 0xC0000005: Access violation reading location 0xbd63841d.
我已經試過很多事情我自己,但依然沒能弄清楚什麼事。
//服務器代碼
int total_bytes = 0;
int bytes_recv=0;
int count = 0;
uint32_t nlength =0;
std::vector<double> m_vector(nlength/sizeof(double));
int length_received;
while(1)
{
//code to received data length from the client
length_received = recvfrom(Socket,(char*)&nlength, 4, 0,(SOCKADDR*)&ClientAddr,&i);
m_vector.resize(nlength/sizeof(double));
//code to received data length from the client
int bytes_recv = recvfrom(Socket,(char*)&m_vector,nlength,0,(SOCKADDR*)&ClientAddr,&i);
count++;
if((bytes_recv > 0))
{
total_bytes = total_bytes+bytes_recv;
std::cout<<"Server: loop counter is"<<count<<std::endl;
std::cout<<"Server: Received bytes are"<<total_bytes<<std::endl;
}else
{
std::cout<<"Data Receiving has finished"<<std::endl;
break;
}
}
試試這個'(char *)&m_vector [0]'。 – imreal
@Nick:感謝您收到錯誤信息,現在我正在使用'm_vector.data()',並且您的建議工作得很好! – User
@Nick POSIX'recvfrom'接受'void *',我猜我必須在Windows上將它轉換爲'char *'。 –