Click here to Skip to main content
15,887,676 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionWAVEINCAPS help... Pin
AmbiguousName30-Jul-10 7:52
AmbiguousName30-Jul-10 7:52 
QuestionCMDIChildWndEx with its own menu Pin
SaintNeil30-Jul-10 5:17
SaintNeil30-Jul-10 5:17 
AnswerRe: CMDIChildWndEx with its own menu Pin
Richard MacCutchan30-Jul-10 6:35
mveRichard MacCutchan30-Jul-10 6:35 
AnswerRe: CMDIChildWndEx with its own menu Pin
Eugen Podsypalnikov30-Jul-10 11:02
Eugen Podsypalnikov30-Jul-10 11:02 
QuestionDisconnect from server in vc++ Pin
dilara semerci30-Jul-10 3:45
dilara semerci30-Jul-10 3:45 
AnswerRe: Disconnect from server in vc++ Pin
Moak30-Jul-10 4:21
Moak30-Jul-10 4:21 
AnswerRe: Disconnect from server in vc++ Pin
Cool_Dev30-Jul-10 4:33
Cool_Dev30-Jul-10 4:33 
QuestionRe: Disconnect from server in vc++ Pin
dilara semerci2-Aug-10 4:38
dilara semerci2-Aug-10 4:38 
what if i want to terminate some specified threads? all i want to do is this: i am creating threads for clients connect to servers by createthread function; i call this function in my onclickconnect() button call, for each selected server in the listbox, i create a thread and do the operations; startconnection, receive message from server etc. I pass the connected ip-ports in some other list and I want to have a disconnect button. when i select ip-ports in the "successful connections" list and click on the button, i want the corresponding threads to terminate. I have done some coding and this works fine for several steps. When I try to connect the server, it connects. When I try to disconnect from the server the thread is terminated. HOWEVER when I try to connect to the disconnected server it gives an error and my program terminates. I dont want such things. If you can examine my code and give me some advice, I will be grateful. Thanks
Here is my connect/disconnect button:
if(m_con1dis2 == 1) //connect 
	{
		m_fails.ResetContent();
		m_success.ResetContent();
		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]; 
		CString *ptr= new CString[Selected]; 
		char str[2];
		CString svar;
		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;	
			}
		}
		for(int i=0;i<Selected;i++)
		{  
			--diziPtr; 	
			Client_Thread[Selected-i-1]=CreateThread(NULL,0,ClientThread,(void *)diziPtr,0,&Client_ThreadID);	
		} 

		while(done==true)
		{
		}
	

		for(int i=0; i< m_ctlServerList.GetCount();i++)
		{
			if(m_ctlServerList.GetCheck(i) == 1 ) 
			{
				_itoa_s(i,str,10);
				svar = str;
				*ptr = lines[i]+_T(":")+svar;
								
				if(connected[i] == 2)
				{
					m_success.AddString(*ptr);
				
				}
				else if(connected[i] == 1)
				{
					m_fails.AddString(*ptr);
				
				}
				++ptr;	
				UpdateData(false);	
			}
		}
		thread = 0;
		done = true;
	
	}//end of connect button

if(m_con1dis2 == 2) //disconnect 
	{
		CString strTxt,thnum;
		int tnum;
		toDisconnect = 0;
		for(int i=0; i< m_success.GetCount(); i++)
		{
			if(m_success.GetSel(i) == 1) 
			{
				toDisconnect++;
				m_success.GetText(i,strTxt);
				MessageBox(strTxt);
				AfxExtractSubString(thnum, strTxt, 2, ':');//third substr after : is the threadno
			    tnum = _wtoi(thnum); //CString to int
				TerminateThread(Client_Thread[tnum],0);
				CloseHandle(Client_Thread[tnum]);
				AfxMessageBox(L"You terminated thread!");
				//system("PAUSE");
				//exit(8);
				
			}
		}
		for(int i=0; i< m_success.GetCount(); i++) //disconnect 
		{
			if(m_success.GetSel(i) == 1) 
			{
				m_success.DeleteString(i);
				m_fails.AddString(strTxt);
			}
		}
		

		UpdateData(false);
		if(m_success.GetCount() == 0)
		{	m_ctlConnect.SetWindowTextW(_T("Connect"));
			m_con1dis2 = 1;
		}


	}//end of disconnect button
}


here is my clientthread function:
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);//CString to int

	AfxExtractSubString(socknums, ipport, 2, ':');//third substr after : is the threadno

	AfxExtractSubString(savepath, ipport, 1, '-');//second substr after - is the savepath

    socknum = _wtoi(socknums); //CString to int
	CStringA ip(ipAddress); //CString to const char *
    Socket sockClient(socknum);

	if(sockClient.ConnectToServer(ip, port,socknum)== 0) //connection failed
	{	connected[socknum] = 1;
		thread++ ; 
		
	}
	else
	{
		connected[socknum] = 2;
		thread++ ; 
		sockClient.timefile(ipAddress,ports,savepath);//creating the ip_port.txt file and write the time in it
		sockClient.RecvData(ipAddress,ports,socknum,savepath);	
						
	}
	if(thread == Selected) //Selected and thread are global int
		{
			done = false;
		}
		
	return 0;
   
} 


the index of the Client_Thread is the threadno given in the creation.
AnswerRe: Disconnect from server in vc++ Pin
Cool_Dev2-Aug-10 18:01
Cool_Dev2-Aug-10 18:01 
QuestionC4309 warning Pin
Rakesh530-Jul-10 3:45
Rakesh530-Jul-10 3:45 
AnswerRe: C4309 warning Pin
Cedric Moonen30-Jul-10 3:52
Cedric Moonen30-Jul-10 3:52 
GeneralRe: C4309 warning Pin
Rakesh530-Jul-10 4:10
Rakesh530-Jul-10 4:10 
GeneralRe: C4309 warning Pin
Chris Losinger30-Jul-10 4:12
professionalChris Losinger30-Jul-10 4:12 
GeneralRe: C4309 warning Pin
Rakesh530-Jul-10 4:18
Rakesh530-Jul-10 4:18 
GeneralRe: C4309 warning Pin
Cedric Moonen30-Jul-10 4:24
Cedric Moonen30-Jul-10 4:24 
GeneralRe: C4309 warning Pin
Chris Losinger30-Jul-10 4:25
professionalChris Losinger30-Jul-10 4:25 
QuestionHow reduce build time of the project? Pin
ganesh.dp30-Jul-10 3:04
ganesh.dp30-Jul-10 3:04 
AnswerRe: How reduce build time of the project? Pin
Sauro Viti30-Jul-10 3:19
professionalSauro Viti30-Jul-10 3:19 
AnswerRe: How reduce build time of the project? Pin
Maximilien30-Jul-10 4:46
Maximilien30-Jul-10 4:46 
AnswerRe: How reduce build time of the project? Pin
Abhi Lahare30-Jul-10 8:15
Abhi Lahare30-Jul-10 8:15 
AnswerRe: How reduce build time of the project? Pin
Aescleal30-Jul-10 20:49
Aescleal30-Jul-10 20:49 
QuestionWriteFile returning zero and GetLastError returning 997(ERROR_IO_PENDING) Pin
learningvisualc30-Jul-10 2:59
learningvisualc30-Jul-10 2:59 
AnswerRe: WriteFile returning zero and GetLastError returning 997(ERROR_IO_PENDING) Pin
Cool_Dev30-Jul-10 3:37
Cool_Dev30-Jul-10 3:37 
AnswerRe: WriteFile returning zero and GetLastError returning 997(ERROR_IO_PENDING) Pin
Xequen30-Jul-10 4:09
Xequen30-Jul-10 4:09 
QuestionQuestion Authentication client-server using message queues in c language Pin
ralph 230-Jul-10 2:47
ralph 230-Jul-10 2:47 

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.