| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2271 人关注过本帖
标题:求助:使用 MFC 开发图形界面程序
只看楼主 加入收藏
zhichishengz
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-6-20
结帖率:33.33%
收藏
 问题点数:0 回复次数:5 
求助:使用 MFC 开发图形界面程序
1.程序为网络应用程序, VC Socket 开发;

2. 采用多线程技术;

3. 体现多线程同步;
急求,忘哪位高手帮忙一下
邮箱785813002@
搜索更多相关主题的帖子: MFC 图形 界面 开发 
2010-06-22 10:41
zhichishengz
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-6-20
收藏
得分:0 
高手们求帮忙啊,我现在非常急啊
2010-06-22 21:04
dyqq1234
Rank: 2
等 级:论坛游民
帖 子:21
专家分:10
注 册:2008-10-24
收藏
得分:0 
tcp聊天的多线程
程序代码:
#include <afxwin.h>
#include <afxsock.h>
#include "WinSock.h"
#include "stdafx.h"
#include <atlimage.h>
#define IDC_BUTTON1 1000
#define IDC_BUTTON2 1001
#define IDC_BUTTON3 1002
#define IDC_LISTBOX1 1003
#define IDC_LISTBOX2 1004
#define IDC_EDIT1 1005
#define IDC_EDIT2 1006
#define IDC_EDIT3 1007
#define IDC_GROUPBOX1 1008
#define IDC_GROUPBOX2 1009

CMyApp myApp;
SOCKET sock,clisock,msgsock[50];
sockaddr_in serv,cli;
int m_nAddlen,ee;
int FontSize=80;
BOOL DoFlag=FALSE;
int xop,yop;

CStatic label1;
CEdit edit1;
CEdit edit2;
CListBox listbox1;
CListBox listbox2;
char ipaddress[35];
COLORREF color=RGB(0,0,0);
CString str1;

BOOL CMyApp::InitInstance()
{
    if (!AfxSocketInit())
    {
        return FALSE;
    }

    m_pMainWnd=new CMainWindow;
    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
    ON_WM_CREATE()
    ON_WM_CTLCOLOR()
    ON_WM_ERASEBKGND()
    ON_WM_MOUSEMOVE()
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONUP()
    ON_BN_CLICKED(IDC_BUTTON1,OnButtonClicked1)
    ON_BN_CLICKED(IDC_BUTTON2,OnButtonClicked2)
    ON_BN_CLICKED(IDC_BUTTON3,OnButtonClicked3)
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
    Create(NULL,_T("WinSock局域网聊天工具"),WS_POPUP,CRect(100,100,606,560));
}

int CMainWindow::OnCreate(LPCREATESTRUCT lpcs)
{
    if (CFrameWnd::OnCreate(lpcs)==-1)
        return -1;

    font.CreatePointFont(80,"MS Sans Serif");
    CRect rect;
    rect.SetRect(430,330,470,350);
    button1.Create(_T("连接"),WS_CHILD | WS_VISIBLE | SS_CENTER | BS_DEFPUSHBUTTON,rect,this,IDC_BUTTON1);
    button1.SetFont(&font);

    rect.SetRect(415,360,485,380);
    button2.Create(_T("断开连接"),WS_CHILD | WS_VISIBLE | SS_CENTER | BS_DEFPUSHBUTTON,rect,this,IDC_BUTTON2);
    button2.SetFont(&font);

    rect.SetRect(415,390,485,410);
    button3.Create(_T("发送信息"),WS_CHILD | WS_VISIBLE | SS_CENTER | BS_DEFPUSHBUTTON,rect,this,IDC_BUTTON3);
    button3.SetFont(&font);

    rect.SetRect(6,10,320,304);
    listbox1.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL,rect,this,IDC_LISTBOX1);
    listbox1.SetFont(&font);

    rect.SetRect(340,10,490,304);
    listbox2.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL,rect,this,IDC_LISTBOX2);
    listbox2.SetFont(&font);

    rect.SetRect(338,0,491,305);
    GroupBox1.Create(_T("客户端名称"),WS_CHILD | WS_VISIBLE | BS_GROUPBOX,rect,this,IDC_GROUPBOX1);
    GroupBox1.SetFont(&font);
   
    rect.SetRect(5,0,325,305);
    GroupBox2.Create(_T("聊天记录窗口"),WS_CHILD | WS_VISIBLE | BS_GROUPBOX,rect,this,IDC_GROUPBOX2);
    GroupBox2.SetFont(&font);

    rect.SetRect(105,340,190,360);
    edit1.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,rect,this,IDC_EDIT1);
    edit1.SetFont(&font);
    edit1.EnableWindow(FALSE);

    rect.SetRect(5,310,85,330);
    label2.Create("对方IP地址栏:",WS_CHILD | WS_VISIBLE | SS_CENTER,rect,this);
    label2.SetFont(&font);

    rect.SetRect(5,340,95,360);
    label1.Create(NULL,WS_CHILD | WS_VISIBLE | SS_CENTER,rect,this);
    label1.SetFont(&font);

    rect.SetRect(105,310,190,330);
    edit2.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,rect,this,IDC_EDIT2);
    edit2.SetFont(&font);

    rect.SetRect(20,370,400,450);
    edit3.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE,rect,this,IDC_EDIT3);
    edit3.SetFont(&font);

    WinSockStart();
}

void CMainWindow::WinSockStart()
{
    TRY{
    for (int i=0;i<50;i++)
        msgsock[i]=NULL;
    serv.sin_addr.s_addr=htonl(INADDR_ANY);
    serv.sin_family=AF_INET;
    serv.sin_port=5000;
    m_nAddlen=sizeof(serv);
    sock=socket(AF_INET,SOCK_STREAM,0);

    if (bind(sock,(sockaddr*)&serv,m_nAddlen)){
        MessageBox("绑定错误!");
    }else{
        label1.SetWindowText("成功启动服务器");
        listen(sock,5);
        AfxBeginThread(thread,0);
    }
    }CATCH (CException,e){

    }END_CATCH
}

int getcount();

UINT thread(LPVOID wParam)
{
    label1.SetWindowText("");
    char buff[100];
    int s=1,msgcount,loop=1,flag=0;
   
    //CMainWindow *main=(CMainWindow* ) AfxGetApp()->GetMainWnd();
    //CMainWindow *main=(CMainWindow* ) AfxGetApp()->m_pMainWnd;
    //CMainWindow *main=(CMainWindow*)CWnd::FromHandle((HWND) wParam);

    msgcount=getcount();
    if (msgcount==-1)
        loop=0;
    if(loop){
        s=1;
        msgsock[msgcount]=accept(sock,(sockaddr*)&(serv),&(m_nAddlen));
        if (msgsock[msgcount]==INVALID_SOCKET){
            label1.SetWindowText("Error accept");
        }else{
            AfxBeginThread(thread,0);
            while (s!=SOCKET_ERROR){
                s=recv(msgsock[msgcount],buff,100,0);
                if (s!=SOCKET_ERROR){
                    listbox1.AddString(CString("连入端 ")+ipaddress+CString(" : ")+buff);
                }
            }
            msgsock[msgcount]=NULL;
            closesocket(msgsock[msgcount]);
        }
    }
    AfxEndThread(0);

    return 0;
}

int getcount()
{
    for(int i=0;i<50;i++){
        if (msgsock[i]==NULL)
            return i;
    }
    return -1;
}

UINT thread1(LPVOID wParam){
    char buff[100];
    char array[25][30];
    int s=1,addcount=0;

    //CMainWindow *main=(CMainWindow* ) AfxGetApp()->GetMainWnd();
    //CMainWindow *main=(CMainWindow*)AfxGetMainWnd();
    //CMainWindow *main=(CMainWindow*)(CWnd::FromHandle((HWND) wParam));

    while(connect(clisock,(sockaddr*)&cli,sizeof(cli))&&ee!=0){
        label1.SetWindowText("");
        label1.SetWindowText("等待中...");
        label1.SetWindowText("");
        for (int i=0;i<=65000;i++)
            for(int j=0;j<=200;j++);
        if (addcount==25)
            addcount=0;
        cli.sin_addr.s_addr=inet_addr(array[addcount++]);
    }
    if (ee==1){
        edit1.SetWindowText("连接成功!");
    }

    while (s!=SOCKET_ERROR && ee!=0){
        s=recv(clisock,buff,100,0);
        if (s!=SOCKET_ERROR && ee!=0){
            listbox1.AddString(buff);
        }
    }

    closesocket(clisock);
    AfxEndThread(0);
    return 0;
}

void CMainWindow::OnButtonClicked1()
{
    edit2.GetWindowText(ipaddress,30);
    cli.sin_addr.s_addr=inet_addr(ipaddress);
    cli.sin_family=AF_INET;
    cli.sin_port=5000;
    clisock=socket(AF_INET,SOCK_STREAM,0);
    ee=1;
    edit1.SetWindowText(ipaddress);
    listbox2.AddString(ipaddress);
    button1.EnableWindow(FALSE);
    edit2.EnableWindow(FALSE);
    AfxBeginThread(thread1,0);
}

void CMainWindow::OnButtonClicked2()
{
    ee=0;
    closesocket(clisock);
    edit2.EnableWindow(TRUE);
    button1.EnableWindow(TRUE);
}

void CMainWindow::OnButtonClicked3()
{
    char buff[100];
    edit3.GetWindowText(buff,99);
    listbox1.AddString(CString("本人端 : ")+inet_ntoa(serv.sin_addr)+CString(" : ")+buff);
    send(clisock,buff,100,0);
}

BOOL CMainWindow::PreTranslateMessage(MSG *pMsg)
{
    label1.SetWindowText("成功启动服务器");
    char buff[100];
    edit3.GetWindowText(buff,99);
    CString str=buff;

    edit2.GetWindowText(ipaddress,30);
    str1=ipaddress;

    if ((pMsg->wParam==VK_RETURN) && (::GetKeyState(VK_CONTROL)<0)){
        if (edit3.GetFocus()==GetDlgItem(IDC_EDIT3)){
            if (str1.IsEmpty()){
                MessageBox("请输入IP地址!");
                edit3.SetWindowTextA("");
            }else if (!str.IsEmpty()){
                listbox1.AddString(CString("本人端 ")+inet_ntoa(serv.sin_addr)+CString(" : ")+buff);
                send(clisock,buff,100,0);
                edit3.SetWindowTextA("");
            }
        }
    }else if((pMsg->wParam==0x46) && (::GetKeyState(VK_CONTROL)<0)){
        CFontDialog dlg;

        if (IDOK==dlg.DoModal()){
            font1.Detach();
            LOGFONT temp;
            dlg.GetCurrentFont(&temp);
            font1.CreateFontIndirect(&temp);
            color=dlg.GetColor();
            listbox1.SetFont(&font1,FALSE);
            Invalidate();
        }
    }else if (pMsg->wParam==VK_ESCAPE){
        DestroyWindow();
        PostQuitMessage(0);
    }else if (pMsg->wParam==VK_RETURN){
        if (edit3.GetFocus()==GetDlgItem(IDC_EDIT3)){
            if (str1.IsEmpty()){
                MessageBox("请输入IP地址!");
                edit3.SetWindowTextA("");
            }else if (!str.IsEmpty()){
                listbox1.AddString(CString("本人端 ")+inet_ntoa(serv.sin_addr)+CString(" : ")+buff);
                send(clisock,buff,100,0);
                edit3.SetWindowTextA("");
            }
        }else if (edit2.GetFocus()==GetDlgItem(IDC_EDIT2)){
            if (!str1.IsEmpty()){
                edit2.GetWindowText(ipaddress,30);
                cli.sin_addr.s_addr=inet_addr(ipaddress);
                cli.sin_family=AF_INET;
                cli.sin_port=5000;
                clisock=socket(AF_INET,SOCK_STREAM,0);
                ee=1;
                edit1.SetWindowText(ipaddress);
                listbox2.AddString(ipaddress);
                button1.EnableWindow(FALSE);
                edit2.EnableWindow(FALSE);
                AfxBeginThread(thread1,0);
            }else{
                MessageBox("请输入IP地址!");
            }
        }
    }else{
        return CFrameWnd::PreTranslateMessage(pMsg);
    }
}

BOOL CMainWindow::OnEraseBkgnd(CDC *pDC)
{
    CRect rect;
    GetClientRect(rect);

    DoGradientFill(pDC,&rect);
    return TRUE;
}

void CMainWindow::DoGradientFill(CDC *pDC,CRect rect)
{
    CImage img;
    pDC->SetBkMode(TRANSPARENT);
    img.Load("21.png");
    img.Draw(pDC->m_hDC,0,0);
    SetWindowLong(m_hWnd,GWL_EXSTYLE,WS_EX_LAYERED);
    SetLayeredWindowAttributes(RGB(255,255,255),0,LWA_COLORKEY);
}

HBRUSH CMainWindow::OnCtlColor(CDC *pDC,CWnd *pWnd,UINT nCtlColor)
{
    CDC dc;
    CBitmap bitmap;
    bitmap.LoadBitmap("IDB_BITMAP2");
    dc.CreateCompatibleDC(pDC);
    dc.SelectObject(&bitmap);

    HBRUSH hbr;
    //HBRUSH hbr=CFrameWnd::OnCtlColor (pDC,pWnd,nCtlColor );
    if (listbox1.m_hWnd==pWnd->m_hWnd){
        pDC->SetTextColor(color);
        pDC->SetBkMode(TRANSPARENT);
        hbr=::CreateSolidBrush(RGB(23,234,123));
        return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
    }else if (listbox2.m_hWnd==pWnd->m_hWnd){
        pDC->SetTextColor(RGB(0,0,0));
        pDC->SetBkMode(TRANSPARENT);
        hbr=::CreateSolidBrush(RGB(23,34,123));
        return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
    }else if (edit1.m_hWnd==pWnd->m_hWnd){
        hbr=::CreateSolidBrush(RGB(23,234,123));
        return hbr;
    }else if (edit2.m_hWnd==pWnd->m_hWnd){
        hbr=::CreateSolidBrush(RGB(23,234,123));
        return hbr;
    }else if (edit3.m_hWnd==pWnd->m_hWnd){
        pDC->SetTextColor(RGB(0,0,0));
        pDC->SetBkMode(TRANSPARENT);
        pDC->BitBlt(0,0,365,65,&dc,0,0,SRCCOPY);
        return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
    }else if (label1.m_hWnd==pWnd->m_hWnd){
        pDC->SetTextColor(RGB(0,0,0));
        pDC->SetBkMode(TRANSPARENT);
        return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
    }else if (label2.m_hWnd==pWnd->m_hWnd){
        pDC->SetTextColor(RGB(0,0,0));
        pDC->SetBkMode(TRANSPARENT);
        return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
    }
    return hbr;
}

void CMainWindow::PostNcDestroy()
{
    delete this;
}

void CMainWindow::OnLButtonDown(UINT nFlags,CPoint point)
{
    DoFlag=TRUE;
    xop=point.x;
    yop=point.y;
}

void CMainWindow::OnLButtonUp(UINT nFlags, CPoint point)
{
    DoFlag=FALSE;
}

void CMainWindow::OnMouseMove(UINT nFlags, CPoint point)
{
    if (DoFlag){
        CRect rect;
        ::GetWindowRect(m_hWnd,rect);

        ::SetWindowPos(m_hWnd,NULL,rect.left+point.x-xop,rect.top+point.y-yop,rect.Width(),rect.Height(),SWP_NOSIZE);
    }
}
收到的鲜花
  • zhichishengz2010-06-22 22:20 送鲜花  3朵   附言:我很赞同
2010-06-22 21:23
dyqq1234
Rank: 2
等 级:论坛游民
帖 子:21
专家分:10
注 册:2008-10-24
收藏
得分:0 
UDP聊天程序
程序代码:
#include "Main.h"

CMyApp myApp;

/*************************************************/
/**作 者: 丁 屹                                  */
/**日 期: 2010.4.28                              */
/**函数参数: 无参数                              */
/**函数功能: 初始化窗体                          */
/**函数返回值: 无返回值                          */
/*************************************************/
BOOL CMyApp::InitInstance()
{
    m_pMainWnd = new CMainWindow;
    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
    ON_WM_CREATE()
    ON_BN_CLICKED(IDC_BUTTON1,OnButtonClicked1)
    ON_NOTIFY(TVN_SELCHANGED, IDC_TREEVIEW, OnSelchangedTrdisktree)
END_MESSAGE_MAP()

/*************************************************/
/**作 者: 丁 屹                                  */
/**日 期: 2010.4.28                              */
/**函数参数: 无参数                              */
/**函数功能: 创建UDP连接                         */
/**函数返回值: 无返回值                          */
/*************************************************/
void CMainWindow::CreateUDP()
{
    ListenSocket = INVALID_SOCKET;
    WORD VersionRequested = MAKEWORD(1,1);
    WSADATA wsaData;
    WSAStartup(VersionRequested, &wsaData);
    if (wsaData.wVersion !=VersionRequested)
    {
        TextBox2.SetWindowText("Wrong version or WinSock not loaded\n");
        fflush(0);
        return;
    }

    ListenSocket = socket(PF_INET, SOCK_DGRAM, 0);  
    if (ListenSocket == INVALID_SOCKET)
    {
        TextBox2.SetWindowText("Error: socket创建失败\n");
        fflush(0);
        return;
    }

    char szHostName[128];
    hostent *hostdata;
    gethostname(szHostName,128);
    hostdata = gethostbyname(szHostName);

    srv.sin_family = PF_INET;
    srv.sin_addr.s_addr = htonl(INADDR_ANY);
    srv.sin_port = htons(5000);
    ListBox1.AddString(CString("【在线】") + inet_ntoa(*(struct in_addr *)hostdata->h_addr_list[0]));

    if (bind(ListenSocket,(struct sockaddr *)&srv,sizeof(srv)) != 0)
    {
        TextBox2.SetWindowText("Error: 绑定失败\n");
        fflush(0);
        closesocket(ListenSocket);
        return;
    }

    FileReader("IPList.tmp");
    AfxBeginThread(RecvThread,inet_ntoa(*(struct in_addr *)hostdata->h_addr_list[0]));
    AfxBeginThread(SendMe,NULL);
}

/*************************************************/
/**作 者: 丁 屹                                  */
/**日 期: 2010.5.12                              */
/**函数功能: 发送自己IP地址                      */
/**函数返回值: 无返回值                          */
/*************************************************/
UINT SendMe(LPVOID wParam)
{
    CString Ipa2;
    CString Ipa;
    CString Ipa1;

    for (int i = 1; i<ListBox1.GetCount(); i++)
    {
        ListBox1.GetText(i,Ipa2);
        //Ipa1 = Ipa1 + Ipa2 + CString("\r\n");
        //TextBox1.SetWindowText(Ipa1);
        dest.sin_family = PF_INET;
        dest.sin_addr.s_addr=inet_addr(Ipa2.Mid(8));
        dest.sin_port = htons(5000);

        ListBox1.GetText(1,Ipa);
        Sent = sendto(ListenSocket, CString("@") + Ipa.Mid(8) , Ipa.Mid(8).GetLength() + 1, 0, (sockaddr *)&dest, sizeof(sockaddr_in));
    }
    AfxEndThread(0);
    return 0;
}

/*************************************************/
/**作 者: 丁 屹                                  */
/**日 期: 2010.5.12                              */
/**函数功能: 发送本人所有好友的IP地址            */
/**函数返回值: 无返回值                          */
/*************************************************/
UINT SendAll(LPVOID wParam)
{
    CString Ipa2;
    CString Msg;

    for (int i = 1; i<ListBox1.GetCount(); i++)
    {
        ListBox1.GetText(i,Ipa2);
        dest.sin_family = PF_INET;
        dest.sin_addr.s_addr=inet_addr(Ipa2.Mid(8));
        dest.sin_port = htons(5000);

        for (int j = 1; j<ListBox1.GetCount(); j++)
        {
            ListBox1.GetText(j,Msg);
            Sent = sendto(ListenSocket, CString("%") + Msg.Mid(8), Msg.Mid(8).GetLength() + 1, 0, (sockaddr *)&dest, sizeof(sockaddr_in));
        }
    }
    AfxEndThread(0);
    return 0;
}

/*************************************************/
/**作 者: 丁 屹                                  */
/**日 期: 2010.4.28                              */
/**函数参数: 无参数                              */
/**函数功能: 创建窗体                            */
/**函数返回值: 无返回值                          */
/*************************************************/
CMainWindow::CMainWindow()
{
    Create(NULL,_T("UDP网络功能函数"),WS_OVERLAPPEDWINDOW,CRect(50,50,700,600));
}

/*************************************************/
/**作 者: 丁 屹                                  */
/**日 期: 2010.4.28                              */
/**函数参数: LPCREATESTRUCT窗体创建结构体        */
/**函数功能: 初始化窗体控件                      */
/**函数返回值: 返回值为创建成功与否              */
/*************************************************/
int CMainWindow::OnCreate(LPCREATESTRUCT lpcs)
{
    if (CFrameWnd::OnCreate(lpcs) == -1)
        return -1;

    font.CreatePointFont(100,_T("楷体_GB2312"));
    TextBox1.Create(WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL | ES_MULTILINE,CRect(10,380,430,460),this,IDC_TEXTBOX1);
    TextBox1.SetFont(&font);
   
    TextBox2.Create(WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL | ES_MULTILINE,CRect(10,10,210,370),this,IDC_TEXTBOX2);
    TextBox2.SetFont(&font);
    TextBox2.EnableWindow(FALSE);
   
    TextBox3.Create(WS_VISIBLE | WS_CHILD | WS_BORDER,CRect(90,470,190,490),this,IDC_TEXTBOX3);
    TextBox3.SetFont(&font);

    Button1.Create(_T("发送"),WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,CRect(10,470,80,490),this,IDC_BUTTON1);
    Button1.SetFont(&font);
   
    ListBox1.Create(WS_VISIBLE | WS_CHILD | WS_BORDER,CRect(230,10,430,370),this,IDC_LISTBOX1);
    ListBox1.SetFont(&font);
    ListBox1.AddString("【群聊】");
   
    TreeView.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | TVS_HASLINES | TVS_LINESATROOT,CRect(450,10,630,480),this,IDC_TREEVIEW);
    ImageList.Create(28,28,ILC_COLOR32 | ILC_MASK,0,0);
    TreeView.SetImageList(&ImageList,TVSIL_NORMAL);

    CreateUDP();
    return 0;
}

/***************************************************/
/**作 者: 丁 屹                                    */
/**日 期: 2010.5.1                                 */
/**函数功能: 获得IP地址列表                        */
/**函数参数: 文件名字                              */
/**函数返回值: 无返回值                            */
/***************************************************/
void FileReader(CString FileName)
{
    CString IPName;
    CFileFind fd;
    if (fd.FindFile(FileName) == TRUE)
    {
        CStdioFile file(FileName,CFile::modeRead);
        while (file.ReadString(IPName))
        {
            ListBox1.AddString(CString("【离线】") + IPName);
        }
    }
}

/*************************************************/
/**作 者: 丁 屹                                  */
/**日 期: 2010.4.28                              */
/**函数功能: 按键点击事件                        */
/**函数返回值: 无返回值                          */
/*************************************************/
void CMainWindow::OnButtonClicked1()
{
    CString Ipa1;
    CString Msg;
    CString Ipa2;

    TextBox1.GetWindowText(Msg);
    //群聊跟能模块
    if (ListBox1.GetCurSel() == 0)
    {
        for (int i = 1; i<ListBox1.GetCount(); i++)
        {
            int Len;
            ListBox1.GetText(i,Ipa2);
            dest.sin_family = PF_INET;
            //dest.sin_addr = *(in_addr *)(hostdata->h_addr_list[0]);
            dest.sin_addr.s_addr=inet_addr(Ipa2.Mid(8));
            dest.sin_port = htons(5000);
            //TextBox1.SetWindowText("信息已经被发送到主机 %s 端口为 %i\n",inet_ntoa(dest.sin_addr), ntohs(dest.sin_port));
            Len = strlen(Msg);
            Sent = sendto(ListenSocket, Msg, Msg.GetLength(), 0, (sockaddr *)&dest, sizeof(sockaddr_in));
        }
    }
    //私聊功能模块
    else
    {
        int Len;
        ListBox1.GetText(ListBox1.GetCurSel(),Ipa1);
        dest.sin_family = PF_INET;
        //dest.sin_addr = *(in_addr *)(hostdata->h_addr_list[0]);
        dest.sin_addr.s_addr=inet_addr(Ipa1.Mid(8));
        dest.sin_port = htons(5000);
        //TextBox1.SetWindowText("信息已经被发送到主机 %s 端口为 %i\n",inet_ntoa(dest.sin_addr), ntohs(dest.sin_port));
        Len = strlen(Msg);
        Sent = sendto(ListenSocket, Msg, Msg.GetLength(), 0, (sockaddr *)&dest, sizeof(sockaddr_in));
    }
}

/*************************************************/
/**作 者: 丁 屹                                  */
/**日 期: 2010.4.28                              */
/**函数功能: 接受文件线程                        */
/**函数返回值: 返回线程                          */
/*************************************************/
UINT RecvThread(LPVOID wParam)
{
    char buf[4096];
    int len = sizeof(client);
    CString buff="";

    while(TRUE)
    {
        int result = recvfrom(ListenSocket, buf, sizeof(buf)-1, 0,(sockaddr*)&client, (socklen_t *)&len);
        for (int j = 1 ;j<ListBox1.GetCount(); j++)
        {
            if(ListBox1.FindString(j,CString("【在线】") + inet_ntoa(client.sin_addr)) == LB_ERR)
            {
                FileWriter(inet_ntoa(client.sin_addr));
                ReConnect(inet_ntoa(client.sin_addr));
                break;
            }
        }

        buf[result]=0;
        if (result > 0)
        {
            //获取根目录
            if (buf[0] == '&')
            {
                Index1 = ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
                root = TreeView.InsertItem(CString(buf).Mid(1),0,0);
            }
            //获取子目录名
            else if (buf[0] == '#')
            {
                Index1 = ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
                TreeView.InsertItem(CString(buf).Mid(1),Index1,Index1,root);
            }
            //获取文件名
            else if (buf[0] == '$')
            {
                Index2 = ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
                TreeView.InsertItem(CString(buf).Mid(1),Index2,Index2,root);
            }
            //点击事件
            else if (buf[0] == '*')
            {
                strcat(buf ,"\\*.*");
                AfxBeginThread(SendDir,(char *)buf + 1);
            }
            //发送自己的IP地址
            else if (buf[0] == '@')
            {
                CString UserName;
                for (int j = 1 ;j<ListBox1.GetCount(); j++)
                {
                    if(ListBox1.FindString(j,CString("【在线】") + inet_ntoa(client.sin_addr)) == LB_ERR)
                    {
                        ListBox1.AddString(CString("【在线】") + CString(buf).Mid(1));
                        Sent = sendto(ListenSocket, CString("@") + (char*)wParam, strlen((char*)wParam) + 1, 0, (sockaddr *)&dest, sizeof(sockaddr_in));
                    }

                    ListBox1.GetText(j,UserName);
                    if (UserName.Mid(8) == CString(buf).Mid(1) && UserName.Left(8) == CString("【离线】"))
                    {
                        ListBox1.DeleteString(j);
                        break;
                    }
                }
                //Sent = sendto(ListenSocket, CString("@") + (char*)wParam, strlen((char*)wParam) + 1, 0, (sockaddr *)&dest, sizeof(sockaddr_in));
            }
            //接受消息
            else
            {
                buff = buff + CString(buf) + "\r\n";
                //TextBox2.SetWindowText(CString("Message received from host") +inet_ntoa(client.sin_addr) + CString("port") + ntohs(client.sin_port) + CString("\r\n"));
                TextBox2.SetWindowText(buff);
            }
        }
        //TreeView.Invalidate();
    }
    AfxEndThread(0);
    return 0;
}

/***************************************************/
/**作 者: 丁 屹                                    */
/**日 期: 2010.5.1                                 */
/**函数功能: 记录对方IP地址                        */
/**函数参数: 对方的IP                              */
/**函数返回值: 无返回值                            */
/***************************************************/
void FileWriter(CString IPName)
{
    CFile file;
    file.Open("IPList.tmp",CFile::modeCreate | CFile::modeWrite);
    file.Write(IPName + "\r\n",IPName.GetLength() + 2);
}

/***************************************************/
/**作 者: 丁 屹                                    */
/**日 期: 2010.5.1                                 */
/**函数功能: 发送目录列表                          */
/**函数返回值: 无返回值                            */
/***************************************************/
UINT SendDir(LPVOID wParam)
{
    if ((char*)wParam ==NULL)
    {
        //遍历该系统中的所有盘符
        DWORD dw=GetLogicalDrives();
        DWORD mask=1;
        HTREEITEM root[1000];
        for(int i=0;i<sizeof(DWORD)*8;i++){
            if(dw&mask){
                //遍历每个盘符下的文件
                Sent = sendto(ListenSocket, CString("&") + CString(char('A'+i)), + CString(char('A'+i)).GetLength() + 1 , 0, (sockaddr *)&dest, sizeof(sockaddr_in));
                FindDir(char('A'+i) + CString(":\\*.*"));
            }
            mask<<=1;
        }
    }
    else
    {
        FindDir((char*)wParam);
    }

    AfxEndThread(0);
    return 0;
}

/***************************************************/
/**作 者: 丁 屹                                    */
/**日 期: 2010.5.1                                 */
/**函数功能: 文件遍历函数                          */
/**函数参数: 目录路径                              */
/**函数返回值: 无返回值                            */
/***************************************************/
void FindDir(CString Name)
{
    CFileFind file;
    CFileFind files;
    CString str;

    BOOL bf1 = file.FindFile(Name);

    while (bf1)
    {
        bf1 = file.FindNextFile();
        if (!file.IsDots() && !file.IsDirectory())
        {
            Sent = sendto(ListenSocket, CString("$") + file.GetFilePath(), file.GetFilePath().GetLength() + 1 , 0, (sockaddr *)&dest, sizeof(sockaddr_in));
        }
    }

    BOOL bf2 = files.FindFile(Name);
    while (bf2)
    {
        bf2 = files.FindNextFile();
        if (files.IsDirectory() && !files.IsDots())
        {
            Sent = sendto(ListenSocket,CString("#") + files.GetFilePath(), files.GetFilePath().GetLength() + 1, 0, (sockaddr *)&dest, sizeof(sockaddr_in));
        }
    }
}

/***************************************************/
/**作 者: 丁 屹                                    */
/**日 期: 2010.5.3                                 */
/**函数功能: 树形列表点击事件                      */
/**函数参数: 目录路径                              */
/**函数返回值: 无返回值                            */
/***************************************************/
void CMainWindow::OnSelchangedTrdisktree(NMHDR* pNMHDR, LRESULT* pResult)
{
    root = TreeView.GetSelectedItem();
    Sent = sendto(ListenSocket,CString("*") + TreeView.GetItemText(root) , TreeView.GetItemText(root).GetLength() + 1, 0, (sockaddr *)&dest, sizeof(sockaddr_in));
}


BEGIN_MESSAGE_MAP(TextBox,CEdit)
    ON_WM_KEYDOWN()
END_MESSAGE_MAP()
/*************************************************/
/**作 者: 丁 屹                                  */
/**日 期: 2010.4.28                              */
/**函数功能: 按键点击事件                        */
/**函数返回值: 无返回值                          */
/*************************************************/
void TextBox::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    CString Ipa;
    if (nChar == VK_RETURN)
    {
        GetWindowText(Ipa);
        hostent *hostdata;
        u_long ip = inet_addr(Ipa);
        hostdata = gethostbyaddr((char *)&ip, sizeof(ip),PF_INET);

        dest.sin_family = PF_INET;
        //dest.sin_addr = *(in_addr *)(hostdata->h_addr_list[0]);
        dest.sin_addr.s_addr = inet_addr(Ipa);
        dest.sin_port = htons(5000);

        //ListBox1.AddString(Ipa);
        TextBox2.SetWindowText(CString("") + Ipa + CString("连接成功") + CString("\r\n"));
        EnableWindow(FALSE);
        AfxBeginThread(SendDir,NULL);
    }
}

/***************************************************/
/**作 者: 丁 屹                                    */
/**日 期: 2010.5.10                                */
/**函数功能: 返回连接事件                          */
/**函数参数: 对方IP地址                            */
/**函数返回值: 无返回值                            */
/***************************************************/
void ReConnect(CString Ipa)
{
    int i = 1;
    hostent *hostdata;
    u_long ip = inet_addr(Ipa);
    hostdata = gethostbyaddr((char *)&ip, sizeof(ip),PF_INET);

    dest.sin_family = PF_INET;
    //dest.sin_addr = *(in_addr *)(hostdata->h_addr_list[0]);
    dest.sin_addr.s_addr=inet_addr(Ipa);
    dest.sin_port = htons(5000);

    i++;
    ListBox1.AddString(CString("【在线】") + Ipa);
    //ListBox1.AddString(inet_ntoa(*(struct in_addr *)hostdata->h_addr_list[0]));
    TextBox2.SetWindowText(CString("") + Ipa + CString("连接成功") + CString("\r\n"));
    TextBox3.SetWindowText(Ipa);
    TextBox3.EnableWindow(FALSE);

    AfxBeginThread(SendDir,NULL);
}
收到的鲜花
  • zhichishengz2010-06-22 22:20 送鲜花  3朵   附言:我很赞同
2010-06-22 21:24
dyqq1234
Rank: 2
等 级:论坛游民
帖 子:21
专家分:10
注 册:2008-10-24
收藏
得分:0 
TCP文件传输
程序代码:
#include "Main.h"

CMyApp myApp;
CListBox ListBox1;
CListBox ListBox2;
sockaddr_in serv,cli,fserv,fcli;
SOCKET sock1=NULL,sock2=NULL,msock1,msock2,sock3=NULL,sock4=NULL;
CString SourceFileName;
CString DestFileName;
CFile sourceFile;
CEdit TextBox1;
CFile destFile;
BYTE *buff1;
BYTE *buff2;
BOOL flag=FALSE;
CString strIP;
int count=0;
int scount=0;
int ConnectNum=0;
CButton Button1;

BOOL CMyApp::InitInstance()
{
    m_pMainWnd=new CMainWindow;
    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
    ON_WM_CREATE()
    ON_WM_ERASEBKGND()
    ON_WM_CTLCOLOR()
    ON_WM_NCHITTEST()
    ON_BN_CLICKED(IDC_BUTTON1,OnButtonClicked1)
    ON_BN_CLICKED(IDC_BUTTON2,OnButtonClicked2)
    ON_BN_CLICKED(IDC_BUTTON3,OnButtonClicked3)
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
    if (!AfxSocketInit())
        MessageBox("套接字初始失败");

    Create(NULL,_T("超级终端"),WS_POPUPWINDOW,CRect((20*GetSystemMetrics(SM_CXSCREEN))/1280,(20*GetSystemMetrics(SM_CYSCREEN))/960,(960*GetSystemMetrics(SM_CXSCREEN))/1280,(900*GetSystemMetrics(SM_CYSCREEN))/960));
}

int CMainWindow::OnCreate(LPCREATESTRUCT lpcs)
{
    if (CFrameWnd::OnCreate(lpcs)==-1)
        return -1;

    int nFullWidth=GetSystemMetrics(SM_CXSCREEN);
    int nFullHeight=GetSystemMetrics(SM_CYSCREEN);

    HRGN hRgn=::CreateRoundRectRgn((10*nFullWidth)/1280,(10*nFullHeight)/960,(930*nFullWidth)/1280,(870*nFullHeight)/960,30,30);
    SetWindowRgn(hRgn,TRUE);

    font.CreatePointFont(100,"楷书_GB2312");
    Button1.Create(_T("连接"),WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,CRect((50*nFullWidth)/1280,(800*nFullHeight)/960,(100*nFullWidth)/1280,(825*nFullHeight)/960),this,IDC_BUTTON1);
    Button1.SetFont(&font);
    Button2.Create(_T("发送"),WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,CRect((105*nFullWidth)/1280,(800*nFullHeight)/960,(155*nFullWidth)/1280,(825*nFullHeight)/960),this,IDC_BUTTON2);
    Button2.SetFont(&font);
    Button3.Create(_T("发送文件"),WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,CRect((160*nFullWidth)/1280,(800*nFullHeight)/960,(220*nFullWidth)/1280,(825*nFullHeight)/960),this,IDC_BUTTON3);
    Button3.SetFont(&font);
    ListBox1.Create(WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_BORDER,CRect((300*nFullWidth)/1280,(30*nFullHeight)/960,(880*nFullWidth)/1280,(610*nFullHeight)/960),this,IDC_LISTBOX1);
    ListBox1.SetFont(&font);
    ListBox2.Create(WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_BORDER,CRect((50*nFullWidth)/1280,(30*nFullHeight)/960,(290*nFullWidth)/1280,(610*nFullHeight)/960),this,IDC_LISTBOX2);
    ListBox2.SetFont(&font);
    TextBox1.Create(WS_CHILD | WS_VISIBLE | WS_BORDER,CRect((150*nFullWidth)/1280,(615*nFullHeight)/960,(280*nFullWidth)/1280,(640*nFullHeight)/960),this,IDC_TEXTBOX1);
    TextBox1.SetFont(&font);
    TextBox2.Create(WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | ES_MULTILINE | WS_BORDER,CRect((50*nFullWidth)/1280,(650*nFullHeight)/960,(880*nFullWidth)/1280,(790*nFullHeight)/960),this,IDC_TEXTBOX2);
    TextBox2.SetFont(&font);
    Label1.Create(_T("IP地址输入框:"),WS_VISIBLE | WS_CHILD,CRect((50*nFullWidth)/1280,(620*nFullHeight)/960,(145*nFullWidth)/1280,(645*nFullHeight)/960),this,IDC_LABEL1);
    Label1.SetFont(&font);

    CreateFileSocket();
    CreateSocket();
    GetHostIP();

    //FindFile();
    return 0;
}

void CMainWindow::GetHostIP()
{
    char szHostName[128];
    if (gethostname(szHostName,128)==0)
    {
        hostent *pHost;
        pHost=gethostbyname(szHostName);
        strIP=inet_ntoa(*(in_addr*)pHost->h_addr_list[0]);
        ListBox2.AddString(CString("本机IP:")+strIP);
    }
}

void CMainWindow::FindFile()
{
    CFileFind file;
    BOOL bf=file.FindFile("C:\\record.log");
    if (bf)
    {
        char buff[4096]="";
        CFile file1;
        file1.Open("C:\\record.log",CFile::modeRead);
        file1.Read(buff,4096);
        CString buffsize1=CString(buff).Left(CString(buff).Find('#'));
        SourceFileName=buffsize1;
        CString buffsize2=CString(buff).Right(CString(buff).GetLength()-CString(buff).ReverseFind('#')-1);

        CString buffsize3=CString(buff).Mid(CString(buff).Find('#')+1);
        buffsize3=buffsize3.Left(buffsize3.Find('#'));
        scount=atoi(buffsize3);
        count=atoi(buffsize2);

        AutoConnect1();
        AutoConnect2();
    }
    else
    {
        MessageBox("没有文件");
    }
}

void CMainWindow::AutoConnect1()
{
    sock2=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
    cli.sin_addr.s_addr=inet_addr("192.168.1.101");
    cli.sin_family=AF_INET;
    cli.sin_port=5000;

    AfxBeginThread(&Thread2,0);
}

void CMainWindow::AutoConnect2()
{
    sock3=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
    fcli.sin_addr.s_addr=inet_addr("192.168.1.101");
    fcli.sin_family=AF_INET;
    fcli.sin_port=8005;

    AfxBeginThread(&Thread3,0);
}

void AutoConnect(CString ip)
{
    sock3=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
    fcli.sin_addr.s_addr=inet_addr(ip);
    fcli.sin_family=AF_INET;
    fcli.sin_port=8005;

    AfxBeginThread(&Thread2,0);
}

BOOL CMainWindow::OnEraseBkgnd(CDC *pDC)
{
    CRect rect;
    GetClientRect(rect);
    FillWindow(pDC,rect);
    return TRUE;
}

void CMainWindow::FillWindow(CDC *pDC,CRect rect)
{
    CBrush *pBrush[255];
    for (int i=0;i<255;i++)
        pBrush[i]=new CBrush(PALETTERGB(0,0,255-i));

    CRect pRect;
    int nWidth=rect.right-rect.left;
    int nHeight=rect.bottom-rect.top;
    for (int i=0;i<nHeight;i++)
    {
        pRect.SetRect(0,i,nWidth,i+1);
        pDC->FillRect(&pRect,pBrush[(i*254)/nHeight]);
    }

    for (int i=0;i<255;i++)
        delete pBrush[i];
}

void CMainWindow::OnButtonClicked1()
{
    flag=TRUE;
    TextBox1.GetWindowText(ipaddress);
    TextBox1.EnableWindow(FALSE);
    sock2=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
    cli.sin_addr.s_addr=inet_addr(ipaddress);
    cli.sin_family=AF_INET;
    cli.sin_port=5000;
    ListBox2.AddString(CString("对方IP:")+ipaddress);

    AfxBeginThread(&Thread2,0);
}

void CMainWindow::OnButtonClicked2()
{
    CString buff;
    TextBox2.GetWindowText(buff);
    send(sock2,strIP+CString(":")+buff,strIP.GetLength()+buff.GetLength()+1,0);
    TextBox2.SetWindowText("");
}

void CMainWindow::OnButtonClicked3()
{
    CFileDialog dialog(true,"*.*");
    if (dialog.DoModal()==IDOK)
    {
        scount=0;
        count=1;
        SourceFileName=dialog.GetPathName();

        sock3=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
        fcli.sin_addr.s_addr=inet_addr(ipaddress);
        fcli.sin_family=AF_INET;
        fcli.sin_port=8005;

        AfxBeginThread(&Thread3,0);
    }
}

UINT Thread4(LPVOID wParam)
{
    CFileException ex;
    CString FileSize;

    while(TRUE)
    {
        int m_Addlem;
        m_Addlem=sizeof(fserv);

        msock2=accept(sock4,(sockaddr*)&fserv,&m_Addlem);
        char buff[1024]="";
        if (msock2!=INVALID_SOCKET)
        {
            ListBox1.AddString("接受成功");
            recv(msock2,buff,sizeof(buff),0);
            ListBox1.AddString(buff);
        }
        DestFileName="C:\\template.tmp";
        destFile.Open((LPCTSTR)DestFileName, CFile::modeWrite | CFile::modeCreate, &ex);

        buff1=new BYTE[4096];
        DWORD dwRead=0;
        //destFile.Seek(count,CFile::begin);
        count=0;
        do{
            dwRead=recv(msock2,(char *)buff1,4096,0);
            count+=4096;
            destFile.Write(buff1,dwRead);
        }while (dwRead>0);
        destFile.Close();

        FileSize.Format("%d",count);
        ListBox1.AddString(CString("文件大小:")+CString(buff).Right(CString(buff).GetLength()-CString(buff).ReverseFind(':')-1)+CString(" 完成传输:")+FileSize);

        CFileDialog dialog(true,"*.*");
        if (dialog.DoModal()==IDOK)
        {
            CString NewDestFileName=dialog.GetPathName();
            destFile.Rename(DestFileName,NewDestFileName);
        }
    }

    closesocket(sock4);
    closesocket(msock2);
    AfxEndThread(0);
    return 0;
}

UINT Thread3(LPVOID wParam)
{
    CString FileSize;
    CFileException ex;
    sourceFile.Open(SourceFileName,CFile::modeRead, &ex);
    FileSize.Format("%d",sourceFile.GetLength());
   
    if (connect(sock3,(sockaddr*)&fcli,sizeof(fcli))!=INVALID_SOCKET)
    {
        ListBox1.AddString("连接成功");
        send(sock3,SourceFileName+CString(" :")+FileSize,SourceFileName.GetLength()+FileSize.GetLength()+2,0);
    }

    buff2=new BYTE[4096];
    DWORD dwRead=0;
    scount=0;
    //sourceFile.Seek(scount,CFile::begin);
    do{
        dwRead = sourceFile.Read(buff2,4096);
        scount+=4096;
        send(sock3,(char*)buff2,dwRead,0);
    }while (dwRead > 0);

    sourceFile.Close();
    closesocket(sock3);
    AfxEndThread(0);
    return 0;
}

UINT Thread2(LPVOID wParam)
{
    if (connect(sock2,(sockaddr*)&cli,sizeof(cli))!=INVALID_SOCKET)
    {
        ConnectNum++;
        if (ConnectNum<2)
        {
            ListBox1.AddString("连接成功");
            send(sock2,CString("#")+strIP,strIP.GetLength()+1,0);
        }
    }

    while(TRUE)
    {
        char buff[1024]="";
        //recv(sock2,buff,1024,0);
        //ListBox1.AddString(buff);
    }
    closesocket(sock2);
    AfxBeginThread(0);
    return 0;
}

UINT Thread1(LPVOID wParam)
{
    int i=0;
    while(TRUE)
    {
        int m_Addlem;
        m_Addlem=sizeof(serv);
        msock1=accept(sock1,(sockaddr*)&serv,&m_Addlem);
        if (msock1!=INVALID_SOCKET)
        {
            ListBox1.AddString("接受成功");
        }

        while (TRUE)
        {
            char buff[1024]="";
            recv(msock1,buff,1024,0);
            if (CString(buff).Left(1)=='#' && strlen(buff)>0)
            {
                i++;
                if (i<2 && !flag)
                {
                    TextBox1.SetWindowText(CString(buff).Right(CString(buff).GetLength()-CString(buff).Find('#')-1));
                    ::SendMessage(Button1.m_hWnd,BM_CLICK, NULL, NULL);
                }
            }
            else if (strlen(buff)>0)
            {
                ListBox1.AddString(buff);
            }
        }
    }
    closesocket(sock1);
    closesocket(msock1);
    AfxEndThread(0);
    return 0;
}

void CMainWindow::CreateSocket()
{
    sock1=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);

    //广播地址
    //serv.sin_addr.s_addr=htonl(INADDR_BROADCAST);
    serv.sin_addr.s_addr=htonl(INADDR_ANY);
    serv.sin_family=AF_INET;
    serv.sin_port=5000;

    if (bind(sock1,(sockaddr*)&serv,sizeof(serv)))
    {
        ListBox1.AddString("----文本服务器创建失败----");
        return;
    }
    else
    {
        ListBox1.AddString("----文本服务器创建成功----");
        listen(sock1,5);
        AfxBeginThread(&Thread1,0);
    }
}

void CMainWindow::CreateFileSocket()
{
    sock4=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);

    fserv.sin_addr.s_addr=htonl(INADDR_ANY);
    fserv.sin_family=AF_INET;
    fserv.sin_port=8005;

    if (bind(sock4,(sockaddr*)&fserv,sizeof(fserv)))
    {
        ListBox1.AddString("----文件服务器创建失败----");
        return;
    }
    else
    {
        ListBox1.AddString("----文件服务器创建成功----");
        listen(sock4,5);
        AfxBeginThread(&Thread4,0);
    }
}

HBRUSH CMainWindow::OnCtlColor(CDC *pDC, CWnd *pWnd,UINT nCtlColor)
{
    HBRUSH br=CFrameWnd::OnCtlColor(pDC,pWnd,nCtlColor);
    if (pWnd->m_hWnd==Label1.m_hWnd)
    {
        pDC->SetBkMode(TRANSPARENT);
        pDC->SetTextColor(RGB(255,255,0));
        return (HBRUSH)::GetStockObject(NULL_BRUSH);
    }
    return br;
}

LRESULT CMainWindow::OnNcHitTest(CPoint point)
{
    LRESULT nHitTest=CFrameWnd::OnNcHitTest(point);
    if (nHitTest==HTCLIENT)
    {
        nHitTest=HTCAPTION;
    }
    return nHitTest;
}

BOOL CMainWindow::DestroyWindow()
{
    /*flag=TRUE;
    sourceFile.Close();
    destFile.Close();
    closesocket(sock3);
    closesocket(sock4);
    closesocket(msock2);

    CFile file;
    CString sizename1;
    CString sizename2;
    file.Open("c:\\record.log",CFile::modeCreate | CFile::modeWrite);
    sizename1.Format("%d",scount);
    sizename2.Format("%d",count);
    file.Write(SourceFileName+CString("#")+sizename1+CString("#")+sizename2,SourceFileName.GetLength()+sizename1.GetLength()+sizename2.GetLength()+2);
    file.Close();*/
    CFrameWnd::DestroyWindow();
    return TRUE;
}
收到的鲜花
  • zhichishengz2010-06-22 22:19 送鲜花  3朵   附言:我很赞同
2010-06-22 21:26
zhichishengz
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-6-20
收藏
得分:0 
非常感谢上面的几位朋友帮忙!!!!!!!!!!!
2010-06-22 22:17
快速回复:求助:使用 MFC 开发图形界面程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.079499 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved