⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 workingsock.cpp

📁 一个聊天的程序不错
💻 CPP
字号:
// workingSock.cpp : implementation file
//

#include "stdafx.h"
#include "聊天程序.h"
#include "workingSock.h"

#include "聊天程序Dlg.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// workingSock

workingSock::workingSock()
{
}

workingSock::~workingSock()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(workingSock, CAsyncSocket)
	//{{AFX_MSG_MAP(workingSock)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// workingSock member functions

void workingSock::OnReceive(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
//	AfxMessageBox("receiving",MB_OK);
	recvNum=0;
	recvNum=Receive(recvBuf,4096);//,MSG_PEEK);
	if(recvNum!=0)
	{
        CWinApp* thisApp;        //获得进程的句柄
		thisApp=AfxGetApp();
	
        CMyDlg *dialWnd;      //获得对话框的句柄
	    dialWnd=(CMyDlg *)thisApp->GetMainWnd();
	    CListBox  *allMes;
        allMes=(CListBox  *)dialWnd->GetDlgItem(IDC_LIST1);//获得list box 句柄
	    recvBuf[recvNum]='\0';
        allMes->AddString(recvBuf);      //将接收到的消息显示在LIST BOX中
        allMes->UpdateData(FALSE);
	    memset(recvBuf,0,4096);//置0
	}
/*
	int length;
	length=dialWnd->m_listAll.GetLength();//从后面插入
	recvBuf[recvNum]='\0';   //使之成为字符串
	dialWnd->m_listAll.Insert(length,recvBuf);
*/		


	AsyncSelect(FD_READ);
	CAsyncSocket::OnReceive(nErrorCode);
}

void workingSock::OnSend(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
        
	CWinApp* thisApp;        //获得进程的句柄
		thisApp=AfxGetApp();
	
    CMyDlg *dialWnd;      //获得对话框的句柄
	dialWnd=(CMyDlg *)thisApp->GetMainWnd();
//	AfxMessageBox("SENDING",MB_OK); 
    if(dialWnd->m_message!="")
	{
		int length;
		length=dialWnd->m_message.GetLength();
		memcpy(recvBuf,dialWnd->m_message.GetBuffer(length),length);
		dialWnd->m_message.ReleaseBuffer();
		Send(recvBuf,length,0);
  
	}
dialWnd->m_message="";
dialWnd->UpdateData(FALSE);
	AsyncSelect(FD_READ);
	CAsyncSocket::OnSend(nErrorCode);
}

void workingSock::OnConnect(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
    this->AsyncSelect(FD_READ);
//	AfxMessageBox("client :Connection is OK",MB_OK);

	{
	    CWinApp* thisApp;        //获得进程的句柄
		thisApp=AfxGetApp();
	
        CMyDlg *dialWnd;      //获得对话框的句柄
	    dialWnd=(CMyDlg *)thisApp->GetMainWnd();
	    CWnd  *sendBut,*mesEdit;
        sendBut=dialWnd->GetDlgItem(IDC_BUTTON3);
		mesEdit=dialWnd->GetDlgItem(IDC_EDIT3);
        sendBut->EnableWindow(TRUE); 
		mesEdit->EnableWindow(TRUE);//使可以输入信息
	}
	CAsyncSocket::OnConnect(nErrorCode);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -