BOOL CSRVDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
SetTimer(1,1000,NULL);
// TODO: Add extra initialization here
//初始化
WORD wVersionRequest;
WSADATA ws;
wVersionRequest=MAKEWORD(1,1);
int err=WSAStartup(wVersionRequest,&ws);
if(err!=0)
return true;
if(LOBYTE(ws.wVersion)!=1||HIBYTE(ws.wVersion)!=1)
{
WSACleanup();
return true;
}
srvListener=socket(AF_INET,SOCK_STREAM,0);
ad.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
ad.sin_family=AF_INET;
ad.sin_port=htons(6000);
bind(srvListener,(SOCKADDR*)&ad,sizeof(SOCKADDR));
len=sizeof(SOCKADDR);
listen(srvListener,5);
return TRUE; // return TRUE unless you set the focus to a control
}
void CSRVDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSRVDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSRVDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSRVDlg::OnButton2()
//就是当点允许连接是就没响应,OnButton2()就是允许连接的处理函数{
// TODO: Add your control notification handler code here
while(1)
{
srvUse=accept(srvListener,(SOCKADDR*)&use,&len);
if(srvUse!=INVALID_SOCKET )
{
return;
}
}
}
void CSRVDlg::OnButton1()
//这个事发送按钮的处理函数
{
// TODO: Add your control notification handler code here
char *a=NULL;
GetDlgItemText(IDC_EDIT2,a,1000);
send(srvUse,a,1000,0);
}
void CSRVDlg::OnTimer(UINT nIDEvent)//这个是服务器收到的处理函数,我用定时器来处理的
{
// TODO: Add your message handler code here and/or call default
char *b=NULL;
recv(srvUse,b,1000,0);
SetDlgItemText(IDC_EDIT3,b);
CDialog::OnTimer(nIDEvent);
}