Click here to Skip to main content
15,889,281 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: C programming question Pin
Richard MacCutchan7-Jul-12 0:48
mveRichard MacCutchan7-Jul-12 0:48 
GeneralRe: C programming question Pin
Code-o-mat7-Jul-12 9:04
Code-o-mat7-Jul-12 9:04 
GeneralRe: C programming question Pin
Richard Andrew x647-Jul-12 10:55
professionalRichard Andrew x647-Jul-12 10:55 
GeneralRe: C programming question Pin
BCN-1637-Jul-12 16:24
BCN-1637-Jul-12 16:24 
GeneralRe: C programming question Pin
Code-o-mat7-Jul-12 23:20
Code-o-mat7-Jul-12 23:20 
AnswerRe: C programming question Pin
CPallini8-Jul-12 9:45
mveCPallini8-Jul-12 9:45 
JokeRe: C programming question Pin
Albert Holguin8-Jul-12 17:12
professionalAlbert Holguin8-Jul-12 17:12 
QuestionSocket connection problem Pin
ForNow6-Jul-12 3:08
ForNow6-Jul-12 3:08 
Hi,

I have a loop for which I am tryig to connect on four ports for which I only get two
connections I know sometimes sockets take a while to connect

the Creation of the Socket and connection take place in CWinThread Dervived ::Initinstance after the CAsyncSocket objects have been created


Server is a Dos "C" program on the same Windows Vista machine

below is the loop

I am connected thru a router that is kind of old don't know if thats the issue

or If an imporvment in my hardware might help

I put my derived CAsynSocket class inside a a CWinThread class hoping that if such

issues arise I might raise the priority of the thread

looking for any pointers from the experts out there

// loop in CWinApp::Initinstance

C++
  for (i = 0, start_port = 11007; i < 4; start_port++, i++)
{
        threadptr[i] = new SockCLeintThread(start_port);

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

          threadptr[i]->CreateThread(CREATE_SUSPENDED,0,NULL);

          threadptr[i]->flags.is_connected = 0;

           threadptr[i]->ipaddr = "192.168.1.4";                   // ip address

           SetThreadName(threadptr[i]->m_nThreadID,thread[i]);




          threadptr[i]->ResumeThread();


     }


// My Casynsocket .h file

C++
lass SockClient : public CAsyncSocket
{      
	DECLARE_SERIAL(SockClient)
 public:
      // SockCleint(char *sockptr);
        SockClient(UINT myport);
		SockClient();
		~SockClient();
        char *sockbuffer;		// send and receive buffer
		int num_buff;
		CWnd *send_wnd;
		int thread_no;
		UINT port;
	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);
}

// my CASynSocket .cpp file

IMPLEMENT_SERIAL(SockClient,CAsyncSocket,VERSIONABLE_SCHEMA | 2)
SockClient::SockClient()
 {
	sockbuffer = new char[300];
//	port = 11007;
 }

SockClient::SockClient(UINT myport)
 {
	 port = myport;
    sockbuffer = new char[300];
//	port = 11007;
 }
SockClient::~SockClient()
{
}
  void SockClient :: OnConnect(int nErrorcode)
  {

	 //   SockCLeintThread* current; 

	 if(nErrorcode == 0)
		 Sockevent.SetEvent();
	 //  SockCLeintThread* current;
	  //   (CWinThread *) current = AfxGetThread();
       UNREFERENCED_PARAMETER(nErrorcode);
          CAsyncSocket::OnConnect(nErrorcode);
       busy = 1;       
  //    if(nErrorcode == WSAETIMEDOUT)
	//	  Connect(current->ipaddr,current->port);

        AsyncSelect(FD_WRITE);
   //    main_app->pevent->SetEvent();
  }
  void SockClient::OnClose(int nErrorCode)                    // SockCLeintThread 
  {
	  SockCLeintThread* current = dynamic_cast<SockCLeintThread*>(current);
 	  current = (SockCLeintThread*)AfxGetThread();
	 //  SockCLeintThread* current;
	  //   (CWinThread *) current = AfxGetThread();
       
       current->flags.is_connected = 0; 
           
           CAsyncSocket::OnClose(nErrorCode);
  }
  void SockClient :: OnSend(int nErrorcode)
  {
	int bytes_sent;
               
      if (num_buff > 0)
	  {
         bytes_sent = Send(sockbuffer,num_buff,0);
         num_buff = num_buff - bytes_sent;
        CAsyncSocket::OnSend(nErrorcode);			
      AsyncSelect(FD_READ);
	  }
 }
   void SockClient::OnReceive(int nErrorcode)
 {
                                               // derived from CAsyncSocket

   static int i=0;

   i++;


   int nRead;
   nRead = Receive(sockbuffer, 4096);

   switch (nRead)
   {
   case 0:
      Close();
      break;
   case SOCKET_ERROR:
      if (GetLastError() != WSAEWOULDBLOCK)
      {
         AfxMessageBox ("Error occurred");
         Close();
      }
      break;
   default:
     num_buff = nRead; //terminate the string

   }
   CAsyncSocket::OnReceive(nErrorcode);
  //  UNREFERENCED_PARAMETER(nErrorcode);
     

    //  Receive(sockbuffer,300,0);

       //  main_app = (CHERC_CMDApp *)AfxGetApp();
         // Post  Event
		 

         send_wnd->SendMessage(WM_RECEIVE_SOCK_MESS,0,(LPARAM)sockbuffer);
        // strncpy(main_app->mytcpipeventptr->command_buffer,sockbuffer,30);


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

// CWinthread .h file

class SockCLeintThread : public CWinThread {
   public:
	      DECLARE_DYNCREATE(SockCLeintThread)
        //		DECLARE_SERIAL(SockCLeintThread)
       //     IMPLEMENT_DYNAMIC(SockCLientThread)
        public:
			     SockCLeintThread(UINT myport);
				 SockCLeintThread();
				~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;
				} flags;
				
protected:
	  virtual BOOL InitInstance();      // Just for starters
           DECLARE_MESSAGE_MAP()
};

// CwinThread .cpp file

IMPLEMENT_DYNCREATE(SockCLeintThread,CWinThread)
BEGIN_MESSAGE_MAP(SockCLeintThread, CWinThread)
     //{{AFX_MSG_MAP(CMyThread)
         ON_THREAD_MESSAGE(WM_SEND_SOCK_MESS,Sendit)
      //   ON_THREAD_MESSAGE(WM_SOCKET_NOTIFY,sockoper)
     // NOTE - the ClassWizard will add and remove mapping macros here.
       //  ON_THREAD_MESSAGE ( WM_MYTHREADMESSAGE, MyMessageHandler )
     //}}AFX_MSG_MAP
     END_MESSAGE_MAP()

void SockCLeintThread::Sendit(WPARAM count, LPARAM command)
{
                UNREFERENCED_PARAMETER(command);
    //  thisocket.AsyncSelect(FD_READ|FD_WRITE);
			    thisocket.num_buff = count;
				thisocket.AsyncSelect(FD_WRITE);
        //      strncpy(thisocket.sockbuffer,(char *)command,20);
				return;
}

void SockCLeintThread::Receiveit(WPARAM count, LPARAM command)
{
      UNREFERENCED_PARAMETER(count);
          UNREFERENCED_PARAMETER(command);
        thisocket.num_buff = 0;
}


SockCLeintThread::SockCLeintThread() 
{
//	thisocket = new SockClient;
//	 if (thisocket == NULL)
//			 this->m_pMainWnd->MessageBox("Socket","Socket Error",MB_ICONERROR);

//        ipaddr = "192.168.1.4";
	// thisocket(myport); 
        m_bAutoDelete = FALSE;
}
SockCLeintThread::SockCLeintThread(UINT myport) : thisocket(myport)
{
//	thisocket = new SockClient;
//	 if (thisocket == NULL)
//			 this->m_pMainWnd->MessageBox("Socket","Socket Error",MB_ICONERROR);

//        ipaddr = "192.168.1.4";
	// thisocket(myport); 
        m_bAutoDelete = FALSE;
}

SockCLeintThread::~SockCLeintThread()
{
}

BOOL SockCLeintThread::InitInstance()
   {
int error_code;
MSG m_msgCur;
extern void SetThreadName( DWORD dwThreadID, LPCSTR szThreadName);
  
           socklock.Lock();

		  

          	 	 if(thisocket.Create(thisocket.port,SOCK_STREAM,NULL)== 0)
          {
                          error_code =  GetLastError();
                   }

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

       

          
      //    WaitForSingleObject(Sockevent.m_hObject,INFINITE);

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



    return TRUE;                                                   // [11]
   }

AnswerRe: Socket connection problem Pin
Albert Holguin6-Jul-12 4:15
professionalAlbert Holguin6-Jul-12 4:15 
GeneralRe: Socket connection problem Pin
ForNow6-Jul-12 6:57
ForNow6-Jul-12 6:57 
AnswerRe: Socket connection problem Pin
fat_boy6-Jul-12 5:53
fat_boy6-Jul-12 5:53 
QuestionReading Buffer of unicode and ansi character Pin
john56326-Jul-12 0:06
john56326-Jul-12 0:06 
GeneralRe: Reading Buffer of unicode and ansi character Pin
Jochen Arndt6-Jul-12 0:22
professionalJochen Arndt6-Jul-12 0:22 
GeneralRe: Reading Buffer of unicode and ansi character Pin
john56326-Jul-12 0:39
john56326-Jul-12 0:39 
AnswerRe: Reading Buffer of unicode and ansi character Pin
Jochen Arndt6-Jul-12 0:56
professionalJochen Arndt6-Jul-12 0:56 
GeneralRe: Reading Buffer of unicode and ansi character Pin
john56326-Jul-12 1:09
john56326-Jul-12 1:09 
GeneralRe: Reading Buffer of unicode and ansi character Pin
Jochen Arndt6-Jul-12 1:18
professionalJochen Arndt6-Jul-12 1:18 
GeneralRe: Reading Buffer of unicode and ansi character Pin
john56326-Jul-12 1:31
john56326-Jul-12 1:31 
GeneralRe: Reading Buffer of unicode and ansi character Pin
Jochen Arndt6-Jul-12 1:52
professionalJochen Arndt6-Jul-12 1:52 
GeneralRe: Reading Buffer of unicode and ansi character Pin
BCN-1637-Jul-12 16:32
BCN-1637-Jul-12 16:32 
GeneralRe: Reading Buffer of unicode and ansi character Pin
Jochen Arndt7-Jul-12 22:28
professionalJochen Arndt7-Jul-12 22:28 
GeneralRe: Reading Buffer of unicode and ansi character Pin
BCN-1638-Jul-12 2:36
BCN-1638-Jul-12 2:36 
QuestionProgramming Problem Pin
Ahmed Charfeddine5-Jul-12 22:54
Ahmed Charfeddine5-Jul-12 22:54 
GeneralRe: Programming Problem Pin
«_Superman_»6-Jul-12 17:55
professional«_Superman_»6-Jul-12 17:55 
GeneralRe: Programming Problem Pin
Ahmed Charfeddine7-Jul-12 0:25
Ahmed Charfeddine7-Jul-12 0:25 

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.