-4

I tried to use std::string in WSARecv (winsock), but it didnt work, can you tell me if it's possibleand and how it works

Vlaidslav
  • 13
  • 1

1 Answers1

1

You could initalize your WSABUF structures that you pass to WSARecv, so that the *buf pointer in each WSA buf points to the buffer of a prepared string opbject, something along the lines:

std::string myStringBuffer;
myStringBuffer.resize(1024);
WSABuf wsaBuffer;
wsaBuffer.len = 1024;
wsaBuffer.buf = &myStringBuffer[0];
  • or `buf = myStringBuffer.data()` in C++17 and later. And I would use `len = myStringBuffer.size()` instead of hard-coding the `len`, in case you want to change the size of `myStringBuffer` later, or make it dynamic. – Remy Lebeau Apr 05 '19 at 22:02
  • @Remy: correct, although `len = myStringBuffer.size()` might need a static_cast to avoid compiler warnings, which is why I didn't mention it. – Marco Freudenberger Apr 06 '19 at 07:08