Click here to Skip to main content
15,892,059 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to display a progress bar while video is playing...MFC.? Pin
mbatra3131-May-13 4:04
mbatra3131-May-13 4:04 
Questionbinding client and server Pin
noislude30-May-13 10:13
noislude30-May-13 10:13 
AnswerRe: binding client and server Pin
Richard MacCutchan30-May-13 21:11
mveRichard MacCutchan30-May-13 21:11 
GeneralRe: binding client and server Pin
noislude31-May-13 2:00
noislude31-May-13 2:00 
GeneralRe: binding client and server Pin
Richard MacCutchan31-May-13 6:50
mveRichard MacCutchan31-May-13 6:50 
Questionipaddress for CAsyncSocket Class Pin
ForNow30-May-13 2:27
ForNow30-May-13 2:27 
AnswerRe: ipaddress for CAsyncSocket Class Pin
Richard MacCutchan30-May-13 2:54
mveRichard MacCutchan30-May-13 2:54 
GeneralRe: ipaddress for CAsyncSocket Class Pin
ForNow30-May-13 4:48
ForNow30-May-13 4:48 
a little background first I am writting a front end to the Hercules MainFrame Emulator this MFC C++ app

I also have a MVS started task communicating with this MFC C++ app when I first bought the Lap top
and moved in my code it worked the its a quad core lap top that I bought in August

Since then my machine has slowed and I am getting the timeouts

I Tried using Socket and Bind to Create the socket as opposed to just using the create

but in case it does take the IP address I use 192.168.1.4 which I think is the IP address of the laptop

I tried 192.168.1.111 as that is the ip address of my laptop 192.168.1.4 is routed to 192.168.1.111

I have a a loop in My CWinApp:: initinstance creating 4 socket threads

C++
 int i;
UINT start_port;

     // Sockevent.SetEvent();

        for (i = 0, start_port = 11007; i < 4; start_port++, i++)
   {
 #ifdef _DEBUG
	   msOld.Checkpoint();
 #endif

         threadptr[i] = new SockCLeintThread(start_port);
 #ifdef _DEBUG

		   msnew.Checkpoint();

 if( diffMemState.Difference(msOld, msnew))
 {
   diffMemState.DumpStatistics();
   diffMemState.DumpAllObjectsSince();
 }
 #endif
 

         
		 
         if (threadptr[i] == NULL)
  	        m_pMainWnd->MessageBox((LPCTSTR)"SockClientThreadFail",NULL,MB_ICONERROR);

	        threadptr[i]->CreateThread(CREATE_SUSPENDED,0,NULL);  
   
                 threadptr[i]->flags.is_connected = 0;
             
                 threadptr[i]->ipaddr = (LPCTSTR)"192.168.1.4";					// ip address
  
 			 threadptr[i]->flags.busy = 0;
 
 			 SetThreadName(threadptr[i]->m_nThreadID,thread[i]);
 					
               
 	
       	 //   threadptr[i]->SetThreadPriority(THREAD_PRIORITY_TIME_CRITICAL); 
 		threadptr[i]->ResumeThread();

		WaitForSingleObject(*pevent,INFINITE);   // wait for connection

//              threadptr[i]->SetThreadPriority(THREAD_PRIORITY_NORMAL);    
		
		}     


in My CWinTread initinstance I try to create the socket
C++
BOOL SockCLeintThread::InitInstance()
   {
int error_code, threadpri;
MSG m_msgCur;
extern void SetThreadName( DWORD dwThreadID, LPCSTR szThreadName);
HANDLE mysocket;
int val = sizeof(int);
int *val_ptr = &val;
//  CCriticalSection socklock;
  //         socklock.Lock();

	//	  threadpri = GetThreadPriority();
		    thisocket.Socket(SOCK_STREAM,FD_READ|FD_WRITE|FD_CONNECT,NULL,AF_INET);
			if(thisocket.Bind(thisocket.port,NULL) == 0)
				 error_code = GetLastError();
			int* len = new int;
			thisocket.herccmd= new SOCKADDR;
			if(thisocket.GetSockName(thisocket.herccmd,(int *)len) == 0)
				error_code = GetLastError();
			if(thisocket.GetSockOpt(SO_KEEPALIVE | SO_DEBUG, &thisocket.option,val_ptr,  SOL_SOCKET) == 0)
				 error_code = GetLastError();
		    if(thisocket.SetSockOpt(SO_KEEPALIVE | SO_DEBUG, &thisocket.option,val, SOL_SOCKET) == 0)
				 error_code = GetLastError(); 
		
			 

       //   	 	 if(thisocket.Create(thisocket.port,SOCK_STREAM,FD_READ|FD_WRITE|FD_CONNECT, NULL)== 0)
      //    {
       //                   error_code =  GetLastError();
        //           }
				  

				  if(thisocket.AsyncSelectFD_READ|FD_WRITE|FD_CONNECT|FD_CLOSE|FD_OOB|FD_ACCEPT) == 0) 
 			                   error_code =  GetLastError();
			 

       

         //   SetThreadPriority(THREAD_PRIORITY_HIGHEST); 

 


      //    WaitForSingleObject(Sockevent.m_hObject,INFINITE);

           if( thisocket.Connect(ipaddr,thisocket.port) == 0)
                  {
 		                         error_code =  GetLastError();
                   }
 		   else
 				 	  flags.is_connected = 1;
		  
		//   SetThreadPriority(threadpri);       // set it back
	         //		socklock.Unlock();   
                  // PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE);   // Create Message Queue



    return TRUE;                                                   // [11]
   }


the CASyncSockt::OnConnect

C++
void SockClient :: OnConnect(int nErrorCode)
  {
	   CAsyncSocket::OnConnect(nErrorCode);
	   CHERC_CMDApp* main_app;
	    main_app = (CHERC_CMDApp *) AfxGetApp(); 
	   	main_app->pevent->SetEvent();

	  if (0 != nErrorCode)
   {
      switch( nErrorCode )
      {
         case WSAEADDRINUSE: 
            AfxMessageBox("The specified address is already in use.\n");
            break;
         case WSAEADDRNOTAVAIL: 
            AfxMessageBox("The specified address is not available from the local machine.\n"); 
               break;
         case WSAEAFNOSUPPORT: 
            AfxMessageBox("Addresses in the specified family cannot be  used with this socket.\n");
              break;
         case WSAECONNREFUSED: 
            AfxMessageBox("The attempt to connect was forcefully rejected.\n");
             break;
         case WSAEDESTADDRREQ: 
            AfxMessageBox("A destination address is required.\n");
            break;
         case WSAEFAULT: 
            AfxMessageBox("The lpSockAddrLen argument is incorrect.\n");
            break;
         case WSAEINVAL: 
            AfxMessageBox("The socket is already bound to an  address.\n");
            break;
         case WSAEISCONN: 
            AfxMessageBox("The socket is already connected.\n");
            break;
         case WSAEMFILE: 
            AfxMessageBox("No more file descriptors are available.\n");
            break;
         case WSAENETUNREACH: 
            AfxMessageBox("The network cannot be reached from this host   at this time.\n");
              break;
         case WSAENOBUFS: 
            AfxMessageBox("No buffer space is available. The socket cannot be connected.\n");
              break;
         case WSAENOTCONN: 
            AfxMessageBox("The socket is not connected.\n");
            break;
         case WSAENOTSOCK: 
            AfxMessageBox("The descriptor is a file, not a socket.\n");
            break;
         case WSAETIMEDOUT: 
            AfxMessageBox("The attempt to connect timed out without establishing a connection. \n");
              break;
         default:
            TCHAR szError[256];
            wsprintf(szError, "OnConnect error: %d", nErrorCode);
            AfxMessageBox(szError);
            break;
      }
      AfxMessageBox("Please close the application");
   }
  // CAsyncSocket::OnConnect(nErrorCode);

             // mainapp thread
SockCLeintThread* threadptr;         // this SockCLeintThread

   // Get Main App
 
 
	 //   SockCLeintThread* current; 

	// if(nErrorcode != 0)
		// main_wnd->MessageBox((LPCTSTR)"Port Not Connected",(LPCTSTR)"Port",MB_ICONERROR);
	//	 Sockevent.SetEvent();
	 //  SockCLeintThread* current;
	  //   (CWinThread *) current = AfxGetThread();
      // UNREFERENCED_PARAMETER(nErrorcode);
        //  CAsyncSocket::OnConnect(nErrorCode);
		  threadptr = (SockCLeintThread *) AfxGetThread();
          threadptr->flags.is_connected = 1;       
  //    if(nErrorcode == WSAETIMEDOUT)
	//	  Connect(current->ipaddr,current->port);

        AsyncSelect(FD_WRITE);

	
   //    main_app->pevent->SetEvent();
  }

The CwinThread Derived Class

C++
 class SockCLeintThread : public CWinThread {
   public:
	      DECLARE_DYNCREATE(SockCLeintThread)
        //		DECLARE_SERIAL(SockCLeintThread)
       //     IMPLEMENT_DYNAMIC(SockCLientThread)
        public:
			     SockCLeintThread();
			     SockCLeintThread(UINT myport);
				 ~SockCLeintThread();
                CWnd *sendwindow;
                CWnd *call_wnd;
       			char *thread_id;
				 SockClient thisocket;           <== // Socket to do ocommunication
				 void Sendit(WPARAM, LPARAM);
				void Receiveit(WPARAM, LPARAM);
			    LPCTSTR ipaddr;
                BOOL idle;				 // looking for work
		//	 allexceptions myallexception;
				 
				struct {
					unsigned int is_connected : 1;
					unsigned int busy : 1;
					unsigned int not_connected : 1;
				} flags;
				
protected:
	  virtual BOOL InitInstance();      // Just for starters
	//  virtual BOOL OnIdle(LONG lCount);
	//  virtual int Run();
           DECLARE_MESSAGE_MAP()

};


The CAsyncSocket Derived Class

C++
class SockClient : public CAsyncSocket
{      
	DECLARE_SERIAL(SockClient)
 public:
      // SockCleint(char *sockptr);
	    SockClient();
        SockClient(UINT myport);
		~SockClient();
        char *sockbuffer;		// send and receive buffer
		int num_buff;
	//	CWnd *send_wnd;
		int thread_no;
		UINT port;
		SOCKADDR* herccmd;                     // HERCMCD IP ADDRESS
		SOCKADDR MVS;		// MVS IP address
		int option;
	private:
		int busy;               // inuse
		               // port used by this class
   protected:
                         // window to send message to
        virtual void OnReceive(int nErrorCode);
        virtual void OnSend(int nErrorCode);
        virtual void OnConnect(int nErrorCode);
		virtual void OnClose(int nErrorCode);

		
		
};


I know its not formatted that neatly but I am always fixing the code

Thanks
GeneralRe: ipaddress for CAsyncSocket Class Pin
Richard MacCutchan30-May-13 5:27
mveRichard MacCutchan30-May-13 5:27 
GeneralRe: ipaddress for CAsyncSocket Class Pin
ForNow30-May-13 5:54
ForNow30-May-13 5:54 
GeneralRe: ipaddress for CAsyncSocket Class Pin
Richard MacCutchan30-May-13 6:21
mveRichard MacCutchan30-May-13 6:21 
GeneralRe: ipaddress for CAsyncSocket Class Pin
ForNow31-May-13 4:34
ForNow31-May-13 4:34 
GeneralRe: ipaddress for CAsyncSocket Class Pin
Richard MacCutchan31-May-13 6:52
mveRichard MacCutchan31-May-13 6:52 
GeneralRe: ipaddress for CAsyncSocket Class Pin
ForNow31-May-13 9:29
ForNow31-May-13 9:29 
GeneralRe: ipaddress for CAsyncSocket Class Pin
Richard MacCutchan31-May-13 10:36
mveRichard MacCutchan31-May-13 10:36 
QuestionNetwork simulator -3 : problem in recieving data. Pin
Manoj739029-May-13 19:29
Manoj739029-May-13 19:29 
AnswerRe: Network simulator -3 : problem in recieving data. Pin
dusty_dex30-May-13 3:35
dusty_dex30-May-13 3:35 
QuestionC++ program is not building in G++ (Ubuntu) Pin
Manoj739029-May-13 19:24
Manoj739029-May-13 19:24 
AnswerRe: C++ program is not building in G++ (Ubuntu) Pin
Richard MacCutchan29-May-13 20:58
mveRichard MacCutchan29-May-13 20:58 
GeneralRe: C++ program is not building in G++ (Ubuntu) Pin
Manoj739029-May-13 21:22
Manoj739029-May-13 21:22 
QuestionRe: C++ program is not building in G++ (Ubuntu) Pin
CPallini29-May-13 21:30
mveCPallini29-May-13 21:30 
AnswerRe: C++ program is not building in G++ (Ubuntu) Pin
Manoj739029-May-13 21:41
Manoj739029-May-13 21:41 
GeneralRe: C++ program is not building in G++ (Ubuntu) Pin
CPallini29-May-13 22:09
mveCPallini29-May-13 22:09 
GeneralRe: C++ program is not building in G++ (Ubuntu) Pin
Richard MacCutchan29-May-13 21:40
mveRichard MacCutchan29-May-13 21:40 
GeneralRe: C++ program is not building in G++ (Ubuntu) Pin
Manoj739029-May-13 21:53
Manoj739029-May-13 21:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.