المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Socket connection problem



C++ Programming
07-06-2012, 06:22 PM
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

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

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 fileIMPLEMENT_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(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 fileclass 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 fileIMPLEMENT_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] }