/*------------------------------------------------------------------------------------------------------------------------------------------------- Program Listing for: MySocketExample.cpp Project: socketclass Namespace: c++ ---------------------------------------------------------------------------------------------------------------------------------------------------- */ #include "stdafx.h" #include "MySocket.h" // not using ATL string stuff since this is an MFC app, but we do use a few // standard library string functions #include <string.h> // prototypes are "inline" for this very quick example int MB(char *cstring, int flags = MB_OK, char *title = NULL); void SampleCall1(void) ; void SampleCall2(void) ; // two samples of how to use the csocket class stuff // SampleCall() // performs a raw socket get (warning, if the server uses chunking, you will have to // remove it yourself, chunking is a way that apache returns web sites by pieces // so it will have a few numeric values thrown into the raw response string) void SampleCall1(void) { CMySocket newconn("www.sony.com"); CString response = newconn.GetFileAsString("/", "www.sony.com"); MB( response ); return; } // // SampleCall2() attempts to connect to an actual IP at a specific port // ideally this should be called from a thread void SampleCall2(void) { CMySocket newconn("63.230.230.145", 29807 ); CString direct_response; bool bSentOkay; // if connected okay if (! newconn.bError ) { // attempt to send this string via the socket newconn.socksend("ground control to major Tom\r\n"); // if no errors (so far) if (! newconn.bError ) { int ncnt = 0; // sometimes there is a slight delay after connect before reading is allowed, // since this is in a thread we can just // use a slight pause (unscientific but appears to work) Sleep(400); // loop thru a few times and see if we can get a confirmation string back, like "ok" // for this example we assume they are only sending 200 chars char *direct_response2 = new char[201]; // make the string of length zero initially direct_response2[0] = (char)0; // offset is used as a position marker to add to string int ioffset = 0; // try for 5 seconds total, no reply or activity within 5 seconds is log off condition while (! newconn.bFinished && ! newconn.bError && (ncnt++ < 20) ) { // attempt to receive anything pending in socket buffer, note this is non-blocking (hence // the while loop) TRACE("calling sock receive\n"); newconn.sockreceive(); if ( strlen(newconn.myout) > 0) { if ( strlen(newconn.myout) + ioffset > 200 ) { strncpy(direct_response2 + ioffset, newconn.myout, 200 - ioffset ); direct_response2[200] = (char)0; break; } TRACE("appending to direct response 2\n"); strcpy(direct_response2 + ioffset, newconn.myout); ioffset += strlen(newconn.myout); } // calling sleep is nice otherwise we are performance hogging a bit which isn't necessary // there is no real hurry going on here no need to continually be hitting sockreceive // i could also make sockreceive blocking I might do that, but this offers a nice level of // extra control. blocking timeout vals are weird under Windows. Sleep(100); // how does multi-tasking under windows handle sleep? does it ignore the function // or thread that is calling sleep for a bit? I hope so but not sure. } direct_response = direct_response2; delete [] direct_response2; // they should send "ok:" type messages back, add further confirmation type processing later // e.g. checksums back or something to verify they got the full message or whatever if ( direct_response.Left(9) == "Here am I") { bSentOkay = true; MB( direct_response ); } } } return } int MB(char *cstring, int flags, char *title) { if (! title ) return (::MessageBox(NULL, cstring, "", flags )); else return (::MessageBox(NULL, cstring, title, flags )); }