Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Moak 29-Jul-10 9:54am    
Double posting, I answered in the C++ forum.....

you can use SetLastError() API to set a error number if any and GetLastError() to get your error number ?

this should help ?
 
Share this answer
 
You may post a message at your application window (need to pass the window handle as parameter to the thread).
On message reception, the main window procedure (inside the 'OnMessage' handler) will update the listbox content.
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900