0

I have some legacy code using a CAsyncSocket derived class. This class overrides OnReceive and it also has a wrapper around SendTo. The code is in a dll which multiple applications will be using on the same PC, all ports are hard coded.

I'm guessing that I need to provide synchronization in the form of a named mutex (CMutex in MFC) class bit I'm not sure.

Should I lock a named mutex in the OnReceive and a different named mutex in the Send methods of CMyAsyncSocket?

Do I need synchronization at all or will the MFC CAsyncSocket do that for me? Worried about accessing a socket on multiple processes.

jonsca
  • 10,218
  • 26
  • 54
  • 62
flobadob
  • 2,804
  • 2
  • 22
  • 23

1 Answers1

1

The underlying socket will provide connection isolation so you don't need to worry about protecting each processes connection. Assuming you just have one thread using the socket in each process you just need to handle the blocking issues which can occur with AsyncSocket, ie when the socket is not ready to deal with the request ?

snowdude
  • 3,854
  • 1
  • 18
  • 27
  • Thanks snowdude. If I have more than one thread using the socket in each process then what synchronization would I need to provide? I've read the Microsoft page on 'Windows Sockets: Blocking' and am still not sure what synchronization I would need to consider here. – flobadob Oct 10 '12 at 12:23
  • I'm not sure that I have multiple threads using the socket - am checking this. I do have the multiple processes though. You said previously: "Assuming you just have one thread using the socket in each process" which makes me wonder if multiple threads in one process is not catered for by CAsyncSocket but multiple processes are? Sorry but my understanding of how CAsyncSocket works is limited to the msdn docs. – flobadob Oct 10 '12 at 12:43
  • 1
    You only need to worry about synchronization if you have multiple threads accessing the SAME CAsyncSocket object (just as you would any multi-threaded access to the same bit of data). CAsyncSocket is just a wrapper for the underlying Winsock API functions, e.g. socket, accept, recv etc. http://msdn.microsoft.com/en-us/library/windows/desktop/ms740673(v=vs.85).aspx – snowdude Oct 10 '12 at 15:25
  • Ok thanks. So even if I had multiple processes on one PC talking to the same socket (let's say multicast mode, using the same multicast address & port) then the underlying CAsyncSocket will handle the synchronization for me. If I have multiple threads in a process using the same CAsyncSocket instance then I would need to add a criticalsection for synchronization? So, I never need a mutex with CAsyncSocket, just a CCriticalSection if I have multiple threads? – flobadob Oct 11 '12 at 08:33
  • 1
    The CAsyncSocket does not handle any synchronization for you, the underlying SOCKET does. Test it out. See what happens. – snowdude Oct 11 '12 at 10:32