代碼點擊這裏下載
服務器端截圖:
客戶端截圖:
基於MFC對話框的MFC UPD客戶端以及服務器端程序
1.服務器端
1.1服務器端點擊create按鈕後,程序讀取設置的服務器端端口,默認端口爲100,創建服務器端socket,將create的按鈕變換成close按鈕,並且將服務器端的地址及端口與socket bind.開啓讀取數據線程.
1.2當服務器讀取線程接收到數據時,將保存數據源的ip地址以及端口,將數據以及數據的源在receive edit控件中顯示出來,並將數據源在client中顯示出來.如果是第一次接收到數據,將使能send按鈕.
1.3當點擊send時,將讀取send edit控件中的內容並且發送.
1.4當關閉對話框時將終止讀取線程.
2.客戶端
2.1輸入服務器端ip以及端口號後reset,即可設置發送數據的目標地址,並且開啓讀取數據線程讀取數據,使能send按鈕.
2.2按下send按鈕後將讀取send edit控件中的數據發送至服務器端.
2.3當接收到數據時,將在receive edit控件中顯示接收的數據.
2.4當關閉對話框時,將終止讀取數據線程
// UDP_ServerDlg.h : 頭文件
//
#pragma once
#include "afxcmn.h"
#include "afxwin.h"
#include <WinSock2.h>
// CUDP_ServerDlg 對話框
class CUDP_ServerDlg : public CDialog
{
// 構造
public:
CUDP_ServerDlg(CWnd* pParent = NULL); // 標準構造函數
// 對話框數據
enum { IDD = IDD_UDP_SERVER_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 實現
protected:
HICON m_hIcon;
// 生成的消息映射函數
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButtonCreateServer();
public:
afx_msg void OnBnClickedButtonCloseServer();
public:
// 端口號控件變量
CEdit m_editPortNO;
public:
// IP地址數組
char m_gcIP;
public:
CString tempStr;
public:
afx_msg void OnClose();
public:
CString m_strTempString;
public:
// 服務器端口號
unsigned short m_nPortNO;
public:
// 服務器端socket
SOCKET m_sockServer;
public:
// 若已經創建服務器則爲true,否則爲false
bool m_bServerCreated;
public:
struct sockaddr_in m_clientAddr;
public:
// 接收數據的edit控件
CEdit m_editReceive;
public:
// 接收數據控件的控件變量,str
CString m_strReceEdit;
public:
afx_msg void OnBnClickedButtonSend();
public:
// 發送edit控件變量,str
CString m_strSendData;
public:
// 若爲true則終止線程
bool m_bTerminateThread;
};
// UDP_ServerDlg.cpp : 實現文件
//
#include "stdafx.h"
#include "UDP_Server.h"
#include "UDP_ServerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用於應用程序“關於”菜單項的 CAboutDlg 對話框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 對話框數據
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 實現
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CUDP_ServerDlg 對話框
CUDP_ServerDlg::CUDP_ServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUDP_ServerDlg::IDD, pParent)
, m_gcIP(0)
, tempStr(_T(""))
, m_strTempString(_T(""))
, m_nPortNO(0)
, m_bServerCreated(false)
, m_strReceEdit(_T(""))
, m_strSendData(_T(""))
, m_bTerminateThread(false)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUDP_ServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_SERVER_PORT, m_editPortNO);
DDX_Control(pDX, IDC_EDIT_RECEIVE, m_editReceive);
DDX_Text(pDX, IDC_EDIT_RECEIVE, m_strReceEdit);
DDX_Text(pDX, IDC_EDIT_SEND, m_strSendData);
}
BEGIN_MESSAGE_MAP(CUDP_ServerDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON_CREATE_SERVER, &CUDP_ServerDlg::OnBnClickedButtonCreateServer)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON_SEND, &CUDP_ServerDlg::OnBnClickedButtonSend)
END_MESSAGE_MAP()
// CUDP_ServerDlg 消息處理程序
BOOL CUDP_ServerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 將“關於...”菜單項添加到系統菜單中。
// IDM_ABOUTBOX 必須在系統命令範圍內。
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);
}
}
// 設置此對話框的圖標。當應用程序主窗口不是對話框時,框架將自動
// 執行此操作
SetIcon(m_hIcon, TRUE); // 設置大圖標
SetIcon(m_hIcon, FALSE); // 設置小圖標
// TODO: 在此添加額外的初始化代碼
//WSAStartup()
WSADATA wsaData;
PHOSTENT hostinfo;
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
MessageBox("WSAStartup ERROR!");
}
//設置服務器IP地址
char gcTemp[255];
char * IP;
if (0 == gethostname(gcTemp, 255))
{
hostinfo = gethostbyname(gcTemp);
//tempStr.Format("%s", gcTemp);
//MessageBox(tempStr);
IP = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
tempStr.Format(IP);
//MessageBox(IP);
SetDlgItemText(IDC_STATIC_SERVER_ADDRESS, tempStr);
//m_addServerAddress.SetAddress();
}
//設置服務器默認端口號爲100
SetDlgItemText(IDC_EDIT_SERVER_PORT, "100");
m_bServerCreated = false;
m_bTerminateThread = false;
return TRUE; // 除非將焦點設置到控件,否則返回 TRUE
}
void CUDP_ServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向對話框添加最小化按鈕,則需要下面的代碼
// 來繪製該圖標。對於使用文檔/視圖模型的 MFC 應用程序,
// 這將由框架自動完成。
void CUDP_ServerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用於繪製的設備上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 CUDP_ServerDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
UINT ServerRecvThread(LPVOID lpParm )
{
CUDP_ServerDlg *dlg = (CUDP_ServerDlg*)lpParm;
char gcInBuffer[1027];
int lenth;
int size = sizeof(sockaddr_in);
CString strReceive, tempStr;
char*gcClientIP;
bool bSendEnable = false;
while(!dlg->m_bTerminateThread)
{
if ((lenth = recvfrom(dlg->m_sockServer, gcInBuffer, 1024, 0, (struct sockaddr *)&dlg->m_clientAddr, &size) )>0)
{
if (!bSendEnable)
{
CWnd *cwnd = dlg->GetDlgItem(IDC_BUTTON_SEND);
cwnd->EnableWindow(TRUE);
bSendEnable = true;
}
gcClientIP = inet_ntoa((dlg->m_clientAddr).sin_addr);
tempStr.Format("%s", gcClientIP);
dlg->SetDlgItemText(IDC_STATIC_CLINET_ADDRESS, tempStr);
tempStr.Format("%hu", dlg->m_clientAddr.sin_port);
dlg->SetDlgItemText(IDC_STATIC_CLIENT_PORTNO, tempStr);
tempStr.Format("%s:%hu: ", gcClientIP, dlg->m_clientAddr.sin_port);
strReceive += tempStr;
gcInBuffer[lenth] = '\r';
gcInBuffer[lenth+1] = '\n';
gcInBuffer[lenth+2] = '\0';
strReceive += gcInBuffer;
dlg->m_editReceive.SetWindowText(strReceive);
}
}
return 0;
}
void CUDP_ServerDlg::OnBnClickedButtonCreateServer()
{
// TODO: 在此添加控件通知處理程序代碼
if (m_bServerCreated)
{
//終止線程
m_bTerminateThread = true;
m_bServerCreated = false;
m_editPortNO.EnableWindow(TRUE);
SetDlgItemText(IDC_BUTTON_CREATE_SERVER, "Create");
//disable send 按鈕
CWnd * cwnd = GetDlgItem(IDC_BUTTON_SEND);
cwnd->EnableWindow(FALSE);
}
else//創建服務器端
{
//讀取服務器地址以及端口號
GetDlgItemText(IDC_EDIT_SERVER_PORT, m_strTempString);
m_nPortNO = atoi(m_strTempString.GetBuffer());
//MessageBox(m_strTempString);
//socket
if ((m_sockServer = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
MessageBox("ERROR: Create Server Socket Error!");
exit(-1);
}
//bind
struct sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(m_nPortNO);
(serAddr.sin_addr).s_addr = htonl(INADDR_ANY);
if ((bind(m_sockServer, (LPSOCKADDR)&serAddr, sizeof(serAddr)))==SOCKET_ERROR)
{
MessageBox("ERROR: Bind Socket Error!");
exit(-1);
}
//disable portno
m_editPortNO.EnableWindow(FALSE);
SetDlgItemText(IDC_BUTTON_CREATE_SERVER, "Close");
m_bServerCreated = true;
//創建線程等待
m_bTerminateThread = false;
AfxBeginThread(ServerRecvThread, this, THREAD_PRIORITY_NORMAL, 0, 0, NULL);
}
}
void CUDP_ServerDlg::OnClose()
{
// TODO: 在此添加消息處理程序代碼和/或調用默認值
m_bTerminateThread = true;
closesocket(m_sockServer);
WSACleanup();
//Disable close按鈕,enable create按鈕
CWnd * cwnd = (GetDlgItem(IDC_BUTTON_CREATE_SERVER));
cwnd->EnableWindow(TRUE);
CDialog::OnClose();
}
void CUDP_ServerDlg::OnBnClickedButtonSend()
{
// TODO: 在此添加控件通知處理程序代碼
UpdateData(TRUE);
//MessageBox(m_strSendData);
sendto(m_sockServer, m_strSendData, strlen(m_strSendData), 0, (struct sockaddr*)&m_clientAddr, sizeof(sockaddr));
}
// UDP_ClientDlg.h : 頭文件
//
#pragma once
#include <WinSock2.h>
#include "afxcmn.h"
#include "afxwin.h"
// CUDP_ClientDlg 對話框
class CUDP_ClientDlg : public CDialog
{
// 構造
public:
CUDP_ClientDlg(CWnd* pParent = NULL); // 標準構造函數
// 對話框數據
enum { IDD = IDD_UDP_CLIENT_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 實現
protected:
HICON m_hIcon;
// 生成的消息映射函數
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButtonResetServer();
public:
afx_msg void OnBnClickedButtonSend();
public:
CString tempStr;
public:
// 客戶端socket
SOCKET m_sockClient;
public:
struct sockaddr_in m_serverAddr;
public:
afx_msg void OnClose();
public:
// 服務器端地址控件
CIPAddressCtrl m_addrServer;
public:
unsigned short m_nServerPortNo;
public:
// 若服務器ip及端口已設定則爲true,否則爲false
bool m_bServerSet;
public:
CString m_strServerIPPort;
public:
CEdit m_editReceive;
public:
// 控件變量,str
CString m_strSendData;
public:
// 若爲true則終止線程
bool m_bTerminateThread;
};
// UDP_ClientDlg.cpp : 實現文件
//
#include "stdafx.h"
#include "UDP_Client.h"
#include "UDP_ClientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用於應用程序“關於”菜單項的 CAboutDlg 對話框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 對話框數據
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 實現
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CUDP_ClientDlg 對話框
CUDP_ClientDlg::CUDP_ClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUDP_ClientDlg::IDD, pParent)
, tempStr(_T(""))
, m_nServerPortNo(0)
, m_bServerSet(false)
, m_strSendData(_T(""))
, m_bTerminateThread(false)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUDP_ClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_IPADDRESS_SERVER, m_addrServer);
DDX_Control(pDX, IDC_EDIT_RECEIVE, m_editReceive);
DDX_Text(pDX, IDC_EDIT4, m_strSendData);
}
BEGIN_MESSAGE_MAP(CUDP_ClientDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON_RESET_SERVER, &CUDP_ClientDlg::OnBnClickedButtonResetServer)
ON_BN_CLICKED(IDC_BUTTON_SEND, &CUDP_ClientDlg::OnBnClickedButtonSend)
ON_WM_CLOSE()
END_MESSAGE_MAP()
// CUDP_ClientDlg 消息處理程序
BOOL CUDP_ClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 將“關於...”菜單項添加到系統菜單中。
// IDM_ABOUTBOX 必須在系統命令範圍內。
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);
}
}
// 設置此對話框的圖標。當應用程序主窗口不是對話框時,框架將自動
// 執行此操作
SetIcon(m_hIcon, TRUE); // 設置大圖標
SetIcon(m_hIcon, FALSE); // 設置小圖標
// TODO: 在此添加額外的初始化代碼
m_bServerSet = false;
m_bTerminateThread = false;
//WSAStartup()
WSADATA wsaData;
PHOSTENT hostinfo;
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
MessageBox("WSAStartup ERROR!");
}
//設置客戶端IP地址及端口
//讀取本機ip地址
char gcTemp[255];
char * IP;
if (0 == gethostname(gcTemp, 255))
{
hostinfo = gethostbyname(gcTemp);
//tempStr.Format("%s", gcTemp);
//MessageBox(tempStr);
IP = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
tempStr.Format(IP);
//MessageBox(IP);
SetDlgItemText(IDC_STATIC_CLIENT_ADDRESS, tempStr);
//m_addServerAddress.SetAddress();
}
//create client socket
//socket
if ((m_sockClient = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
MessageBox("ERROR: Create Client Socket Error!");
exit(-1);
}
return TRUE; // 除非將焦點設置到控件,否則返回 TRUE
}
void CUDP_ClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向對話框添加最小化按鈕,則需要下面的代碼
// 來繪製該圖標。對於使用文檔/視圖模型的 MFC 應用程序,
// 這將由框架自動完成。
void CUDP_ClientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用於繪製的設備上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 CUDP_ClientDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
UINT ClientRecvThread(LPVOID lpParm )
{
CUDP_ClientDlg *dlg = (CUDP_ClientDlg*)lpParm;
char gcInBuffer[1027];
int lenth;
int size = sizeof(sockaddr_in);
CString strReceive, tempStr;
while(!dlg->m_bTerminateThread)
{
if ((lenth = recvfrom(dlg->m_sockClient, gcInBuffer, 1024, 0, (struct sockaddr *)&dlg->m_serverAddr, &size) )>0)
{
strReceive+= dlg->m_strServerIPPort+": ";
gcInBuffer[lenth] = '\r';
gcInBuffer[lenth+1] = '\n';
gcInBuffer[lenth+2] = '\0';
strReceive += gcInBuffer;
dlg->m_editReceive.SetWindowText(strReceive);
}
}
return 0;
}
void CUDP_ClientDlg::OnBnClickedButtonResetServer()
{
// TODO: 在此添加控件通知處理程序代
//重置服務器ip地址以及端口號
m_serverAddr.sin_family = AF_INET;
//讀取服務器端地址
BYTE addr[4];
m_addrServer.GetAddress(addr[0], addr[1], addr[2], addr[3]);
//讀取服務器端口號
GetDlgItemText(IDC_EDIT_SERVER_PORTNO, tempStr);
m_nServerPortNo = atoi(tempStr.GetBuffer());
tempStr.Format("%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
m_strServerIPPort.Format("%d.%d.%d.%d:%d", addr[0], addr[1], addr[2], addr[3], m_nServerPortNo);
//MessageBox(tempStr);
m_serverAddr.sin_port = htons(m_nServerPortNo);
m_serverAddr.sin_addr.s_addr= inet_addr(tempStr);
if (m_bServerSet)//判斷是否第一次按下按鈕
{
return;
}
else
{
m_bServerSet = true;
//enable send 按鈕
CWnd * cwnd = GetDlgItem(IDC_BUTTON_SEND);
cwnd->EnableWindow(TRUE);
//開啓接收數據線程
m_bTerminateThread = false;
AfxBeginThread(ClientRecvThread, this, THREAD_PRIORITY_NORMAL, 0, 0, NULL);
}
}
void CUDP_ClientDlg::OnBnClickedButtonSend()
{
// TODO: 在此添加控件通知處理程序代碼
UpdateData(TRUE);
sendto(m_sockClient, m_strSendData, strlen(m_strSendData), 0, (struct sockaddr*)&m_serverAddr, sizeof(sockaddr));
}
void CUDP_ClientDlg::OnClose()
{
// TODO: 在此添加消息處理程序代碼和/或調用默認值
m_bTerminateThread = true;
closesocket(m_sockClient);
WSACleanup();
CDialog::OnClose();
}