winsocket main.cpp

类别:编程语言 点击:0 评论:0 推荐:


#include "main.h"


/*  Declare Dialog procedure  */
BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */

HINSTANCE hinst;
HMENU menu;
BOOL server;
int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nCmdShow)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
   
    hinst=hThisInstance;
    hwnd = CreateDialog(hThisInstance,
                     MAKEINTRESOURCE(IDD_DEMO),//identifies dialog box template name      
                     0,//HWND_DESKTOP,// handle to owner window      
                     (DLGPROC)DlgProc// pointer to dialog box procedure
                     );
     if (!hwnd)
  {
     char buf[100];
  //formats and stores a series of characters and values in a buffer
     wsprintf (buf,"Error x%x", GetLastError());
     MessageBox(0,buf,szDlgTitle,MB_ICONEXCLAMATION | MB_OK);
     return 1;
  }
 
   
    /* Run the message loop. It will run until GetMessage() returns 0 */
    int status;
    while ((status=GetMessage (&messages, NULL, 0, 0))!=0)
    {
        if(!IsDialogMessage(hwnd,&messages))
        {
            if (status==-1)
                 return -1;
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }   
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

BOOL CALLBACK DlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HWND hSend;
    switch (message)                  /* handle the messages */
    {
        case WM_INITDIALOG:
            menu = LoadMenu(hinst, MAKEINTRESOURCE(ID_MENU));
            SetMenu(hwnd, menu);
            hSend=GetDlgItem(hwnd,IDB_SEND);
            //SetWindowText(hSend,"dsds" );
            SetDlgItemText(hwnd,IDB_LC,"LC" );
            EnableWindow(GetDlgItem(hwnd,IDB_LC),FALSE);
   EnableWindow(GetDlgItem(hwnd,IDC_IP),FALSE);
   EnableWindow(GetDlgItem(hwnd,IDC_PORT),FALSE);

            SetDlgItemText(hwnd,IDC_IP,"192.168.10.4");
   SetDlgItemText(hwnd,IDC_PORT,"7007" );
            //创建一个计时器
            SetTimer(hwnd,1,1000,NULL);
   //SetFocus(GetDlgItem(hwnd,IDC_SENDTXT));
   break;
        case WM_COMMAND:
           switch(wParam)
           {
             case IDM_FILENEW:
                 HttpClient();
                 break;
             case IDM_FILEOPEN:
             case IDM_FILELISTEN:
      server=TRUE;
               SetDlgItemText(hwnd,IDB_LC,"监听(&L)" );
               SetWindowText(hwnd,"服务器端" );
      EnableWindow(GetDlgItem(hwnd,IDB_LC),TRUE);
      EnableWindow(GetDlgItem(hwnd,IDC_IP),FALSE);
      EnableWindow(GetDlgItem(hwnd,IDC_PORT),FALSE);
               EnableMenuItem(menu,IDM_FILECONNECT,MF_GRAYED);     
      break;
             case IDM_FILECONNECT:
      server=FALSE;
      SetDlgItemText(hwnd,IDB_LC,"连接服务器(&C)" );
      SetWindowText(hwnd,"客户端" );
      EnableWindow(GetDlgItem(hwnd,IDB_LC),TRUE);
      EnableWindow(GetDlgItem(hwnd,IDC_IP),TRUE);
      EnableWindow(GetDlgItem(hwnd,IDC_PORT),TRUE);
      EnableMenuItem(menu,IDM_FILELISTEN,MF_GRAYED);
      break;
             case IDM_EDITUNDO:
             case IDM_EDITCUT:
             case IDM_EDITCOPY:
             case IDM_EDITPASTE:
             case IDM_EDITDELETE:
    case IDM_TRANSPORTFILE:
                  MessageBox( hwnd, (LPSTR) "功能没有实现",
                              (LPSTR)szDlgTitle,
                              MB_ICONINFORMATION | MB_OK );
                  break;

             case IDM_HELPHELP:
                  WinHelp( hwnd, (LPSTR) "HELPFILE.HLP",
                           HELP_HELPONHELP, 0L );
                  break;

             case IDM_FILEEXIT:
                  SendMessage( hwnd, WM_CLOSE, 0, 0L );
                  break;

             case IDM_HELPABOUT:
                 MessageBox(hwnd,"欢迎使用聊天程序1.0版\n\n**科技大学胡子\n2004(C)版权所有 " ,
                 (LPSTR)szDlgTitle,
                 MB_ICONINFORMATION | MB_OK );
                 
                  break;
             case IDM_HELPMYBLOG:
                 ShellExecute(hwnd,"open","http://blog.csdn.net/huyoo",NULL,NULL,SW_NORMAL);
                 break;
   

             case IDCANCEL:
     //MessageBox(hwnd,"你单击了退出, 对话框将会关闭.\n\n再见!",szDlgTitle,0);
     EndDialog(hwnd,TRUE);
        SendMessage( hwnd, WM_CLOSE, 0, 0L );
     break;   
     
             case IDB_SEND:
     char szText[128],buf[256];
     GetDlgItemText(hwnd,IDC_SENDTXT,szText,sizeof(szText));
     wsprintf(buf,"你单击了发送消息.\n\n你输入的文本是:%s",szText);
     
     if(server)
     {
         if( send(connected_skt,buf,strlen(buf),0) == SOCKET_ERROR ) //客户端是阻塞式的
                        {
                          MessageBox(hwnd, "发送消息出错!",szDlgTitle,MB_OK);
                          break;
                        }
     }
                    else
                    {
                        if( send(skt,buf,strlen(buf),0) == SOCKET_ERROR ) //客户端是阻塞式的
                        {
                          MessageBox(hwnd, "发送消息出错!",szDlgTitle,MB_OK);
                          break;
                        }
                        //recv(skt,buf,strlen(buf),0); //它将会停在这里,直到接到数据为止
                    }
                    SetDlgItemText(hwnd,IDC_REVTXT,szText);   
     break;
             case IDB_QQ:
                 ShellExecute(hwnd,"open","qq.exe",NULL,NULL,SW_NORMAL);
                 break;
    case IDB_LC:
      if(server)
      {
     //MessageBox(hwnd,"listen",szDlgTitle,MB_OK);
     if(!CreateServer(hwnd))
     {
                    MessageBox(hwnd,"服务器创建失败",(LPSTR)szDlgTitle,MB_ICONINFORMATION | MB_OK );
                    break;
                 }   
    
      }
      else
      {
     //MessageBox(hwnd,"connect",szDlgTitle,MB_OK);
                 if(!CreateClient(hwnd))
                 {
                    MessageBox(hwnd,"客户端创建失败",(LPSTR)szDlgTitle,MB_ICONINFORMATION | MB_OK );
                    break;
                 }
     char szIP[127],szPORT[127],buf[256];
     GetDlgItemText(hwnd,IDC_IP,szIP,sizeof(szIP));
     GetDlgItemText(hwnd,IDC_PORT,szPORT,sizeof(szPORT));
     wsprintf(buf,">>>>>>连接到服务器:%s端口:%s",szIP,szPORT);
     SetDlgItemText(hwnd,IDC_REVTXT,buf);

      }
      break;

           }
           break;
        case WM_TIMER:
            SYSTEMTIME ti;
            char buf[256];
           
            GetLocalTime(&ti);
            wsprintf(buf,"现在时间是%04d年%02d月%02d日%02d:%02d:%02d,周%d",
                         ti.wYear,ti.wMonth,ti.wDay,ti.wHour,ti.wMinute,ti.wSecond,ti.wDayOfWeek);
            SetWindowText(GetDlgItem(hwnd,IDC_TIME),buf);
           
               break;
  case SOCKETMSG:
      //if(WSAGETSELECTERROR(lParam))
      //break;
   switch(WSAGETSELECTEVENT(lParam))
   {
               case FD_READ:
                   char buf[256];
                   recv(connected_skt,buf,strlen(buf),0);
                   SetDlgItemText(hwnd,IDC_REVTXT,buf);//显示接受到的消息
                  
     break;
      case FD_WRITE:
     break;
         case FD_CLOSE:
                  closesocket(connected_skt);
                  break;
               case FD_CONNECT:
                   SetDlgItemText(hwnd,IDC_REVTXT,"已经连接上");//显示接受到的消息
                   break;
      case FD_ACCEPT:
     int Len = sizeof(clientaddr);
     //建立连接,得到的地址存入客户端clientaddr地址中去
     if((connected_skt=accept(skt,(struct sockaddr *)&clientaddr,&Len))== INVALID_SOCKET  )
     {
       MessageBox(hwnd,"接受客户端的Socket连接失败", szDlgTitle,MB_OK);
       return FALSE;
     }
     //连接上了,然后监听客户端的FD_READ和关闭
     WSAAsyncSelect(connected_skt, hwnd,SOCKETMSG,FD_READ|FD_CLOSE);
     char *welcome="欢迎来到聊天室!!";
     send(wParam, welcome, strlen(welcome), 0);
     break;
         
   }
   SetDlgItemText(hwnd,IDC_REVTXT,"客户端发送消息过来了");//显示接受到的消息
   break;
        case WM_CLOSE:
            if(connected_skt != INVALID_SOCKET)
            {
              closesocket(connected_skt);
            }
      if( skt != INVALID_SOCKET )
            {
              closesocket(skt);
            }
            if( WSACleanup() != 0 )
            {
              MessageBox(hwnd, "不能释放Winsocket",szDlgTitle,MB_OK );
            }

            KillTimer(hwnd,1);
            DestroyWindow(hwnd);

            break;
        case WM_DESTROY:
     
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:
   return FALSE;
       
    }

    return TRUE;
}

本文地址:http://com.8s8s.com/it/it24742.htm