www.pudn.com > VOIP_server_client.rar > peer1_main.cpp


#include  
#include  
#include  
#include  
#include  
#include  // include important C/C++ stuff 
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
 
 
#include "peer1_main.h" 
#include "peer1_resource.h" 
#include "dsound.h" 
#include "peer1_sound_cap.h" 
#include "peer1_sock.h" 
#include "peer1_sound_play.h" 
 
#include      /* _threadid variable */ 
#include     /* _beginthread, _endthread */ 
#include  
 
#pragma hdrstop 
#pragma argsused 
// Function Prototypes ========================================================= 
BOOL CALLBACK DialogFunc(HWND, UINT, WPARAM, LPARAM); 
 
// defines for windows ========================================================= 
#define WINDOW_CLASS_NAME "WINCLASS1"  // class name 
#define WINDOW_WIDTH  400              // size of window 
#define WINDOW_HEIGHT 300 
 
 
// GLOBAL VARIABLES ============================================================ 
HWND main_window_handle = NULL; 
HINSTANCE main_instance = NULL; 
HWND hdDlg;          // Global Dialog Box Handle 
WPARAM wParam_Dlg;  // Global one for Dialog Box 
LPARAM lParam_Dlg;  // Global one for Dialog Box 
int share_array[10]={0,0,0,0,0,0,0,0,0,0};       // Marker array 
int share_array_rcv[PLAY_BUF_LEN];   // Marker array 
 
BOOL thread1_stop = FALSE;                       // thread stop mark 
BOOL thread2_stop = FALSE; 
BOOL thread3_stop = FALSE;                       // thread stop mark 
BOOL thread4_stop = FALSE; 
 
struct cap_buffer{ 
                   char sound_data[CAP_LENGTH]; 
                 }; 
struct cap_buffer my_cap_buffer[10];          //10 capture sound store buffers 
 
struct play_buffer{ 
                   char play_data[PLAY_LENGTH]; 
                   }; 
struct play_buffer my_play_buffer[PLAY_BUF_LEN];      // 10 Play store buffers 
 
 
int send_only = 0; 
int rec_only = 0; 
int both = 0; 
 
char   gszMsg [255]; /*Get the Text field entry*/ 
char   remote_ip[30]; 
char st01[100]; 
float num_of_played; 
float  total_rcv_time, new_rcv_time, old_rcv_time, avg_delay, max_delay, min_delay; 
int err_restart = 0; 
 
//External Global Variables ==================================================== 
extern HANDLE g_hNotificationEvent_cap; 
extern HANDLE              g_hNotificationEvent_play; 
extern SOCKET s_rcv; 
extern float num_of_trans; 
extern float late_arrive_skip, num_of_rcv, num_of_mRecent, too_long_skip; 
 
 
// Temp Global Variables ======================================================= 
float clk_1;             // Clock when generating captured data 
float avg_time=0;        // Average clock 
extern int send_num;     // number of sending 
 
int play_num=0; 
float total_time_play=0; 
float avg_time_play; 
float TS_cap; 
 
 
//----------------------------------------------------------------------------- 
//********************** thread_code1( Capture) ******************************* 
//----------------------------------------------------------------------------- 
void thread_code1(void *threadno) 
{ 
  //-----temp variables ------ 
  char st01[100]; 
  //-------------------------- 
 
  HWND hwndDlg = hdDlg; 
  int p=1;                        // Keep the thread running 
  DWORD dwResult; 
  int i=0;                        //Counter for my_cap_buffer[i].sound_data 
  //Display("thread_code1 start\r\n"); 
  Start_Record(hwndDlg);          // Start capture looping 
 
  while(p!=0) // While-1 
  { 
  if (thread1_stop == TRUE) 
  { 
  //Display("thread_code1 stoped.\r\n"); 
  _endthread(); 
  } 
 
   while(i!=10)  // while-2 
   { 
     ///// TimeStamp //////// 
     TS_cap = clock(); 
     sprintf(st01, "%.1f\r\n", TS_cap); 
     //Display(st01); 
     /////////////////////// 
 
     dwResult=WaitForSingleObject(g_hNotificationEvent_cap,INFINITE); 
     if(thread1_stop == TRUE) 
      { 
        //Display("thread_code1 stoped.\r\n"); 
        _endthread(); 
      }// end if 
       switch(dwResult) 
        { 
                    case WAIT_OBJECT_0 + 0: 
                           clk_1 = clock(); 
                           Save_Record(hwndDlg,my_cap_buffer[i].sound_data); 
                           break; 
                    default: 
                           char st20[30]; 
                           sprintf(st20, "event %d ", dwResult); 
                           Display(st20); 
                           Get_Current_Position(hwndDlg); 
                           break; 
        }// end switch 
        i++; 
        if(i==10) 
        i=0; 
     }  //end while-2 
  }//end while-1 
 
}// end function 
 
//----------------------------------------------------------------------------- 
//********************** thread_code2 (Send_sock) ***************************** 
//----------------------------------------------------------------------------- 
void thread_code2(void *threadno) 
{ 
  int p = 1;               // Keep thread running 
  int i = 0;               // Counter for  share_array[i] 
  HWND hwndDlg = hdDlg; 
 
  wsa_start(hwndDlg, err_restart); 
  Connect_snd(hwndDlg, wParam_Dlg, lParam_Dlg, remote_ip);   //Create send socket 
  EnableWindow(GetDlgItem(hwndDlg, IDC_START), FALSE);         // Disable start button 
 
  while (p!=0) 
  { 
     if (thread2_stop == TRUE) 
      { 
      _endthread(); 
      } 
 
     if (share_array[i]==1) 
     { 
      send_file(hwndDlg,my_cap_buffer[i].sound_data); 
      share_array[i]=0; 
      i++; 
      if (i==10) 
      i=0; 
     } 
  } 
 
} 
 
//----------------------------------------------------------------------------- 
//********************** thread_code3 (rec_sock) ****************************** 
//----------------------------------------------------------------------------- 
void thread_code3(void *threadno) 
{ 
  HWND hwndDlg = hdDlg; 
  wsa_start(hwndDlg, err_restart); 
  Connect_rcv(hwndDlg, wParam_Dlg, lParam_Dlg); 
  if(thread3_stop==TRUE) 
  { 
    _endthread(); 
  } 
 
} 
 
//----------------------------------------------------------------------------- 
//********************** thread_code4 (Play) ********************************** 
//----------------------------------------------------------------------------- 
void thread_code4(void *threadno) 
{ 
  float this_clk; 
  DWORD dwResult; 
  DWORD dwOffset, dwBytes; 
  int j=0;             // Counter for Marker array and Play store buffers 
  int p = 1;           // Keep thread running 
  HWND hwndDlg = hdDlg; 
 
 for(int n=0; n<1; n++) 
 { 
  while(share_array_rcv[j]!=1) 
  { 
    Sleep(5); 
  } 
  if((Copy_Play_Data(my_play_buffer[j].play_data,hwndDlg))!=-1) 
  { 
   share_array_rcv[j]=0; 
   dwOffset = j* PLAY_LENGTH; 
   dwBytes = PLAY_LENGTH; 
   fill_data(hwndDlg,dwOffset, dwBytes); 
 
   num_of_played++; 
   old_rcv_time = clock(); 
   j++; 
  } 
 } 
  Play_Sound(hwndDlg); 
 
  while (p!= 0) 
  { 
  if (thread4_stop == TRUE) 
  { 
   _endthread(); 
  } 
 
          dwResult=WaitForSingleObject(g_hNotificationEvent_play,INFINITE); 
               while(share_array_rcv[j]!=1) 
                   { 
                     if (thread4_stop == TRUE) 
                       { 
                       _endthread(); 
                       } 
                     Sleep(1); 
                   } 
                    
               switch(dwResult) 
                { 
                 case WAIT_OBJECT_0 + 0: 
                  if((Copy_Play_Data(my_play_buffer[j].play_data,hwndDlg))!=-1) 
                    { 
                      play_num++; 
                      share_array_rcv[j]=0; 
 
                      dwOffset = (j%10)* PLAY_LENGTH; 
                      dwBytes = PLAY_LENGTH; 
                      fill_data(hwndDlg,dwOffset, dwBytes); 
 
                       ///////// End-End Delay /////////// 
                      new_rcv_time = clock(); 
                      total_rcv_time = total_rcv_time + (new_rcv_time-old_rcv_time); 
 
                      if((new_rcv_time-old_rcv_time)> max_delay) 
                      max_delay = new_rcv_time-old_rcv_time; 
                      if((new_rcv_time-old_rcv_time)< min_delay) 
                      min_delay = new_rcv_time-old_rcv_time; 
 
                      avg_delay = total_rcv_time/num_of_played; 
 
                      old_rcv_time = new_rcv_time; 
                      ///////////////////////////////////// 
 
                      j++; 
                      num_of_played++; 
                      if (j==PLAY_BUF_LEN) 
                      j=0; 
                     } 
                     break; 
 
                    default: 
                           char st20[30]; 
                           sprintf(st20, "event %d ", dwResult); 
                           Display(st20); 
                           break; 
                  } 
  } 
} 
 
 
 
 
 
//------------------------------------------------------------------------------ 
//**********************  WindowProc ******************************************* 
//------------------------------------------------------------------------------ 
 
LRESULT CALLBACK WindowProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam) 
{ 
// this is the main message handler of the system 
PAINTSTRUCT	ps;		   // used in WM_PAINT 
HDC		hdc;	           // handle to a device context 
 
// what is the message 
switch(msg) 
	{	 
        case WM_COMMAND: 
          switch(LOWORD(wparam)) 
          { 
           case IDM_DIALOG: 
              DialogBox(main_instance, "MyDB", hwnd, (DLGPROC) DialogFunc); 
              break; 
          } 
	case WM_CREATE: 
        { 
		// do initialization stuff here 
		return(0); 
		} //break; 
 
 
     case WM_PAINT: 
         { 
         // start painting 
         hdc=BeginPaint(hwnd,&ps); 
         SetTextColor(hdc, RGB(0,0,255)); 
         SetBkColor(hdc, RGB(0,0,0)); 
         SetBkMode(hdc, OPAQUE); 
 
         char str1[30]; 
         sprintf(str1,"Peer1"); 
         TextOut(hdc, 50, 50, str1, strlen(str1)); 
 
         // end painting 
         EndPaint(hwnd,&ps); 
         return(0); 
        } //break; 
 
 
 
	case WM_DESTROY: 
		{ 
		// kill the application 
		PostQuitMessage(0); 
		return(0); 
		} //break; 
 
	default:break; 
 
    } // end switch 
 
// process any messages that we didn't take care of  
return (DefWindowProc(hwnd, msg, wparam, lparam)); 
 
} // end WinProc 
 
//------------------------------------------------------------------------------ 
//************************** DialogFunc **************************************** 
//------------------------------------------------------------------------------ 
 
BOOL CALLBACK DialogFunc(HWND hwndDlg,UINT message, 
                         WPARAM wParam, LPARAM lParam) 
{ 
    hdDlg = hwndDlg; 
    wParam_Dlg = wParam; 
    lParam_Dlg = lParam; 
 
    switch(message) 
    { 
        case WM_INITDIALOG: 
            EnableWindow(GetDlgItem(hwndDlg, IDC_STOP), FALSE);// Disenable STOP button 
            EnableWindow(GetDlgItem(hwndDlg, IDC_START), FALSE); // Disable START button 
            EnableWindow(GetDlgItem(hwndDlg, IDC_SEND_TEXT), FALSE);// Disable send text button 
            SendDlgItemMessage(hwndDlg,IDC_RB_BOTH,BM_SETCHECK,BST_CHECKED,0); 
 
            FILE *tmp2;           // Get and display old ip 
            char old_ip[30]; 
            int read; 
            if ((tmp2 = fopen("c:\\voip_remote_ip","rb")) == NULL) 
            { 
            break; 
            } 
            read=fread(old_ip, sizeof(char), sizeof(old_ip), tmp2); 
            fclose(tmp2); 
 
            if ((tmp2 = fopen("c:\\voip_remote_ip","rb")) == NULL) 
                           { 
                             break; 
                           } 
 
                         memset(old_ip,0, sizeof(old_ip)); 
                         fread(old_ip, sizeof(char), read, tmp2); 
                         fclose(tmp2); 
 
            SetDlgItemText( hwndDlg, IDC_IP, TEXT(old_ip) ); 
 
            return 1; 
 
 
        case WM_COMMAND: 
           switch(LOWORD(wParam)) 
           { 
             case IDCANCEL: 
                { 
                EndDialog(hwndDlg,0); 
		return 1; 
                } 
 
             case IDC_R_STOP: 
                  { 
 
                  err_restart = 0; 
                  comm_stop(hwndDlg); 
                  Display("---- Stopped by remote site ----\r\n"); 
 
 
                return 1; 
                } 
 
             case IDC_ERR_STOP: 
                  { 
                 Send_Control(hwndDlg, 0,0,2,0);   ////////// Send ERROR Ask_Discon 
                 err_restart = 1; 
                 comm_stop(hwndDlg); 
                  Display("---- Stopped by error ----\r\n"); 
 
                return 1; 
                } 
 
             case IDC_R_ERR_STOP: 
                  { 
                 err_restart = 1; 
                 comm_stop(hwndDlg); 
                  Display("---- Stopped by remote site error ----\r\n"); 
 
                return 1; 
                } 
 
 
             case IDC_STOP: 
                  { 
                 Send_Control(hwndDlg, 0,0,1,0);   ////////// Send Ask_Discon 
                 err_restart = 0; 
                 comm_stop(hwndDlg); 
                  Display("---- Stopped ----\r\n"); 
 
 
                return 1; 
                } 
 
             case IDC_ERR_START: 
                 { 
                   err_restart = 1; 
                Display("---- Please wait. Trying to reconnect... ----\r\n"); 
                Send_Control(hwndDlg, 2,0,0,0); ///////// send  (due to error) 
                return 1; 
                } 
 
             case IDC_L_ERR_START: 
                 { 
                   err_restart = 1; 
                  comm_start(hwndDlg); 
                Display("### Reconnected. You can start talk now. ###\r\n"); 
                return 1; 
                } 
 
              case IDC_R_ERR_START: 
                 { 
                err_restart = 1; 
                comm_start(hwndDlg); 
                Display("### Reconnected (due to remote site error) ###\r\n"); 
                return 1; 
                } 
 
             case IDC_R_START: 
                 { 
                   num_of_played = 0; 
                   total_rcv_time = 0; 
                   max_delay = 0; 
                   min_delay = 10000; 
                   err_restart = 0; 
 
 
                comm_start(hwndDlg); 
                Display("#### Started by remote site####\r\n"); 
 
 
                return 1; 
                } 
 
              case IDC_START: 
                { 
                 Send_Control(hwndDlg, 1,0,0,0); ///////// send  
                 return 1; 
                 } 
 
 
             case  IDC_L_START: 
                 { 
                   num_of_played = 0; 
                   total_rcv_time = 0; 
                   max_delay = 0; 
                   min_delay = 10000; 
                   err_restart = 0; 
 
 
                comm_start(hwndDlg); 
                Display("#### Started ####\r\n"); 
 
 
                return 1; 
                } 
 
             case IDC_SEND_TEXT: 
                  { 
                   if ( !GetDlgItemText(hwndDlg, IDC_TEXT, 
                                                gszMsg, sizeof(gszMsg) )) 
 
                                                { 
                                                MessageBox(hwndDlg, 
                                                    "Please enter some text.", 
                                                    "TEXT ERR", MB_OK); 
                                                break; 
                                                } 
                   else 
                   { 
                    Send_Msg(hwndDlg, gszMsg); 
                    } 
                   return (1); 
                   } 
 
             case IDC_TRY: 
                   { 
                    //***Debugging-1 Display contents of share_array_rcv buffer 
                    //Uncomment the following lines to enable Debugging-1 
                    /*for (int p = 0; p < PLAY_BUF_LEN; p++) 
                    { 
                     sprintf(st01, "%d", share_array_rcv[p]); 
                     pr1(hwndDlg, st01); 
                    } 
                    pr1(hwndDlg, "\r\n"); */ 
                    return (1); 
                    } 
 
             case IDC_CLEAR: 
                   { 
                     SetDlgItemText(hwndDlg, IDC_EDIT1, " "); 
                     return (1); 
                     } 
 
             case IDC_INIT: 
                 { 
                   err_restart = 0; 
                   if ( !GetDlgItemText(hwndDlg, IDC_IP, 
                           remote_ip, sizeof(remote_ip) )) 
 
                                                { 
                                                MessageBox(hwndDlg, 
                                                    "Please enter remote IP.", 
                                                    "IP ERR", MB_OK); 
                                                break; 
                                                } 
 
                  FILE *tmp1;          // Save old ip 
                 if ((tmp1 = fopen("c:\\voip_remote_ip","wb")) == NULL) 
                         { 
                         Display("Error opening the file\r\n\r\n" ); 
                         break; 
                           } 
                 if(fwrite(remote_ip, sizeof(char),strlen(remote_ip), tmp1)== NULL) 
                     { 
                      Display("Saving ip file failed!\r\n"); 
                      } 
                 fclose(tmp1); 
 
                  wsa_start(hwndDlg, err_restart); 
                  Connect_text(hdDlg, wParam_Dlg, lParam_Dlg, remote_ip); 
                  EnableWindow(GetDlgItem(hwndDlg, IDC_START), TRUE); 
                  EnableWindow(GetDlgItem(hwndDlg, IDC_SEND_TEXT), TRUE); 
        Display("--- Init OK! Text can be sent now or press Start to begin talking. ---\r\n"); 
                  return 1; 
 
                 } 
           } 
           case SM_ASYNC: 
                   { 
                   HandleAsyncMsg(hwndDlg, wParam, lParam); 
                    return 1; 
                    } 
 
           case SM_ASYNC_TEXT: 
                   { 
                   HandleAsyncMsg_Text(hwndDlg, wParam, lParam); 
                    return 1; 
                    } 
     
    } 
   return 0; 
} 
 
//------------------------------------------------------------------------------ 
//*************************** WinMain ****************************************** 
//------------------------------------------------------------------------------ 
int WINAPI WinMain( HINSTANCE hinstance,HINSTANCE hprevinstance, 
                    LPSTR lpcmdline,int ncmdshow) 
{ 
WNDCLASS      winclass;	          // this will hold the class we create 
HWND	      hwnd;		  // generic window handle 
MSG	      msg;		  // generic message 
HDC           hdc;                // generic dc 
PAINTSTRUCT   ps;                 // generic paintstruct 
 
// first fill in the window class stucture 
winclass.style		= CS_DBLCLKS | CS_OWNDC |CS_HREDRAW | CS_VREDRAW; 
winclass.lpfnWndProc	= WindowProc; 
winclass.cbClsExtra	= 0; 
winclass.cbWndExtra	= 0; 
winclass.hInstance	= hinstance; 
winclass.hIcon		= LoadIcon(NULL, IDI_APPLICATION); 
winclass.hCursor	= LoadCursor(NULL, IDC_ARROW); 
winclass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH); 
winclass.lpszMenuName	= "MyMenu";  
winclass.lpszClassName	= WINDOW_CLASS_NAME; 
 
// register the window class 
if (!RegisterClass(&winclass)) 
	return(0); 
 
// create the window, note the use of WS_POPUP 
if (FAILED(hwnd = CreateWindow(WINDOW_CLASS_NAME,             // class 
                          "VoIP-Peer1",	         // title 
                          WS_OVERLAPPEDWINDOW | WS_VISIBLE, 
                          100,100,	                 // x,y 
                          WINDOW_WIDTH,                  // width 
                          WINDOW_HEIGHT,                 // height 
                          NULL,	                         // handle to parent 
                          NULL,	                         // handle to menu 
                          hinstance,                     // instance 
                          NULL)))	                 // creation parms 
return(0); 
 
main_window_handle = hwnd; 
main_instance = hinstance; 
 
InitCommonControls(); 
 
//enter main event loop 
while(1) 
	{ 
          if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
             { 
                //Test if this is a quit 
                if(msg.message==WM_QUIT) 
                  break; 
		// translate any accelerator keys 
		TranslateMessage(&msg); 
 
		// send the message to the window proc 
		DispatchMessage(&msg); 
		}//end if 
             }// end while 
 
  // return to Windows like this 
return(msg.wParam); 
 
} // end WinMain 
 
 
//------------------------------------------------------------------------------ 
//********************* Comm_start ********************************************* 
//------------------------------------------------------------------------------ 
void comm_start(HWND hwndDlg) 
{ 
 
 if(SendDlgItemMessage(hwndDlg, IDC_RB_SEND, BM_GETCHECK, 
                                         0, 0) == BST_CHECKED) 
                   send_only = 1; 
                   if(SendDlgItemMessage(hwndDlg, IDC_RB_REC, BM_GETCHECK, 
                                         0, 0) == BST_CHECKED) 
                   rec_only = 1; 
                   if(SendDlgItemMessage(hwndDlg, IDC_RB_BOTH, BM_GETCHECK, 
                                         0, 0) == BST_CHECKED) 
                   both = 1; 
 
                EnableWindow(GetDlgItem(hwndDlg, IDC_START), FALSE); 
                EnableWindow(GetDlgItem(hwndDlg, IDC_STOP), TRUE); 
                EnableWindow(GetDlgItem(hwndDlg, IDCANCEL), FALSE); 
                EnableWindow(GetDlgItem(hwndDlg, IDC_INIT), FALSE); 
 
                for (int i=0; i < PLAY_BUF_LEN; i++) 
                  { 
                  share_array[i%10]= 0; 
                  share_array_rcv[i]= 0; 
                  } 
 
 
                if(both == 1)             //********* Both ******************* 
                { 
                // Init play DS 
                Play_Sound_Init(hwndDlg,main_window_handle); 
                // Init Cap DS 
                InitCapture(hwndDlg); 
                CreateCaptureBuffer(hwndDlg); 
                InitNotification(hwndDlg); 
                } 
                if (send_only == 1)       //********** Send Only************* 
                { 
                 InitCapture(hwndDlg); 
                CreateCaptureBuffer(hwndDlg); 
                InitNotification(hwndDlg); 
                } 
                if (rec_only == 1)        //************ Receive Only ******** 
                { 
                  Play_Sound_Init(hwndDlg,main_window_handle); 
                } 
 
                if (both == 1)           // ************ Both *************** 
                { 
                if (_beginthread(thread_code1,4096,(void*)1) 
                                                       ==(unsigned long)-1) 
                Display("thread_code1 error\r\n"); 
                if ( _beginthread(thread_code2,4096,(void*)2) 
                                                       ==(unsigned long)-1) 
                Display("thread_code2 error\r\n"); 
                if (_beginthread(thread_code3,4096,(void*)3) 
                                                       ==(unsigned long)-1) 
                Display("thread_code3 error\r\n"); 
                if ( _beginthread(thread_code4,4096,(void*)4) 
                                                       ==(unsigned long)-1) 
                Display("thread_code4 error\r\n"); 
                } 
                if (send_only ==1)             // ********* send_only ********* 
                { 
                  if (_beginthread(thread_code1,4096,(void*)1) 
                                                       ==(unsigned long)-1) 
                Display("thread_code1 error\r\n"); 
                if ( _beginthread(thread_code2,4096,(void*)2) 
                                                       ==(unsigned long)-1) 
                Display("thread_code2 error\r\n"); 
 
 
                } 
                if(rec_only == 1)              // ******* Receive_only ********* 
                { 
                 if (_beginthread(thread_code3,4096,(void*)3) 
                                                       ==(unsigned long)-1) 
                Display("thread_code3 error\r\n"); 
                //Sleep(100); 
                if ( _beginthread(thread_code4,4096,(void*)4) 
                                                       ==(unsigned long)-1) 
                Display("thread_code4 error\r\n"); 
                } 
 } 
 
//------------------------------------------------------------------------------ 
//*********************** comm_stop ******************************************** 
//------------------------------------------------------------------------------ 
void comm_stop(HWND hwndDlg) 
{ 
                 if(both==1) 
                 { 
 
                 thread2_stop = TRUE; 
                 Sleep(10); 
                 thread3_stop = TRUE; 
                 Close_all_socket(); 
                 Sleep(10); 
                 thread1_stop=TRUE; 
                 Sleep(10); 
                 Stop_Record(hwndDlg); 
                 FreeDirectSound_cap(); 
 
                 Sound_stop(hwndDlg); 
                 Sleep(10); 
                 thread4_stop=TRUE; 
                 Sound_Shutdown(hwndDlg); 
                  } 
                  if ( send_only ==1 ) 
                  { 
 
                    thread2_stop = TRUE; 
                    Close_all_socket(); 
                    Sleep(10); 
                    thread1_stop=TRUE; 
                    Sleep(10); 
                    Stop_Record(hwndDlg); 
                    FreeDirectSound_cap(); 
                  } 
                  if ( rec_only == 1) 
                  { 
 
                    thread3_stop = TRUE; 
                    Close_all_socket(); 
                    Sleep(10); 
                    Sound_stop(hwndDlg); 
                    thread4_stop=TRUE; 
                    Sleep(10); 
                    Sound_Shutdown(hwndDlg); 
                  } 
 
                  //*** Debugging-2 Display "Delay" and "Packet Drop" related parameters 
                  // Uncomment the following lines to enable Debugging-2 
                  /*if (err_restart == 0) 
                  { 
                  sprintf(st01, "\r\n<<< num_of_trans = %.0f >>>\r\n", num_of_trans); 
                  Display(st01); 
                  sprintf(st01, "\r\n<<< num_of_rcv=%.0f >>>\r\n", num_of_rcv); 
                  Display(st01); 
                  sprintf(st01, "\r\n<<< min_delay=%f >>>\r\n", min_delay); 
                  Display(st01); 
                  sprintf(st01, "\r\n<<< max_delay=%f >>>\r\n", max_delay); 
                  Display(st01); 
                  sprintf(st01, "\r\n<<< avg_delay=%f >>>\r\n", avg_delay); 
                  Display(st01); 
                  sprintf(st01, "\r\n<<< too_long_skip=%.0f  (%.2f%) >>>\r\n", too_long_skip, (too_long_skip/num_of_rcv)*100); 
                  Display(st01); 
                  sprintf(st01, "\r\n<<< late_arrive_skip=%.0f  (%.2f%) >>>\r\n", late_arrive_skip, (late_arrive_skip/num_of_rcv)*100); 
                  Display(st01); 
                  sprintf(st01, "\r\n<<< num_of_mRecent=%.0f  (%.2f%) >>>\r\n", num_of_mRecent, (num_of_mRecent/num_of_rcv)*100); 
                  Display(st01); 
                  sprintf(st01, "\r\n<<< num_of_played=%.0f  (%.2f%) >>>\r\n", num_of_played, (num_of_played/num_of_rcv)*100); 
                  Display(st01); 
                  }*/ 
 
                  Sleep(100); 
                  thread1_stop = FALSE; 
                  thread2_stop = FALSE; 
                  thread3_stop = FALSE; 
                  thread4_stop = FALSE; 
 
                  both=0; 
                  send_only=0; 
                  rec_only=0; 
                  Sleep(1500); 
                  if(err_restart == 0) 
                  { 
                  EnableWindow(GetDlgItem(hwndDlg, IDC_STOP), FALSE); 
                  EnableWindow(GetDlgItem(hwndDlg, IDC_START), TRUE); 
                  EnableWindow(GetDlgItem(hwndDlg, IDCANCEL), TRUE); 
                  EnableWindow(GetDlgItem(hwndDlg, IDC_INIT), TRUE); 
                  } 
 }