Что находится позади проблем утверждения CAsyncSocket и “неподходящего аргумента” ошибки в моем коде сокетов MFC?

File.ReadLines(filename) даст вам IEnumerable<string> с каждой парой ключ / значение, а затем string.split позволит вам отделить ключ и значение.

Например,

public static Dictionary<string, string> Load(string filename)
{
  var config = new Dictionary<string, string>();
  foreach(string kvp in File.ReadLines(filename))
  {
    var parts = kvp.split(" ");
    config.Add(parts[0], parts[1].Replace("\"", ""););
  }
  return config;
}
5
задан 15 revs, 4 users 93% 17 May 2009 в 21:20
поделиться

2 ответа

Your choice really. If you think you'll have more luck with another sockets implementation, then do it.

However, Microsoft has a lot of developers (and I believe some of them may even be good ones). You may, just may, want to consider the possibility that the fault doesn't all lay at their end.

The amount of help you can get for their APIs and products is also good, in my opinion.

Perhaps if you took the time to understand the MFC model, you would get that "AHA" moment and understand it better. I'm no fan of Winsock - I'm more used to the UNIX world where sync was the way to go, and you just ran separate processes/threads if you wanted async-type behavor.

CAsyncSocket, I suspect, is still hamstrung by the fact that MFC is a single-threaded model (in terms of GUI) even though Windows has had real pre-emptive threads for quite a while. [I may be wrong about that hamstrung comment, it's been a while since I used Win32 directly].


Update:

Based on your update where you stated what you were doing, I'm fairly certain you are not allowed to connect before creating. Quoting http://msdn.microsoft.com/en-us/library/3d46645f(VS.80).aspx,

To use a CAsyncSocket object, call its constructor, then call the Create function to create the underlying socket handle ... and for a client socket call the Connect member function.

As to why, I think this is an extra complexity added because Windows needs to do async sockets in an event-pumping environment since they cannot block the main GUI thread.

In UNIXy environments, there is either no event thread (normal processes) or network ops are just farmed off to another thread manually (in GUI apps).

This was most likely a design decision made in WinSock long ago (probably in Windows 3.11 which was a much more restrictive environment in which to do async operations) and carried through [although that's conjecture on my part, the UNIX sockets API has never had this sort of async behavior where messages are pumped, it always tended to use select() or threads/processes].


Further update:

That assertion (not exception) usually occurs if you have closed and/or deleted the socket object while there is a pending operation on it. In your case, I'd suggest it's still trying to do the connect when you close it.

Then when the connect succeeds or fails, the callback is called and it cannot find your socket in the tables.

This isn't an MFC problem, it's your friend's code violating the contract. If you do a connect (or any async operation), you have to wait for success or failure before closing the socket (or working further on it) - in this case, that means wait for the call to your OnConnect() function.

From memory, you call the create() when you create the async socket, then everything else happens in response to messages arriving in the message queue (i.e., calls to your OnXXX() functions). Like all Win32 GUI stuff, the messages are supposed to drive the program (the code runs in response to messages). This code is looking more and more like classic coding where the program drives everything - that way lies madness as you'll have your program and the async socket 'thread' fighting for control.

I haven't looked at it for quite a while but you should be able to get your hands on a CHATSRVR sample program which will show you how to do it.

6
ответ дан 13 December 2019 в 22:16
поделиться

The issue is likely to be poorly written code and there's a good chance that it has little or nothing to do with MFC. But it's hard to say for sure from your description which boils down to , "I have an MFC application that throws all sorts of debug assertions. What should I do?".

It might be that a rewrite is in order, but no one can really say without knowing more about the application. I think you (or your friend) will need to make that determination.

4
ответ дан 13 December 2019 в 22:16
поделиться
Другие вопросы по тегам:

Похожие вопросы: