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;
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
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
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, ':');
14 AfxExtractSubString(ports, ipport, 1, ':');
15
16 port = _wtoi(ports);
17
18 AfxExtractSubString(socknums, ipport, 2, ':');
19
20 AfxExtractSubString(savepath, ipport, 1, '-');
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
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;
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