网络通信实验报告
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum{ IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if(pSysMenu != NULL)
{
CString strAboutMenu;
#include"Client.h"
#include"Server.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[] = __FILE__;
#endif
HWND g_hWnd;
CServer g_Server;
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
voidCAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
else
{
closesocket(client);
return 0; //退出线程
}
}
closesocket(client);
return 0;
}
客户端:
基于阻塞模式的客户端:
Dlg主类:
#include"stdafx.h"
#include"work1.h"
#include"work1Dlg.h"
SetIcon(m_hIcon, FALSE);// Set small icon
// TODO: Add extra initialization here
m_buttonStop.EnableWindow(false);
g_hWnd=this->GetSafeHwnd();
SetDlgItemInt(IDC_EDIT_PORT,60000);
网络通信程序设计
实验报告
姓 名:
学 号:
专 业:计算机科学与技术
授课教师:贺刚
完成日期:2020.5.27
实验一:TCP套接字编程
内容:
1、利用阻塞模型的开发TCP通信客户端程序。
2、在程序中必须处理粘连包和残缺包问题。
3、自定义应用层协议。
4、采用多线程开发技术。
实验代码:
服务器端:
#include "iostream.h"
returnTRUE;// return TRUE unless you set the focus to a control
}
voidCWork1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
//初始化客户列 表括框
m_listClient.InsertColumn(0,"地址·",LVCFMT_LEFT,80);
m_listClient.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_ONECLICKACTIVATE);
// CWork1Dlg message handlers
BOOL CWork1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
//ON_MESSAGE(WM_NEW_CLIENT,AddClient)
//ON_MESSAGE(WM_DATA_RECV,RecvData)
//ON_MESSAGE(WM_CLIENT_CLOSE,ClientClose)
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
voidCWork1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWork1Dlg)
//{{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()
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Cwork2Dlg 对话框
CWork1Dlg::CWork1Dlg(CWnd* pParent/*=NULL*/)
: CDialog(CWork1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWork1Dlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
// 循环接受客户的连接请求
sockaddr_in remoteAddr;
int nAddrLen = sizeof(remoteAddr);
SOCKET sClient;
HANDLE hWorkerThread;
while(TRUE)
{
// 接受一个新连接
sClient = ::accept(sListen, (SOCKADDR*)&remoteAddr, &nAddrLen);
while(1)
{
iLenth=recv(client,recBuff,1024,0);
if(iLenth>0)
{
//这里仅仅回显,实际上这里通常都要进行业务逻辑处理。
recBuff[iLenth]=0;
cout<<"Recv: "<<recBuff<<endl;
send(client,"谢谢,成功收到!",16,0);
{
cout<<"Failed bind() "<<endl;
return 0;
}
// 进入监听模式
if(::listen(sListen, 5) == SOCKET_ERROR)
{
cout<<"Failed listen() "<<endl;
return 0;
}
cout<<"server is runing... "<<endl;
if(sClient == INVALID_SOCKET)
{
cout<<"Failed accept()"<<endl;
continue;
}
//sClientVector.push_back(sClient);
cout<<"接受到一个连接: "<<inet_ntoa(remoteAddr.sin_addr)<<endl;
}
}
// 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
cout<<sClient<<endl;
hWorkerThread=CreateThread(NULL,0,ThreadProc,(LPVOID)sClient,0,NULL); //创建线程
CloseHandle(hWorkerThread);
}
// 关闭监听套节字
::closesocket(sListen);
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);