实验二 网络基础编程实验(3)

2020-04-14 02:14

湖南大学 计算机网络实验报告二

msg += \有客户端连接:\

msg += ::inet_ntoa(m_clientAddr.sin_addr); CtrlState(TRUE);

m_showMsg.SetWindowText(msg);

m_showMsg.LineScroll(m_showMsg.GetLineCount()); } }

break;

case FD_READ:{

char get[500] = {0};

::recv(m_clientSocket, get, sizeof(get) - 1, 0); CString msg;

m_showMsg.GetWindowText(msg); msg += \

msg += ::inet_ntoa(m_clientAddr.sin_addr); msg += \ msg += get;

m_showMsg.SetWindowText(msg);

m_showMsg.LineScroll(m_showMsg.GetLineCount()); } break;

case FD_CLOSE:{

m_clientSocket = INVALID_SOCKET; CString msg;

m_showMsg.GetWindowText(msg); msg += \客户端断开:\ CtrlState(FALSE);

m_showMsg.SetWindowText(msg);

m_showMsg.LineScroll(m_showMsg.GetLineCount()); }

break; } }

void CNetChatServerDlg::OnMaxtextShowMsg() { m_showMsg.SetWindowText(\消息,自动清理!\}

(10)聊天程序客户端 NetChatClientDlg.cpp #include \

#include \#include \#ifdef _DEBUG

#define new DEBUG_NEW #undef THIS_FILE

static char THIS_FILE[] = __FILE__;

11 / 22

湖南大学 计算机网络实验报告二

#endif

// CNetChatClientDlg dialog

CNetChatClientDlg::CNetChatClientDlg(CWnd* pParent /*=NULL*/) : CDialog(CNetChatClientDlg::IDD, pParent) {

//{{AFX_DATA_INIT(CNetChatClientDlg) m_port = 1130; //}}AFX_DATA_INIT

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_clientSocket = INVALID_SOCKET; }

void CNetChatClientDlg::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CNetChatClientDlg)

DDX_Control(pDX, IDC_SHOW_MSG, m_showMsg); DDX_Control(pDX, IDC_EDIT_MSG, m_editMsg); DDX_Control(pDX, IDC_IPADDR, m_ipAddr); DDX_Control(pDX, IDC_PORT, m_portCtrl); DDX_Text(pDX, IDC_PORT, m_port); //}}AFX_DATA_MAP }

BEGIN_MESSAGE_MAP(CNetChatClientDlg, CDialog) //{{AFX_MSG_MAP(CNetChatClientDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_BN_CLICKED(IDC_SEND, OnSend)

ON_BN_CLICKED(IDC_CONNECT, OnConnect) ON_WM_TIMER()

ON_MESSAGE(WM_SOCKET,OnSocket) //}}AFX_MSG_MAP END_MESSAGE_MAP()

BOOL CNetChatClientDlg::OnInitDialog(){ CDialog::OnInitDialog();

SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon m_ipAddr.SetWindowText(\ CtrlState(TRUE);

return TRUE; // return TRUE unless you set the focus to a control }

void CNetChatClientDlg::OnSysCommand(UINT nID, LPARAM lParam){

if ((nID & 0xFFF0) == SC_CLOSE && m_clientSocket != INVALID_SOCKET) OnConnect();

12 / 22

湖南大学 计算机网络实验报告二

CDialog::OnSysCommand(nID, lParam); }

void CNetChatClientDlg::OnPaint(){ if (IsIconic()){

CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 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; dc.DrawIcon(x, y, m_hIcon); } else

CDialog::OnPaint(); }

HCURSOR CNetChatClientDlg::OnQueryDragIcon(){ return (HCURSOR) m_hIcon; }

BOOL CNetChatClientDlg::PreTranslateMessage(MSG* pMsg) { return CDialog::PreTranslateMessage(pMsg); }

void CNetChatClientDlg::OnSend() {

if(m_clientSocket != INVALID_SOCKET){ char send[500] = {0};

m_editMsg.GetWindowText(send, sizeof(send) - 1); int strlength = strlen(send); if(strlength < 1){

MessageBox(\发送内容不能为空!\ return; }

CString msg;

m_showMsg.GetWindowText(msg);

if(::send(m_clientSocket, send, strlength, 0) != SOCKET_ERROR){ msg += \本机: \\r\\n\ msg += send; } else

msg += \本机: 消息发送失败!\ m_showMsg.SetWindowText(msg);

m_showMsg.LineScroll(m_showMsg.GetLineCount()); } }

13 / 22

湖南大学 计算机网络实验报告二

void CNetChatClientDlg::CtrlState(BOOL state){ m_portCtrl.EnableWindow(state); m_ipAddr.EnableWindow(state);

SetDlgItemText(IDC_CONNECT, state == TRUE ? \连接\断开\ GetDlgItem(IDC_SEND)->EnableWindow(!state); m_editMsg.EnableWindow(!state); }

void CNetChatClientDlg::OnConnect() { CString msg;

m_showMsg.GetWindowText(msg);

if(m_clientSocket != INVALID_SOCKET)//已经启动服务器,则停止服务器{ ::closesocket(m_clientSocket); ::WSACleanup(); CtrlState(TRUE);

m_clientSocket = INVALID_SOCKET; msg += \已断开!\ }

else//启动服务器{ UpdateData(TRUE); if(m_port > 65535){

AfxMessageBox(\端口溢出!请重新设定!\ return; }

WSADATA data;

if(::WSAStartup(MAKEWORD(2, 2), &data)){ AfxMessageBox(\启动套接字失败!\ return; }

sockaddr_in addr;

addr.sin_family = AF_INET; addr.sin_port = htons(m_port);

m_ipAddr.GetAddress(addr.sin_addr.S_un.S_un_b.s_b1, addr.sin_addr.S_un.S_un_b.s_b2,

addr.sin_addr.S_un.S_un_b.s_b3, addr.sin_addr.S_un.S_un_b.s_b4); m_clientSocket = ::socket(AF_INET, SOCK_STREAM, 0); if(INVALID_SOCKET == m_clientSocket ||

::WSAAsyncSelect(m_clientSocket, this->m_hWnd, WM_SOCKET, FD_CONNECT|FD_READ|FD_CLOSE) == SOCKET_ERROR){ AfxMessageBox(\连接失败!\ ::WSACleanup();

m_clientSocket = INVALID_SOCKET; return; }

::connect(m_clientSocket, (sockaddr*)&addr, sizeof(addr));

14 / 22

湖南大学 计算机网络实验报告二

SetTimer(1, 4000, NULL); msg += \正在连接...\ }

m_showMsg.SetWindowText(msg);

m_showMsg.LineScroll(m_showMsg.GetLineCount()); }

void CNetChatClientDlg::OnSocket(WPARAM wParam,LPARAM lParam){ switch (lParam){ case FD_CONNECT:{ KillTimer(1); CtrlState(FALSE); CString msg;

m_showMsg.GetWindowText(msg); msg += \连接成功!\

m_showMsg.SetWindowText(msg);

m_showMsg.LineScroll(m_showMsg.GetLineCount()); }

break;

case FD_READ:{

char get[500] = {0};

::recv(m_clientSocket, get, sizeof(get) - 1, 0); CString msg;

m_showMsg.GetWindowText(msg); msg += \对方:\ msg += get;

m_showMsg.SetWindowText(msg);

m_showMsg.LineScroll(m_showMsg.GetLineCount()); } break;

case FD_CLOSE:{

CtrlState(TRUE); CString msg;

m_showMsg.GetWindowText(msg); msg += \连接断开!\

m_showMsg.SetWindowText(msg);

m_showMsg.LineScroll(m_showMsg.GetLineCount()); m_clientSocket = INVALID_SOCKET; }

break; } }

void CNetChatClientDlg::OnTimer(UINT nIDEvent){

// TODO: Add your message handler code here and/or call default if(nIDEvent == 1){

15 / 22


实验二 网络基础编程实验(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:七年级英语下册Unit3Morepractice练习(新版)牛津深圳版

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: