MFC Listener

Thread Solved

Join Date: Dec 2005
Posts: 43
Reputation: Nedals is an unknown quantity at this point 
Solved Threads: 0
Nedals Nedals is offline Offline
Light Poster

MFC Listener

 
0
  #1
Feb 2nd, 2006
I'm attempting to build a simple listener, preferably without a Thread, but I am lost.

I have a working listener in 'console' mode but I need one that works with MFC. It should stay open indefinately - (until I send a shutdown message)

This is my initial non-working attempt. It connects and listens OK, but the Accept() function only receives the first line sent and then freezes. (using the code as shown - while and do loops edited out)
  1. CString clientIP = "xxx.xxx.xxx.xxx";
  2.  
  3. CSocket hsock;
  4. hsock.Create(port,SOCK_STREAM,clientIP);
  5. hsock.Listen(5);
  6.  
  7. char buf[512];
  8.  
  9. // I know this is NOT correct, but I cannot figure out hwo to make it work
  10. //while (1) {
  11. CSocket tsock;
  12. hsock.Accept(tsock);
  13.  
  14. int bytes = hsock.Receive(buf,sizeof(buf),0);
  15. // do {
  16. AfxMessageBox(buf);
  17. // }
  18. //}
I've read thought Micro$ confusing MFC documentation on Socket, CSocket, AsyncSocket and I come away more confused that when I started. I think I need to use Archive(), but again I get lost.

Any help would be very welcome, but please, no links to the Micro$ site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: MFC Listener

 
0
  #2
Feb 3rd, 2006
What happens when you change your code to this?

  1. char buf[512] = "";
  2. CSocket tsock;
  3. if ( hsock.Accept(tsock) == 0 )
  4. {<blockquote>// Error
  5. return;</blockquote>}
  6. while (1)
  7. {<blockquote>int bytes = tsock.Receive(buf,sizeof(buf),0);
  8. if ( bytes == 0 ) // No data read. Close the Socket.
  9. {<blockquote>break;</blockquote>}
  10. if ( bytes == SOCKET_ERROR )
  11. {<blockquote>AfxMessageBox("Error";
  12. break;</blockquote>}
  13. AfxMessageBox(buf);</blockquote>}
  14. tsock.Close();
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 43
Reputation: Nedals is an unknown quantity at this point 
Solved Threads: 0
Nedals Nedals is offline Offline
Light Poster

Re: MFC Listener

 
0
  #3
Feb 3rd, 2006
Thanks WolfPack. That kind-of worked. The while loop causes the, I'm busy 'hour-glass' to sit on the page but I am getting results.

It dawned on me that a listener is simply a server without a send capability. Googling for Socket Server, returned some interesting techniques. I think I may be able to get this working now. I'll post back a working solution or MORE questions.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 43
Reputation: Nedals is an unknown quantity at this point 
Solved Threads: 0
Nedals Nedals is offline Offline
Light Poster

Re: MFC Listener

 
0
  #4
Feb 3rd, 2006
My solution - with help from my Google search results...
  1. // Initilize the Listener - (in OnNewDocument for me)
  2. BOOL CMFCSocketDoc::OnNewDocument() {
  3. ..
  4. // #include "MFCServer.h" in .doc.h
  5. // declare... MFCServer skt_server; in ...doc.h
  6. skt_server.Create(port#);
  7. skt_server.Listen();
  8. return true;
  9. }
  10.  
  11. // Create 2 new classes:
  12. class MFCServer : public CAsyncSocket {...};
  13. // Add this function
  14. void MFCServer::OnAccept(int nErrorCode) {
  15. // #include "MFCClient.h" in .h
  16. // declare... MFCClient skt_client; in .h
  17. Accept(skt_client);
  18. CAsyncSocket::OnAccept(nErrorCode);
  19. }
  20.  
  21. class MFCClient : public CAsyncSocket {...};
  22. // Add this function
  23. void MFCClient::OnReceive(int nErrorCode) {
  24. char buff[200];
  25. int bytes_read = Receive(buff, 200);
  26. buff[bytes_read] = 0; //terminate the string
  27.  
  28. AfxMessageBox(buff);
  29. CAsyncSocket::OnReceive(nErrorCode);
  30. }
And there you have it - A simple listener without threads.
If I did something REALLY wrong, please let me know.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 43
Reputation: Nedals is an unknown quantity at this point 
Solved Threads: 0
Nedals Nedals is offline Offline
Light Poster

Re: MFC Listener

 
0
  #5
Feb 3rd, 2006
I'm back

I incorporated the above into my program and it worked - well almost.

The computer (listener) is connected to a device that closes it's socket after an idle time of 15 sec to save on network resources. In my 'console' version, when the device comes alive, data is sent and received. - no problem.

But in the MFC version, after this idle time-out period and on the next read, it creates a 'Debug Assertion Failed' popup (sockcore.cpp, Line 117). If I then click on 'Ignore', the data is read. All additional data goes through the same cycle.

So what did I do wrong?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 43
Reputation: Nedals is an unknown quantity at this point 
Solved Threads: 0
Nedals Nedals is offline Offline
Light Poster

Re: MFC Listener

 
0
  #6
Feb 7th, 2006
A few hours of testing and head scratching, and I came up with a solution. Pretty simple really...

When the device closed it's socket, it attempted to reconnect but the skt_client socket was still open and the OnAccept tried to create a new client socket. So I simply closed the socket and allowed a new one to be opened.
  1. void MFCServer::OnAccept(int nErrorCode) {
  2. CAsyncSocket::OnAccept(nErrorCode);
  3. skt_client.Close();
  4. Accept(skt_client);
  5. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC