Prev: Is this UTF-8 regular expression semantically correct?
Next: Newcomer's CAsyncSocket example: trouble connecting with other clients
From: stephen park on 13 May 2010 18:23 Lol ... so I ranted for a whole page not knowing you were replying. Can't we put semaphores on posts? :-) OK, I'll try what you suggested. Thanks!
From: Hector Santos on 13 May 2010 19:06 Joseph M. Newcomer wrote: > It also depends on what you are using for a protocol. Note that my example requires that > each "message" start with a 4-byte binary length value which is the number of bytes in the > packet, and a telnet message would not conform to that protocol, so it depends on how much > of my code you took to use. > joe I downloaded the zip, unzipped it and loaded the AsyncServer.sln solution. Change the port to something I can easily remember (13000) and hit F5. Used TELNET localhost 13000 and begin to type away, type ctrl ] and type QUIT and see the OP issue with the [?.?.?.?] ? Closed and traced it down. A quick fix was to add a "Cached" to the GetPeerPrefix() <g> Other than that, I did see some memory leaks and some non-critical exceptions and your packet model description above probably explains that. See ya -- HLS
From: Joseph M. Newcomer on 13 May 2010 23:29 I had not spotted any memory leaks, so I'd like to know, so I can fix them. joe On Thu, 13 May 2010 19:06:24 -0400, Hector Santos <sant9442(a)gmail.com> wrote: >Joseph M. Newcomer wrote: > >> It also depends on what you are using for a protocol. Note that my example requires that >> each "message" start with a 4-byte binary length value which is the number of bytes in the >> packet, and a telnet message would not conform to that protocol, so it depends on how much >> of my code you took to use. >> joe > >I downloaded the zip, unzipped it and loaded the AsyncServer.sln >solution. Change the port to something I can easily remember (13000) >and hit F5. > >Used > > TELNET localhost 13000 > >and begin to type away, type ctrl ] and type QUIT and see the OP issue >with the > > [?.?.?.?] ? Closed > >and traced it down. A quick fix was to add a "Cached" to the >GetPeerPrefix() <g> > >Other than that, I did see some memory leaks and some non-critical >exceptions and your packet model description above probably explains that. > >See ya Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Joseph M. Newcomer on 13 May 2010 23:32 See below... On Thu, 13 May 2010 18:21:23 -0400, Hector Santos <sant9442(a)gmail.com> wrote: >Ok, I downloaded and tested Joey's AsyncServer application. > >You're question is why you are seeing: > > ?.?.?.? [?] Closed > >Is that correct? > >The reason is that the socket is closed by the time the >GetPeerPrefix() is called in DoClose() thus the socket peer binding >information is already invalid. > >Here is the fix based on the idea that the peer information does not >change during the duration of the connection. > >1) In CONNECTS.H add a protected member: > > protected: > CString PeerInfo; > >2) In CONNECTS.CPP, add the following lines to the top and bottom of >GetPeerInfo() so it looks like this: > >CString CConnectSoc::GetPeerPrefix() > { > if (PeerInfo != "") return PeerInfo; // HLS FIX **** Try this: if(!PerrInfo.IsEmpty()) return PerrInfo; Note that it should be _T("") **** > CString ip; > UINT port; > GetPeerName(ip, port); > if(ip.IsEmpty()) > { /* no name */ > return _T("?.?.?.? [?]"); > } /* no name */ > CString s; > s.Format(_T("%s [%u]"), ip, port); > PeerInfo = s; // HLS FIX > return s; > } // CConnectSoc::GetPeerPrefix > >In other words, once the PeerInfo is set, it no longer needs to get it >again, it it good for the entire session. the IP and PORT will not >ever change during this session. > >That will eliminate this minor logging issue. > >Now, again with minimum review of this code, I don't see it doing >anything useful for you with a client application. > >Add this to test a simple "Echo" server: > >1) Need the top of CONNECTS.CPP add: > >#define HLS_TEST_ECHO_SERVER > >2) In the CConnectSoc::Receive() function, add this before the return >result line: > >#ifdef HLS_TEST_ECHO_SERVER > Send(Buf,result,0); >#endif > >Now, you can test this with any TTY or telnet application, whatever >you type, AsyncServer will echo it back. > >Hope this helps > >--- >HLS > > > > > >stephen park wrote: > >>> I don't know what Joe's program is, but the above is correct when you >>> are talking to a telnet-like like server. I have no reason to >>> question Joe's CAsyncServer, but I have not try it to see what it >>> suppose to do for you. >>> >>> What part are you trying to make work? What are you looking for? >>> >>> What I would do is begin with the basics of writing a TTY client. If >>> you are just learning sockets, you need to begin with the >>> basics, which comes with "mistakes" and trial and error process. >>> >>> Try this TTY() function: >>> >>> void tty(String ^hostname, int port) >>> { >>> >>> Console::WriteLine("* Connecting"); >>> >>> TcpClient^ c; >>> >>> try { >>> c = gcnew TcpClient(hostname,port); >>> } catch(SocketException ^e) { >>> Console::WriteLine(L"- Connect Error {0}",e->ErrorCode); >>> Console::ReadKey(); >>> return; >>> } >>> >>> // use 100ms timeout for reader >>> c->Client->ReceiveTimeout = 100; >>> >>> array<Byte>^ bytes = gcnew array<Byte>(1024); >>> NetworkStream ^cio = c->GetStream(); >>> >>> while (1) { >>> if (Console::KeyAvailable) { >>> ConsoleKeyInfo^ key = Console::ReadKey(true); >>> if (key->Key == ConsoleKey::Escape) break; >>> try { >>> cio->WriteByte(key->KeyChar); >>> } catch( Exception ^e) { >>> break; >>> } >>> } >>> try { >>> int nBytes = c->Client->Receive( bytes ); >>> for (int i=0; i < nBytes; i++) { >>> Console::Write("{0}",(wchar_t)bytes[i]); >>> } >>> } catch( SocketException ^e) { >>> if (e->ErrorCode == 10054) { >>> Console::WriteLine(L"! Disconnect"); >>> break; >>> } >>> } >>> } >>> c->Close(); >>> Console::WriteLine("* Closed"); >>> Console::ReadKey(); >>> >>> } >> >> >> Interesting - this code doesn't work either. Dr. Newcomer's server >> says it got a connection, but when I type stuff in nothing gets sent. >> I can CTRL-C and then the server says its disconnected. Sooo ... >> apparently nothing is getting sent? >> >> I did try calling the .net versions of shutdown() and the >> NetworkStream class even has a flush() function, although the doc says >> that its reserved for future use. I called it anyway in my code but >> still nothing got sent. >> >> Interestingly enough, if I try telnetting to Dr. Newcomer's server I >> get the same result as the tty code above - it says its connected, but >> nothing gets sent? I could type stuff in, then hit Ctrl-D, Ctrl-G, >> whatever Ctrl combo that might hopefully say I'm done sending stuff, >> but nothing goes ... >> Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Joseph M. Newcomer on 17 May 2010 01:17
Thanks, I'll look into both of those. joe On Fri, 14 May 2010 03:40:49 -0400, Hector Santos <sant9442(a)gmail.com> wrote: >Joseph M. Newcomer wrote: > >> I had not spotted any memory leaks, so I'd like to know, so I can fix them. >> joe > >Just all I did was compile the code and before and after the minor >changes for the PeerInfo stuff and adding a simple echo logic, I used >a simple TTY to connect and type away. > >1) After the first 3-4 letters, there was an exception. I believe your >explanation that it assumes a 4 byte packet or whatever explains that. > >2) And when exiting the application, the IDE dumped a few leaks. > >Let me try it again.... > >Ok, started the AsyncServer under the IDE, port 13000. > >Open a Penalty Box and typed > > Telnet LocalHost 13000 > >typed > > HELLO<enter> > ctrl ] > QUIT > >The socket closed and clicked EXIT, the IDE output shows: > >Dumping objects -> >f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\sockcore.cpp(933) : {154} >normal block at 0x00369D90, 28 bytes long. > Data: < s ` > CD CD CD CD 73 03 00 00 60 0F 00 00 01 00 >00 00 >f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\sockcore.cpp(933) : {150} >normal block at 0x00369658, 28 bytes long. > Data: < s ` > CD CD CD CD 73 03 00 00 60 0F 00 00 01 00 >00 00 >Object dump complete. > > >Its odd that it shows that path. I don't see RTL LIB that your binding. > >I just also did another thing. > >I compiled the AsyncClient and ran it to connect to the AsyncClient. > >I connected and entered "hello" in the Send field, clicked Send and >POOF! the AsyncServer faulted. Here is the call stack: > >AsyncServer.exe!CAsyncServerDlg::OnNetworkData(unsigned int >wParam=3579544, long lParam=1700) Line 421 + 0xd bytes C++ > mfc80d.dll!CWnd::OnWndMsg(unsigned int message=32970, unsigned int >wParam=3579544, long lParam=1700, long * pResult=0x0012f850) Line >2004 + 0x11 bytes C++ > >mfc80d.dll!CWnd::WindowProc(unsigned int message=32970, unsigned int >wParam=3579544, long lParam=1700) Line 1741 + 0x20 bytes C++ > >mfc80d.dll!AfxCallWndProc(CWnd * pWnd=0x0012fb5c, HWND__ * >hWnd=0x0019056c, unsigned int nMsg=32970, unsigned int wParam=3579544, >long lParam=1700) Line 240 + 0x1c bytes C++ > >mfc80d.dll!AfxWndProc(HWND__ * hWnd=0x0019056c, unsigned int >nMsg=32970, unsigned int wParam=3579544, long lParam=1700) Line 389 C++ > >mfc80d.dll!AfxWndProcBase(HWND__ * hWnd=0x0019056c, unsigned int >nMsg=32970, unsigned int wParam=3579544, long lParam=1700) Line 411 + >0x15 bytes C++ > >user32.dll!7e418734() >user32.dll!7e418816() >user32.dll!7e4189cd() >user32.dll!7e4196c7() >mfc80d.dll!AfxInternalPumpMessage() Line 183 C++ >mfc80d.dll!CWinThread::PumpMessage() Line 896 C++ >mfc80d.dll!AfxPumpMessage() Line 190 + 0xd bytes C++ > >mfc80d.dll!CWnd::RunModalLoop(unsigned long dwFlags=4) Line 4322 + >0x5 bytes C++ > >mfc80d.dll!CDialog::DoModal() Line 587 + 0xc bytes C++ > AsyncServer.exe!CAsyncServerApp::InitInstance() Line 67 + 0xb >bytes C++ > >mfc80d.dll!AfxWinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ >* hPrevInstance=0x00000000, char * lpCmdLine=0x00151f3b, int >nCmdShow=1) Line 37 + 0xd bytes C++ > >AsyncServer.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, >HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x00151f3b, >int nCmdShow=1) Line 33 C++ > >AsyncServer.exe!__tmainCRTStartup() Line 589 + 0x35 bytes C >AsyncServer.exe!WinMainCRTStartup() Line 414 C >kernel32.dll!7c817077() Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm |