// MyNetChatDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyNetChat.h"
#include "MyNetChatDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_NETICON WM_USER+100
///////////////////////////////////////////////////////////////////////////////////
SOCKET m_socket,m_hSocket;//host sock
sockaddr_in m_addr;//host address
sockaddr_in m_raddr;//host recive client address
sockaddr_in m_caddr;
char *message=NULL;
char *name=NULL;
int nItem;
CString changstr;
bool exitmsg=FALSE;
CString strname;
UINT AcceptThread(LPVOID lpvoid);
//CWinThread *pAccept;
SOCKET m_cSocket;//client sock
NOTIFYICONDATA tnd; //add icon
bool ShowApp=FALSE;
/////////////////////////////////////////////////////////////////////////////
// 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)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyNetChatDlg dialog
CMyNetChatDlg::CMyNetChatDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyNetChatDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyNetChatDlg)
m_sendstr = _T("");
m_showstr = _T("");
m_name = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyNetChatDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyNetChatDlg)
DDX_Control(pDX, IDC_EDIT1, m_showname);
DDX_Control(pDX, IDC_USER, m_List);
DDX_Control(pDX, IDC_SHOWSTR, m_show);
DDX_Control(pDX, IDC_SENDSTR, m_send);
DDX_Control(pDX, IDC_SENDED,m_SD);
DDX_Control(pDX, IDC_FINDUSER,m_FU);
DDX_Text(pDX, IDC_SENDSTR, m_sendstr);
DDX_Text(pDX, IDC_SHOWSTR, m_showstr);
DDX_Text(pDX, IDC_EDIT1, m_name);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyNetChatDlg, CDialog)
//{{AFX_MSG_MAP(CMyNetChatDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(ID_CREATHOST, OnCreathost)
ON_COMMAND(ID_EXITCHAT, OnExitchat)
ON_NOTIFY(NM_DBLCLK, IDC_USER, OnDblclkUser)
ON_WM_CREATE()
ON_COMMAND(ID_EXITWL, OnExitwl)
ON_COMMAND(ID_SHOWAPP, OnSetSever)
ON_BN_CLICKED(IDC_SENDED,OnSended)
ON_BN_CLICKED(IDC_FINDUSER, OnFinduser)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_NETICON,AddIcon)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyNetChatDlg message handlers
BOOL CMyNetChatDlg::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
char localname[64];
GetName(localname);
changstr=localname;
//////////////////////////////////
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyNetChatDlg::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 CMyNetChatDlg::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.
UINT AcceptThread(LPVOID lpvoid)
{
int Addrlength=sizeof(m_raddr);
m_socket=accept(m_hSocket,(LPSOCKADDR)&m_raddr,&Addrlength);
if(m_socket==INVALID_SOCKET)
{
closesocket(m_hSocket);
m_socket=NULL;
AfxMessageBox("Accept Error");
return 0;
}
AfxBeginThread(AcceptThread,NULL);
char buff[1024];
int ret=recv(m_socket,buff,1024,0);
if(ret==0||ret==SOCKET_ERROR)
{
closesocket(m_socket);
AfxMessageBox("recv bad");
return 0;
}
if(buff!=NULL)
{
int msgleng=0;
int nameleng=0;
int i;
nameleng=buff[0];
name=new char[nameleng+1];
for(i=0;i<nameleng;i++)
{name[i]=buff[i+1];}
msgleng=buff[nameleng+1];
message=new char[msgleng+1];
for(i=0;i<msgleng;i++)
{
message[i]=buff[i+2+nameleng];
}
name[nameleng]='\0';
message[msgleng]='\0';
CMyNetChatDlg* SM=(CMyNetChatDlg*)(AfxGetApp()->m_pMainWnd);
SM->ShowRecvData();
}
return 0;
}
HCURSOR CMyNetChatDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyNetChatDlg::OnCreathost() //create host
{
if(m_hSocket!=NULL)
{
closesocket(m_hSocket);
m_hSocket=NULL;
}
if(m_hSocket==NULL)
{
m_hSocket=socket(AF_INET,SOCK_STREAM,0);
ASSERT(m_hSocket!=NULL);
}
UpdateData(TRUE);
m_addr.sin_addr.S_un.S_addr=INADDR_ANY;
m_addr.sin_family=AF_INET;
m_addr.sin_port=htons(52428);
int ret=0;
ret=bind(m_hSocket,(LPSOCKADDR)&m_addr,sizeof(m_addr));
if(ret==SOCKET_ERROR)
{
closesocket(m_hSocket);
AfxMessageBox("Bind Error");
return ;
}
ret=listen(m_hSocket,12);
if(ret==SOCKET_ERROR)
{
closesocket(m_hSocket);
AfxMessageBox("Listen Error");
return ;
}
AfxBeginThread(AcceptThread,NULL);
}
void CMyNetChatDlg::OnExitchat()
{
// EXit Application and all thread;
exitmsg=TRUE;
Shell_NotifyIcon(NIM_DELETE,&tnd);
CDialog::OnOK();
}
void CMyNetChatDlg::GetName(char Temp[64])
{
gethostname(Temp,64);
}
CString CMyNetChatDlg::GetIP(CString username)
{
CString hostaddr;
struct hostent *hostname=gethostbyname(username);
for(int i=0;hostname!=NULL&&hostname->h_addr_list[i];i++)
{
hostaddr=inet_ntoa(*(struct in_addr *)hostname->h_addr_list[i]);
}
return hostaddr;
}
void CMyNetChatDlg::OnSended()
{
// send message to host
m_showname.GetWindowText(m_name);
if(m_name.IsEmpty())
{
AfxMessageBox("请选择发送用户");
return;
}
strname=GetIP(m_name);
if(m_cSocket!=NULL)
{
closesocket(m_cSocket);
m_cSocket=NULL;
}
if(m_cSocket==NULL)
{
m_cSocket=socket(AF_INET,SOCK_STREAM,0);
ASSERT(m_cSocket!=NULL);
}
UpdateData(TRUE);
m_caddr.sin_addr.
没有合适的资源?快使用搜索试试~ 我知道了~
一个基于局域网的聊天软件
共27个文件
h:8个
cpp:6个
opt:1个
需积分: 10 0 下载量 68 浏览量
2011-05-18
17:16:39
上传
评论
收藏 72KB ZIP 举报
温馨提示
一个简单实用的局域网聊天软件,分享给大家,从别处分享来的
资源推荐
资源详情
资源评论
收起资源包目录
一个基于局域网的聊天程序.zip (27个子文件)
一个基于局域网的聊天程序
使用说明.CHM 49KB
www.cnzz.cn
MyNetChat.opt 56KB
StdAfx.cpp 211B
MyNetChat.h 1KB
XPButton.cpp 6KB
HoverEdit.h 1KB
MyNetChat.suo 10KB
MyNetChat.dsp 4KB
DialogUser.cpp 2KB
readme.htm 223B
MyNetChatDlg.h 2KB
MyNetChat.cpp 2KB
DialogUser.h 1KB
MyNetChat.sln 918B
MyNetChatDlg.cpp 13KB
XPButton.h 2KB
MyNetChat.clw 2KB
MyNetChat.rc 7KB
MyNetChat.dsw 543B
StdAfx.h 1KB
MyNetChat.plg 252B
Resource.h 1KB
MyNetChat.vcproj 5KB
HoverEdit.cpp 1KB
res
MyNetChat.ico 1KB
MyNetChat.rc2 401B
TrackControl.h 2KB
共 27 条
- 1
资源评论
abc819492642
- 粉丝: 24
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功