计算机网络程序设计实验6.doc
文本预览下载声明
北京联合大学 信息学院
《》
实 验 序 号:
姓 名:
学 号:
班 级:
专 业:
实验六、基于 MFC 的 UDP 协议的网络程序调试
服务器代码
void CUdpSDlg::OnSocket(WPARAM wParam,LPARAM lParam)
{
char cs[100]=; //定义数据缓冲区
if(lParam==FD_READ) //如果是套接字读取时间
{
CString num=; //定义字符串变量
int n=sizeof(addr1);
::recvfrom(s,cs,100,0,(sockaddr*)addr1,n); //接收客户端信息
GetDlgItem(IDC_EDIT1)-GetWindowText(num); //获取消息框中的内容
num+=\r\n 客户端说:; //添加回车换行符
num+=(LPTSTR)cs; //将接收到的数据转换为字符串
GetDlgItem(IDC_EDIT1)-SetWindowText(num); //设置消息框内容
}
}
BOOL CUdpSDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the applications 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
WORD version = MAKEWORD(2,0);
WSADATA wsadata;
if(WSAStartup(version,wsadata))
{
MessageBox(加载Winsock dll 失败);
return true;
}
addr.sin_family=AF_INET;
addr.sin_port=htons(8088);
addr.sin_addr.S_un.S_addr=INADDR_ANY;
s=::socket(AF_INET,SOCK_DGRAM,0);
::bind(s,(sockaddr*)addr,sizeof(addr));
:: WSAAsyncSelect(s,this-m_hWnd,WM_SOCKET, FD_READ);//将套接字设置为异步模式
return TRUE; // return TRUE unless you set the focus to a control
}
void CUdpSDlg::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();
}
}
void CUdpSDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString str=;
int n=sizeof(addr1);
GetDlgItem(IDC_EDIT2)-GetWindowText(str);
if (:
显示全部