2个回答
展开全部
我这个比较简单的 你可以看下!!
#include "stdafx.h"
#include "tcpclient.h"
#include "tcpclientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
csocket clconect;
CString clientname;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTcpclientDlg dialog
CTcpclientDlg::CTcpclientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTcpclientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTcpclientDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTcpclientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTcpclientDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTcpclientDlg, CDialog)
//{{AFX_MSG_MAP(CTcpclientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_connet, Onconnet)
ON_BN_CLICKED(IDC_send, Onsend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTcpclientDlg message handlers
BOOL CTcpclientDlg::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
// TODO: Add extra initialization here
SetDlgItemText(IDC_EDIT1,"127.0.0.1");
SetDlgItemText(IDC_EDIT2,"6666");
SetDlgItemText(IDC_EDIT4,"大家好,我来了!!");
SetDlgItemText(IDC_EDIT5,"我就是我");
return TRUE; // return TRUE unless you set the focus to a control
}
void CTcpclientDlg::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 CTcpclientDlg::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 CTcpclientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTcpclientDlg::onrev()
{
CString str,str1;
char ss[100];
for(int j=0;j<100;j++) ss[j]='\0';
clconect.Receive(ss,100);
if(ss!="")
str.Format("%s",ss);
GetDlgItemText(IDC_EDIT3,str1);
str=str1+"\r\n"+str;
SetDlgItemText(IDC_EDIT3,str);
CEdit *ce=((CEdit*)GetDlgItem(IDC_EDIT3));
ce->LineScroll(ce->GetLineCount()-1);;
}
void CTcpclientDlg::oncl()
{
//Beep(2000,100);
MessageBox("失去连接或者找不到服务器");
CWnd *cw=GetDlgItem(IDC_EDIT1);
cw->EnableWindow(true);
cw=GetDlgItem(IDC_EDIT2);
cw->EnableWindow(true);
cw=GetDlgItem(IDC_connet);
cw->EnableWindow(true);
cw=GetDlgItem(IDC_EDIT5);
cw->EnableWindow(true);
}
void CTcpclientDlg::OnClose()
{
int confirm=MessageBox("确定退出吗","请确定",MB_YESNO|MB_ICONQUESTION);
if(confirm==IDYES){
CString enterinfo=clientname+"退出了聊天室";
int len=enterinfo.GetLength();
clconect.Send(enterinfo,len);
CDialog::OnClose();
}
}
void CTcpclientDlg::OnOK(){
CTcpclientDlg::Onsend() ;
};
void CTcpclientDlg::Onconnet()
{
// TODO: Add your control notification handler code here
if(clconect) clconect.Close();
CString ipstr,portstr;
int port;
GetDlgItemText(IDC_EDIT1,ipstr);
GetDlgItemText(IDC_EDIT2,portstr);
GetDlgItemText(IDC_EDIT5,clientname);
portstr.TrimLeft();
portstr.TrimRight();
port=atoi(portstr);
if(port>0&&port<9999){
clconect.Create();
// cslisten.Bind(port);
if(clconect.Connect(ipstr,port))
{
clconect.setp(this);
CWnd *cw=GetDlgItem(IDC_EDIT1);
cw->EnableWindow(false);
cw=GetDlgItem(IDC_EDIT2);
cw->EnableWindow(false);
cw=GetDlgItem(IDC_connet);
cw->EnableWindow(false);
cw=GetDlgItem(IDC_EDIT5);
cw->EnableWindow(false);
SetDlgItemText(IDC_EDIT3,"已设名字"+clientname+"\r\n连接服务器成功");
CString enterinfo=clientname+"进入聊天室";
int len=enterinfo.GetLength();
clconect.Send(enterinfo,len);
}
else{
clconect.Close();
MessageBox("连接失败,请检查IP与端口是否与服务器一致!或者服务器没打开","连接错误",MB_ICONERROR);
}
}
else{
MessageBox("端口或IP输入有误,请重新输入");
}
}
void CTcpclientDlg::Onsend()
{
CString str;
GetDlgItemText(IDC_EDIT4,str);
str=clientname+":"+str;
int len=str.GetLength();
if(clconect.Send(str,len)>1)SetDlgItemText(IDC_EDIT4,"");
}
#include "stdafx.h"
#include "tcpclient.h"
#include "tcpclientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
csocket clconect;
CString clientname;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTcpclientDlg dialog
CTcpclientDlg::CTcpclientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTcpclientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTcpclientDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTcpclientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTcpclientDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTcpclientDlg, CDialog)
//{{AFX_MSG_MAP(CTcpclientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_connet, Onconnet)
ON_BN_CLICKED(IDC_send, Onsend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTcpclientDlg message handlers
BOOL CTcpclientDlg::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
// TODO: Add extra initialization here
SetDlgItemText(IDC_EDIT1,"127.0.0.1");
SetDlgItemText(IDC_EDIT2,"6666");
SetDlgItemText(IDC_EDIT4,"大家好,我来了!!");
SetDlgItemText(IDC_EDIT5,"我就是我");
return TRUE; // return TRUE unless you set the focus to a control
}
void CTcpclientDlg::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 CTcpclientDlg::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 CTcpclientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTcpclientDlg::onrev()
{
CString str,str1;
char ss[100];
for(int j=0;j<100;j++) ss[j]='\0';
clconect.Receive(ss,100);
if(ss!="")
str.Format("%s",ss);
GetDlgItemText(IDC_EDIT3,str1);
str=str1+"\r\n"+str;
SetDlgItemText(IDC_EDIT3,str);
CEdit *ce=((CEdit*)GetDlgItem(IDC_EDIT3));
ce->LineScroll(ce->GetLineCount()-1);;
}
void CTcpclientDlg::oncl()
{
//Beep(2000,100);
MessageBox("失去连接或者找不到服务器");
CWnd *cw=GetDlgItem(IDC_EDIT1);
cw->EnableWindow(true);
cw=GetDlgItem(IDC_EDIT2);
cw->EnableWindow(true);
cw=GetDlgItem(IDC_connet);
cw->EnableWindow(true);
cw=GetDlgItem(IDC_EDIT5);
cw->EnableWindow(true);
}
void CTcpclientDlg::OnClose()
{
int confirm=MessageBox("确定退出吗","请确定",MB_YESNO|MB_ICONQUESTION);
if(confirm==IDYES){
CString enterinfo=clientname+"退出了聊天室";
int len=enterinfo.GetLength();
clconect.Send(enterinfo,len);
CDialog::OnClose();
}
}
void CTcpclientDlg::OnOK(){
CTcpclientDlg::Onsend() ;
};
void CTcpclientDlg::Onconnet()
{
// TODO: Add your control notification handler code here
if(clconect) clconect.Close();
CString ipstr,portstr;
int port;
GetDlgItemText(IDC_EDIT1,ipstr);
GetDlgItemText(IDC_EDIT2,portstr);
GetDlgItemText(IDC_EDIT5,clientname);
portstr.TrimLeft();
portstr.TrimRight();
port=atoi(portstr);
if(port>0&&port<9999){
clconect.Create();
// cslisten.Bind(port);
if(clconect.Connect(ipstr,port))
{
clconect.setp(this);
CWnd *cw=GetDlgItem(IDC_EDIT1);
cw->EnableWindow(false);
cw=GetDlgItem(IDC_EDIT2);
cw->EnableWindow(false);
cw=GetDlgItem(IDC_connet);
cw->EnableWindow(false);
cw=GetDlgItem(IDC_EDIT5);
cw->EnableWindow(false);
SetDlgItemText(IDC_EDIT3,"已设名字"+clientname+"\r\n连接服务器成功");
CString enterinfo=clientname+"进入聊天室";
int len=enterinfo.GetLength();
clconect.Send(enterinfo,len);
}
else{
clconect.Close();
MessageBox("连接失败,请检查IP与端口是否与服务器一致!或者服务器没打开","连接错误",MB_ICONERROR);
}
}
else{
MessageBox("端口或IP输入有误,请重新输入");
}
}
void CTcpclientDlg::Onsend()
{
CString str;
GetDlgItemText(IDC_EDIT4,str);
str=clientname+":"+str;
int len=str.GetLength();
if(clconect.Send(str,len)>1)SetDlgItemText(IDC_EDIT4,"");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询