KaiserJohaan
KaiserJohaan

Reputation: 9240

Win32 asynchronous sockets basic question

I've done a client-server voice chat (like ventrilo) in C# and am now doing it in unmanaged win32 c++. I've got a question about how asynchronous reads/writes are handled in c++. In C#, you had a function that got called (EndSend/EndRecieve) once BeginSend/BeginRecieve were done. How is this done in c++? I've looked at msdn and googled and couldn't find an answear.

Here's the code for example that runs when a button is pressed, triggering a WM_WRITE message that called this function:

[...]
case ID_BUTTON_SEND:
    SendMessage(hWnd,SOCKET_TCP,0,FD_WRITE);
    break;

[....]
case FD_WRITE:
      onTextSend()
      break;
[....]

void onSendText(HWND hWnd) {
// get window handle to send edit control
HWND hWndSend = GetDlgItem(hWnd,ID_EDIT_SEND);

// get textlength of send edit control
int textlen = GetWindowTextLength(hWndSend);

// allocate memory for a buffer that will contains the text to be sent and get the text
char* textbuff = (char*)malloc(textlen+1);
SendMessage(hWndSend,WM_GETTEXT,textlen+1,(LPARAM)textbuff);

// send content
send(tcp_sock,textbuff,textlen+1,0);

// cleanup allocated tempbuffer
  free(textbuff);
}

Of course I don't want to initiate an asynchronous send and then proceed to deallocate the buffer used in the send directly, I want to deallocate it once send is complete (like EndSend). I havn't found the c++ way to do it, any suggestions?

EDIT: ALSO! for some reason, when the server accepts the client, the client is not getting a FM_CONNECT message, shouldn't it get it as soon as the client is connected? I can send() and recv() so the connection is definately up.

Upvotes: 0

Views: 761

Answers (2)

Andriy Tylychko
Andriy Tylychko

Reputation: 16256

and if you don't want to deal with Asio, here's good tutorial about asynchronous sockets: http://www.gamedev.net/reference/programming/features/asyncsock/

Upvotes: 2

Andriy Tylychko
Andriy Tylychko

Reputation: 16256

have a look at Boost Asio library. even if you don't need cross-platform solution, Asio is great C++ library that provides asynchronous sockets. an example of asynchronous read/write

Upvotes: 3

Related Questions