CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2010
    Posts
    4

    Red face Connection fail notification to dialog in vc++ mfc

    Hey guys,
    I have a project which connects the TVs(with socket connection) to a computer and saves the log files. I achieve the connection by multithreading but I want to see whether the connection is established or not in the main dialog page that i built in VC++ MFC. Here are my codes:
    view source
    print?
    01 void CDilaraDlg::OnBnClickedConnect()
    02 {
    03 int Selected = 0; //number of the servers selected from the list
    04
    05 for(int i=0; i< m_ctlServerList.GetCount();i++)
    06 {
    07 if(m_ctlServerList.GetCheck(i) == 1 )
    08 {
    09 Selected++;
    10 }
    11 }
    12
    13 //no line is selected:
    14 if(Selected == 0)
    15 AfxMessageBox(L"No Server is Selected!");
    16
    17
    18 CString *diziPtr= new CString[Selected];
    19 char str[2];
    20 CString svar,connection;
    21 for(int i=0; i< m_ctlServerList.GetCount();i++)
    22 {
    23 if(m_ctlServerList.GetCheck(i) == 1 )
    24 {
    25 _itoa_s(i,str,10);
    26 svar = str;
    27 *diziPtr = lines[i]+_T(":")+svar+_T("-")+m_savepath;
    28 ++diziPtr;
    29 }
    30 }
    31
    32 m_fails.ResetContent();
    33 m_success.ResetContent();
    34
    35 for(int i=0;i<Selected;i++){
    36 --diziPtr;
    37 Client_Thread=CreateThread(NULL,0,ClientThread,(void *)diziPtr,0,&Client_ThreadID);
    38 //HERE I WANT TO DO SOME OPERATIONS FOR m_fails.AddString(..) or m_success.AddString(..)
    39 }
    40
    41 }

    m_fails and m_success stand for the control variables of two list boxes that i want to write down the succeed and failed connections.
    For the multithreading :
    view source
    print?
    01 DWORD WINAPI ClientThread(void * num)
    02 {
    03
    04 signal( SIGINT, &signal_handler );
    05 signal( SIGTERM,&signal_handler );
    06 signal( SIGABRT,&signal_handler );
    07
    08 CString ipport= *(CString * ) num;
    09 CString ipAddress,savepath;
    10 CString ports,socknums;
    11 int port,socknum;
    12
    13 AfxExtractSubString(ipAddress, ipport, 0, ':'); //first substr upto : is the ip
    14 AfxExtractSubString(ports, ipport, 1, ':');//second substr after : is the port
    15
    16 port = _wtoi(ports);
    17
    18 AfxExtractSubString(socknums, ipport, 2, ':');//third substr after : is the threadno
    19
    20 AfxExtractSubString(savepath, ipport, 1, '-');//second substr after , is the savepath
    21
    22
    23 socknum = _wtoi(socknums);
    24 CStringA ip(ipAddress);
    25 Socket sockClient(socknum);
    26
    27 CString thno,conn;
    28 thno.Format(_T("%d"), socknum);
    29
    30 //creating the ip_port.txt file and write the time in it
    31 sockClient.timefile(ipAddress,ports,savepath);
    32
    33 if(sockClient.ConnectToServer(ip, port,socknum)==0)
    34 return -1;
    35
    36 sockClient.RecvData(ipAddress,ports,socknum,savepath);
    37
    38 return 0;
    39
    40 }

    in the ConnectToServer function of the Socket, it returns 0 if the connection is not achieved and 1 otherwise.
    view source
    print?
    01 int Socket::ConnectToServer( const char *ipAddress, int port,int i)
    02 {
    03 myAddress.sin_family = AF_INET;
    04 myAddress.sin_addr.s_addr = inet_addr( ipAddress );
    05 myAddress.sin_port = htons( port );
    06
    07 if (connect( mySocket[i], (SOCKADDR*) &myAddress, sizeof( myAddress ) )==SOCKET_ERROR )
    08 {
    09 CString msg = _T("ClientSocket: Failed to connect: Error Value: ");
    10 CString error;
    11 int a = WSAGetLastError();
    12 error.Format(_T("%d"),a);
    13 msg+=error;
    14 AfxMessageBox(msg);
    15 system("pause");
    16 return 0; //connection fails
    17 }
    18 return 1;
    19 }

    How to get 0 or 1 return values in my dialog.cpp to do the listbox operations? or any other suggestions??
    Thanks

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Connection fail notification to dialog in vc++ mfc

    Please, edit your post adding Code tags around code snippets.
    See Announcement: Before you post....
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2010
    Posts
    4

    Re: Connection fail notification to dialog in vc++ mfc

    Hey guys,
    I have a project which connects the TVs(with socket connection) to a computer and saves the log files. I achieve the connection by multithreading but I want to see whether the connection is established or not in the main dialog page that i built in VC++ MFC. Here are my codes:
    view source
    print?
    Code:
    void CDilaraDlg::OnBnClickedConnect()
    {
    int Selected = 0; //number of the servers selected from the list
    
    for(int i=0; i< m_ctlServerList.GetCount();i++)
    {
    if(m_ctlServerList.GetCheck(i) == 1 )
    {
    Selected++;
     }
     }
    
     //no line is selected:
     if(Selected == 0)
     AfxMessageBox(L"No Server is Selected!");
    
    
     CString *diziPtr= new CString[Selected];
    char str[2];
     CString svar,connection;
     for(int i=0; i< m_ctlServerList.GetCount();i++)
     {
     if(m_ctlServerList.GetCheck(i) == 1 )
     {
     _itoa_s(i,str,10);
     svar = str;
     *diziPtr = lines[i]+_T(":")+svar+_T("-")+m_savepath;
     ++diziPtr;
     }
     }
    
     m_fails.ResetContent();
     m_success.ResetContent();
    
     for(int i=0;i<Selected;i++){
     --diziPtr;
     Client_Thread=CreateThread(NULL,0,ClientThread,(void *)diziPtr,0,&Client_ThreadID);
     //HERE I WANT TO DO SOME OPERATIONS FOR m_fails.AddString(..) or m_success.AddString(..)
     }
    
    }
    m_fails and m_success stand for the control variables of two list boxes that i want to write down the succeed and failed connections.
    For the multithreading :
    Code:
    DWORD WINAPI ClientThread(void * num)
     {
    
     signal( SIGINT, &signal_handler );
     signal( SIGTERM,&signal_handler );
     signal( SIGABRT,&signal_handler );
    
     CString ipport= *(CString * ) num;
     CString ipAddress,savepath;
     CString ports,socknums;
     int port,socknum;
    
     AfxExtractSubString(ipAddress, ipport, 0, ':'); //first substr upto : is the ip
     AfxExtractSubString(ports, ipport, 1, ':');//second substr after : is the port
    
     port = _wtoi(ports);
    
     AfxExtractSubString(socknums, ipport, 2, ':');//third substr after : is the threadno
    
     AfxExtractSubString(savepath, ipport, 1, '-');//second substr after , is the savepath
    
    
     socknum = _wtoi(socknums);
     CStringA ip(ipAddress);
     Socket sockClient(socknum);
    
     CString thno,conn;
     thno.Format(_T("&#37;d"), socknum);
    
     //creating the ip_port.txt file and write the time in it
     sockClient.timefile(ipAddress,ports,savepath);
    
    if(sockClient.ConnectToServer(ip, port,socknum)==0)
     return -1;
    
     sockClient.RecvData(ipAddress,ports,socknum,savepath);
    
     return 0;
    
    }
    in the ConnectToServer function of the Socket, it returns 0 if the connection is not achieved and 1 otherwise.
    Code:
    int Socket::ConnectToServer( const char *ipAddress, int port,int i)
     {
     myAddress.sin_family = AF_INET;
     myAddress.sin_addr.s_addr = inet_addr( ipAddress );
     myAddress.sin_port = htons( port );
    
     if (connect( mySocket[i], (SOCKADDR*) &myAddress, sizeof( myAddress ) )==SOCKET_ERROR )
     {
     CString msg = _T("ClientSocket: Failed to connect: Error Value: ");
     CString error;
     int a = WSAGetLastError();
     error.Format(_T("%d"),a);
     msg+=error;
     AfxMessageBox(msg);
     system("pause");
     return 0; //connection fails
     }
    return 1;
     }
    How to get 0 or 1 return values in my dialog.cpp to do the listbox operations? or any other suggestions??
    Thanks

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Connection fail notification to dialog in vc++ mfc

    Is the "ignoring indentations in the code" your credo?
    Have a look at what you've posted: can you read understand this code?
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured