把握命运,追逐梦想

对自己所做的事要有兴趣,同时还要能够坚持不懈

统计

留言簿(1)

阅读排行榜

评论排行榜

一个简单的网络小程序

//tcpsever.cpp
#include"targetver.h"
#define WIN32_LEAN_AND_MEAN
#include
<windows.h>
#include
<commctrl.h>
#include
<tchar.h>
#include
<winsock2.h>
#include
"resource.h"
#include
<string>



HINSTANCE g_hInst 
= NULL;                                // 当前实例
HWND g_hWndDLG = NULL;
HANDLE g_hIconSm
= NULL;
HANDLE g_hIconBg 
= NULL;
HANDLE g_hEnInfo 
= NULL;
SOCKET  g_sServer 
= INVALID_SOCKET;
SOCKET g_sClient1 
= INVALID_SOCKET;
SOCKET g_sClient2 
= INVALID_SOCKET;
std::wstring wstrInfo;
TCHAR  DataBuffer[
256];

// 此代码模块中包含的函数的前向声明:
BOOL InitWindow();
BOOL InitSock();
void AcceptClient();
void RecvInfo();
void CleanupSock();
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     
int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    g_hInst 
= hInstance;

    
if(!InitSock())
    
{
        
return FALSE;
    }


    
if(!InitWindow())
    
{
        
return FALSE;
    }


    MSG msg 
= {0};

    
// 主消息循环:
    while (msg.message != WM_QUIT)
    
{
        
if(PeekMessage(&msg, g_hWndDLG, 00,PM_REMOVE))
        
{
            
if(IsDialogMessage(g_hWndDLG,&msg))
            
{
                wstrInfo.append(L
"i");
            }

            
else
            

                TranslateMessage(
&msg); 
                DispatchMessage(
&msg); 
            }

        }

        
else
        
{
            
/*AcceptClient();
            RecvInfo();
*/

        }


    }


    CleanupSock();
    
return (int) msg.wParam;
}




// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    
switch (message)
    
{
        
//对于WM_INITDIALOG消息,必须要返回TRUE,无论是否处理这个消息,系统都会处理这个消息的.
    case WM_INITDIALOG:
        
//todo:设置初始化时对话框,以及控件的初始状态值
        
//这里没有办法seticon,因为此时对话框句柄还没得到,无法指明窗口,似乎需要窗口句柄的都无法在这里初始化
        
//晕,犯糊涂,上面说错了,消息发送的时候就带着窗口句柄呢!
        SendMessage(hDlg,WM_SETICON,ICON_SMALL,(LPARAM)g_hIconSm);
        SendMessage(hDlg,WM_SETICON,ICON_BIG,(LPARAM)g_hIconBg);
        
return (INT_PTR)TRUE;

    
case WM_COMMAND:
        
switch (LOWORD(wParam))
        
{
        
case IDCANCEL:
            DestroyWindow(g_hWndDLG);
            
return (INT_PTR)TRUE;
            
break;
        }

        
break;
    
case WM_CLOSE:
        MessageBox(hDlg,L
"X按钮阻止使用",L"无效",MB_ICONINFORMATION);
        
return (INT_PTR)TRUE;
        
break;
    
case WM_DESTROY:
        PostQuitMessage(
0);
        
return (INT_PTR)TRUE;
        
break;
    }

    
return (INT_PTR)FALSE;
}




BOOL InitWindow()
{

    
//使用LoadImage有很多好处,它可以根据参数中的宽高来从具有多个image的文件中选择合适大小的image
    
//尽量避免调用LoadIcon,几次试验发现,多次调用LoadIcon导致默认图标被改变的问题
    g_hIconSm = LoadImage(g_hInst,MAKEINTRESOURCE(IDI_ICON1),IMAGE_ICON,16
        
16,LR_DEFAULTCOLOR);
    g_hIconBg 
= LoadImage(g_hInst,MAKEINTRESOURCE(IDI_ICON1),IMAGE_ICON,32
        
32,LR_DEFAULTCOLOR);

    
    g_hWndDLG 
= CreateDialog(g_hInst, MAKEINTRESOURCE(IDD_DIALOGSERVER), NULL, About);

    
if(g_hWndDLG == NULL)
    
{
        
return FALSE;
    }


    ShowWindow(g_hWndDLG,SW_SHOW);

    
return TRUE;
}


BOOL InitSock()
{
    WSADATA wsaData;

    
if((WSAStartup(MAKEWORD(2,2),&wsaData)) != 0)
    
{
        MessageBox(g_hWndDLG,L
"网络模块加载失败,程序马上退出",L"失败",MB_ICONINFORMATION);
        
return FALSE;
    }


    
if((g_sServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))== INVALID_SOCKET)
    
{
        MessageBox(g_hWndDLG,L
"套接字初始化失败,程序马上退出",L"失败",MB_ICONINFORMATION);
        WSACleanup();
        
return FALSE;
    }


    SOCKADDR_IN serverAddr;
    
int port = 5150;

    serverAddr.sin_family 
= AF_INET;
    serverAddr.sin_port 
= htons(port);    
    serverAddr.sin_addr.s_addr 
= htonl(INADDR_ANY);

    
if (bind(g_sServer, (SOCKADDR *)&serverAddr, sizeof(serverAddr))  == SOCKET_ERROR)
    
{
        MessageBox(g_hWndDLG,L
"套接字绑定失败,程序马上退出",L"失败",MB_ICONINFORMATION);
        closesocket(g_sServer);
        WSACleanup();
        
return FALSE;
    }


    
if(listen(g_sServer, 2== SOCKET_ERROR)
    
{
        MessageBox(g_hWndDLG,L
"监听请求失败,程序马上退出",L"失败",MB_ICONINFORMATION);
        closesocket(g_sServer);
        WSACleanup();
        
return FALSE;
    }


    u_long iMode 
= 0;
    ioctlsocket(g_sServer, FIONBIO, 
&iMode);
    
return TRUE;
}


void AcceptClient()
{
    SOCKADDR_IN   clientAddr;
    
int    clientAddrLen;
    
if(g_sClient1 == INVALID_SOCKET)
    
{
        
if ((g_sClient1 = accept(g_sServer, (SOCKADDR *&clientAddr, &clientAddrLen)) == INVALID_SOCKET)
        
{
            
if( WSAEWOULDBLOCK != WSAGetLastError() )
            
{
                wstrInfo.append(L
"\n接受请求失败");
                SetDlgItemText(g_hWndDLG,IDC_ENINFO,wstrInfo.c_str());
            }

            
else
            
{
                
return;
            }

        }

        
else
        
{
            wstrInfo.append(L
"\n接受请求成功");
            SetDlgItemText(g_hWndDLG,IDC_ENINFO,wstrInfo.c_str());
        }

    }

    
if(g_sClient2 == INVALID_SOCKET)
    
{
        
if ((g_sClient2 = accept(g_sServer, (SOCKADDR *&clientAddr, &clientAddrLen)) == INVALID_SOCKET)
        
{
            
if( WSAEWOULDBLOCK != WSAGetLastError() )
            
{
                wstrInfo.append(L
"\n接受请求失败");
                SetDlgItemText(g_hWndDLG,IDC_ENINFO,wstrInfo.c_str());
            }

            
else
            
{
                
return;
            }

        }

        
else
        
{
            wstrInfo.append(L
"\n接受请求成功");
            SetDlgItemText(g_hWndDLG,IDC_ENINFO,wstrInfo.c_str());
        }

    }

}


void RecvInfo()
{
    
int ret = 0;
    
if(g_sClient1 != SOCKET_ERROR)
    
{
        
if (ret = recv(g_sClient1, (char*)DataBuffer, sizeof(DataBuffer), 0== SOCKET_ERROR)
        
{
            wstrInfo.append(L
"\nrecv失败");
            SetDlgItemText(g_hWndDLG,IDC_ENINFO,wstrInfo.c_str());

        }

        
else if(ret == 0)
        
{
            closesocket(g_sClient1);
            g_sClient1 
= INVALID_SOCKET;
            wstrInfo.append(L
"\n连接断开");
            SetDlgItemText(g_hWndDLG,IDC_ENINFO,wstrInfo.c_str());
        }

        
else
        
{
            wstrInfo.append(L
"\n");
            wstrInfo.append(DataBuffer,ret
/2);
            SetDlgItemText(g_hWndDLG,IDC_ENINFO,wstrInfo.c_str());
        }

    }


    
if(g_sClient2 != SOCKET_ERROR)
    
{
        
if (ret = recv(g_sClient2, (char*)DataBuffer, sizeof(DataBuffer), 0== SOCKET_ERROR)
        
{
            wstrInfo.append(L
"\nrecv失败");
            SetDlgItemText(g_hWndDLG,IDC_ENINFO,wstrInfo.c_str());
        }

        
else if(ret == 0)
        
{
            closesocket(g_sClient2);
            g_sClient2 
= INVALID_SOCKET;
            wstrInfo.append(L
"\n连接断开");
            SetDlgItemText(g_hWndDLG,IDC_ENINFO,wstrInfo.c_str());
        }

        
else
        
{
            wstrInfo.append(L
"\n");
            wstrInfo.append(DataBuffer,ret
/2);
            SetDlgItemText(g_hWndDLG,IDC_ENINFO,wstrInfo.c_str());
        }

    }


}


void CleanupSock()
{
    
if(g_sClient1 != INVALID_SOCKET)
    
{
        closesocket(g_sClient1);
        g_sClient1 
= INVALID_SOCKET;
    }

    
if(g_sClient2 != INVALID_SOCKET)
    
{
        closesocket(g_sClient2);
        g_sClient2 
= INVALID_SOCKET;
    }

    
if(g_sServer != INVALID_SOCKET )
    
{
        closesocket(g_sServer);
        g_sServer 
= INVALID_SOCKET;
    }

    WSACleanup();
}

posted on 2009-07-30 07:22 把握命运 阅读(165) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理