📄 clientdlg.cpp
字号:
// clientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "client.h"
#include "ConfigDlg.h"
#include "clientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientDlg)
m_sShowText = _T("");
m_sServerIp = _T("");
m_sServerPort = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientDlg)
DDX_Text(pDX, IDC_SHOWTEXT_EDIT, m_sShowText);
DDX_Text(pDX, IDC_SERVERIP_EDIT, m_sServerIp);
DDX_Text(pDX, IDC_SERVERPORT_EDIT, m_sServerPort);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
//{{AFX_MSG_MAP(CClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CONTOSER_BUTTON, OnContoserButton)
ON_BN_CLICKED(IDC_CONFIG_BUTTON, OnConfigButton)
ON_BN_CLICKED(IDC_SENDER_BUTTON, OnSenderButton)
ON_BN_CLICKED(IDC_END_BUTTON, OnEndButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers
BOOL CClientDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CClientDlg::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 CClientDlg::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 CClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//连接到服务器
void CClientDlg::OnContoserButton()
{
// TODO: Add your control notification handler code here
unsigned short port;//端口
CString addstr;
int ret;
WSADATA wsaData;
if (WSAStartup(0x101,&wsaData))
AfxMessageBox("WSAStartup error! \n");
cli_sock=socket(AF_INET,SOCK_STREAM,0);
if (cli_sock==INVALID_SOCKET)
{
AfxMessageBox("Socket() error!\n");return;
}
//从编辑框得到用户的数据
GetDlgItem(IDC_SERVERPORT_EDIT)->GetWindowText(m_sServerPort);
port=atoi(m_sServerPort);//字符串转换成整形
GetDlgItem(IDC_SERVERIP_EDIT)->GetWindowText(m_sServerIp);
seraddr.sin_family=AF_INET;
seraddr.sin_port=htons(port);
seraddr.sin_addr.S_un.S_addr=inet_addr(m_sServerIp);
ret=connect(cli_sock,(struct sockaddr *)&seraddr,sizeof(seraddr));
if (ret==SOCKET_ERROR)
{
AfxMessageBox("Connect to server error!\n Please input correct IP and PORT!\n");
}
else
{
addstr+="Connect server sucessfully!\n";
Write_to_ShowText(addstr);
}
}
//将要显示的信息写入显示框中
void CClientDlg::Write_to_ShowText(CString adds)
{
CEdit *output=NULL;
m_sShowText+=adds;
m_sShowText+="\r\n";
GetDlgItem(IDC_SHOWTEXT_EDIT)->SetWindowText(m_sShowText);
output=(CEdit*)GetDlgItem(IDC_SHOWTEXT_EDIT);
//跟踪滚动条的位置
output->LineScroll(output->GetLineCount ());
return;
}
//设置链路参数
void CClientDlg::OnConfigButton()
{
// TODO: Add your control notification handler code here
CConfigDlg configDlg;
configDlg.DoModal();
ilost_percent=configDlg.ilost;
iwrong_percent=configDlg.iwrong;
timeout_interval=configDlg.itimeout;
repeatnum=configDlg.irepeatnumber;
}
//发送端的工作程序
void CClientDlg::OnSenderButton()
{
// TODO: Add your control notification handler code here
CString tempstr,addstr;
seq_nr next_frame_to_send;
frame s,r;
packet buffer;
event_type event;
int recvnum,j;
Cprotocol Pro_stopwait;
bool control=true;
j=0;
next_frame_to_send=0;//初始化发送状态变量
Pro_stopwait.from_network_layer(&buffer);//从网络层得到要发送的数据
s.ack=1;
//构造一个要传输的数据帧
s.kind=data;
s.seq=next_frame_to_send;
s.ack=1-s.ack;
s.info=buffer;
s.checksum=Pro_stopwait.checksum_mat(s.kind,s.seq,s.info);
//要发送的帧
addstr="the data that will be send:";
Write_to_ShowText(addstr);
addstr=s.info.data;
Write_to_ShowText(addstr);
//要发送的帧的序号
addstr="发送的序号是:";
Write_to_ShowText(addstr);
addstr.Format("%d",s.seq);
Write_to_ShowText(addstr);
do
{
Pro_stopwait.to_physical_layer(cli_sock,s,iwrong_percent,ilost_percent);
Pro_stopwait.wait_for_event(cli_sock,event,timeout_interval);
//有数据到达时
if (event==frame_arrival)
{
recvnum=Pro_stopwait.from_physical_layer(cli_sock,r);
//检查对方是否工作正常
if(recvnum==-1)
{
addstr="the receiver has closed! Data send will be STOP!";
Write_to_ShowText(addstr);
break;
}
//收到确认帧
if(r.kind==ack)
{
addstr="a ACK arrival!";
Write_to_ShowText(addstr);
addstr="the number of ACK is:";
Write_to_ShowText(addstr);
addstr.Format("%d",r.ack);
Write_to_ShowText(addstr);
addstr="___________________________";
Write_to_ShowText(addstr);
//收到接收方发来的特殊帧的确认,表示发送方不再发送数据帧
if (r.checksum=='111')
{
addstr="发送数据将会停止,并和接收端断开连接!!";
Write_to_ShowText(addstr);
control=false;
shutdown(cli_sock,0);
}
else
{
Pro_stopwait.from_network_layer(&buffer);
//改变发送状态变量
inc(next_frame_to_send);
//构造一个要传输的数据帧
s.kind=data;
s.seq=next_frame_to_send;
s.ack=1-s.ack;
s.info=buffer;
s.checksum=Pro_stopwait.checksum_mat(s.kind,s.seq,s.info);
//要发送的数据
addstr="the data that will be send:";
Write_to_ShowText(addstr);
addstr=s.info.data;
Write_to_ShowText(addstr);
//要发送的数据的序号
addstr="发送的序号是:";
Write_to_ShowText(addstr);
addstr.Format("%d",s.seq);
Write_to_ShowText(addstr);
}
}
else //收到否认帧
{
addstr="a NAK arrival!";
Write_to_ShowText(addstr);
//收到否认帧时,则重发该帧
s.kind=data;
s.seq=next_frame_to_send;
s.ack=1-s.ack;
s.info=buffer; //构造一个要传输的数据帧
s.checksum=Pro_stopwait.checksum_mat(s.kind,s.seq,s.info);
addstr="the data that will be send:";
Write_to_ShowText(addstr);
addstr=s.info.data;
Write_to_ShowText(addstr);
addstr="发送的序号是:";
Write_to_ShowText(addstr);
addstr.Format("%d",s.seq);
Write_to_ShowText(addstr);
}
}//end if (event==frame_arrival)
else //超时
{
if (j<=repeatnum)
{
addstr="occur timeout event!";
Write_to_ShowText(addstr);
}
else
{
shutdown(cli_sock,0);
}
j++;
}
}while(control);//end while循环结束
shutdown(cli_sock,0);
}
//关闭窗口
void CClientDlg::OnEndButton()
{
// TODO: Add your control notification handler code here
closesocket(cli_sock);
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -