-
大小: 136KB文件類型: .rar金幣: 1下載: 0 次發(fā)布日期: 2021-06-12
- 語言: 其他
- 標(biāo)簽: 網(wǎng)絡(luò)??通訊??源代碼??
資源簡介
TCP/IP通訊實(shí)例源代碼(服務(wù)端和客戶端源碼)將socket消息設(shè)置為窗口接收的消息類型!
代碼片段和文件信息
/////////////////////////////////////////////////
//?TCPClient.cpp文件
#include?“TCPClient.h“
#include?“resource.h“
//?定義網(wǎng)絡(luò)事件通知消息
#define?WM_SOCKET?WM_USER?+?1
CMyApp?theApp;
BOOL?CMyApp::InitInstance()
{
//?初始化Winsock庫
WSADATA?wsaData;
WORD?sockVersion?=?MAKEWORD(2?0);
::WSAStartup(sockVersion?&wsaData);
//?彈出主窗口對話框
CMainDialog?dlg;
m_pMainWnd?=?&dlg;
dlg.DoModal();
//?釋放Winsock庫
::WSACleanup();
return?FALSE;
}
CMainDialog::CMainDialog(CWnd*?pParentWnd):CDialog(IDD_MAINDIALOG?pParentWnd)
{
}
BEGIN_MESSAGE_MAP(CMainDialog?CDialog)
ON_BN_CLICKED(IDC_CONNECT?OnButtonConnect)
ON_BN_CLICKED(IDC_SEND?OnButtonSend)
ON_BN_CLICKED(IDC_CLEAR?OnButtonClear)
ON_MESSAGE(WM_SOCKET?OnSocket)
END_MESSAGE_MAP()
BOOL?CMainDialog::OnInitDialog()
{
CDialog::OnInitDialog();
//?設(shè)置圖標(biāo)
SetIcon(theApp.LoadIcon(IDI_MAIN)?FALSE);
//?創(chuàng)建狀態(tài)欄,設(shè)置它的屬性
m_bar.Create(WS_CHILD|WS_VISIBLE|SBS_SIZEGRIP?CRect(0?0?0?0)?this?101);
m_bar.SetBkColor(RGB(0xa6?0xca?0xf0)); //?背景色
int?arWidth[]?=?{?200?-1?};
m_bar.SetParts(2?arWidth); //?分欄
m_bar.SetText(“?Windows程序設(shè)計(jì)進(jìn)階之路!“?1?0); //?第一個欄的文本
m_bar.SetText(“?空閑“?0?0); //?第二個欄的文本
//?初始化發(fā)送按鈕和發(fā)送編輯框的狀態(tài)
GetDlgItem(IDC_SEND)->EnableWindow(FALSE);
GetDlgItem(IDC_TEXT)->EnableWindow(FALSE);
//?初始化連接套節(jié)字
m_socket?=?INVALID_SOCKET;
return?TRUE;
}
void?CMainDialog::OnCancel()
{
if(m_socket?!=?INVALID_SOCKET)
::closesocket(m_socket);
CDialog::OnCancel();
}
void?CMainDialog::OnButtonClear()
{
GetDlgItem(IDC_INFO)->SetWindowText(““);
}
void?CMainDialog::OnButtonConnect()
{
if(m_socket?==?INVALID_SOCKET)??//?連接服務(wù)器
{
//?取得服務(wù)器地址
CString?sAddr;
GetDlgItem(IDC_ADDR)->GetWindowText(sAddr);
if(sAddr.IsEmpty())
{
MessageBox(“請輸入服務(wù)器地址!“);
return;
}
//?取得端口號
CString?sPort;
GetDlgItem(IDC_PORT)->GetWindowText(sPort);
int?nPort?=?atoi(sPort);
if(nPort?1?||?nPort?>?65535)
{
MessageBox(“端口號錯誤!“);
return;
}
//?試圖連接服務(wù)器
if(!Connect(sAddr?nPort))
{
MessageBox(“連接服務(wù)器出錯!“);
return;
}
//?設(shè)置用戶界面
GetDlgItem(IDC_CONNECT)->SetWindowText(“取消“);
m_bar.SetText(“?正在連接……“?0?0);
}
else //?斷開服務(wù)器
{
//?關(guān)閉套節(jié)字
::closesocket(m_socket);
m_socket?=?INVALID_SOCKET;
//?設(shè)置用戶界面
GetDlgItem(IDC_CONNECT)->SetWindowText(“連接服務(wù)器“);
m_bar.SetText(“?空閑“?0?0);
GetDlgItem(IDC_ADDR)->EnableWindow(TRUE);
GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
GetDlgItem(IDC_TEXT)->EnableWindow(FALSE);
GetDlgItem(IDC_SEND)->EnableWindow(FALSE);
}
}
long?CMainDialog::OnSocket(WPARAM?wParam?LPARAM?lParam)
{
//?取得有事件發(fā)生的套節(jié)字句柄
SOCKET?s?=?wParam;
//?查看是否出錯
if(WSAGETSELECTERROR(lParam))
{
if(m_socket?!=?SOCKET_ERROR)
OnButtonConnect();
m_bar.SetText(“?連接出錯!“?0?0);
return?0;
}
//?處理發(fā)生的事件
switch(WSAGETSELECTEVENT(lParam))
{
case?FD_CONNECT: //?套節(jié)字正確的連接到服務(wù)器
{
//?設(shè)置用戶界面
GetDlgItem(IDC_CONNECT)->Se
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????4030??2005-01-30?22:31??10TCPServer\10TCPServer.dsp
?????文件????????545??2004-11-26?23:30??10TCPServer\10TCPServer.dsw
?????文件??????33792??2011-03-10?21:47??10TCPServer\10TCPServer.ncb
?????文件??????53760??2011-03-10?21:47??10TCPServer\10TCPServer.opt
?????文件????????766??2004-02-21?21:18??10TCPServer\ACER.ICO
?????文件???????2238??2004-12-04?10:14??10TCPServer\network?computer.ico
?????文件?????102400??2005-01-30?22:46??10TCPServer\Release\10TCPServer.exe
?????文件???????1190??2004-12-04?15:31??10TCPServer\resource.h
?????文件???????2982??2004-12-04?15:31??10TCPServer\resource.rc
?????文件???????6166??2005-01-30?22:46??10TCPServer\TCPClient.cpp
?????文件???????1273??2004-12-08?12:47??10TCPServer\TCPClient.h
?????文件???????4060??2005-01-30?22:45??10TCPClient\10TCPClient.dsp
?????文件????????545??2004-11-26?23:20??10TCPClient\10TCPClient.dsw
?????文件??????33792??2010-07-21?16:47??10TCPClient\10TCPClient.ncb
?????文件??????53760??2010-07-21?16:47??10TCPClient\10TCPClient.opt
?????文件?????176200??2005-01-30?22:42??10TCPClient\Release\10TCPClient.exe
?????文件???????1420??2004-12-05?14:42??10TCPClient\resource.h
?????文件???????3270??2004-12-09?16:37??10TCPClient\resource.rc
?????文件???????5798??2005-01-30?22:42??10TCPClient\TCPClient.cpp
?????文件????????968??2004-12-08?22:13??10TCPClient\TCPClient.h
?????文件????????766??2004-12-05?14:42??10TCPClient\TCPClient.ICO
?????目錄??????????0??2013-01-15?14:29??10TCPServer\Debug
?????目錄??????????0??2013-01-15?14:29??10TCPServer\Release
?????目錄??????????0??2013-01-15?14:29??10TCPClient\Debug
?????目錄??????????0??2013-01-15?14:29??10TCPClient\Release
?????目錄??????????0??2013-01-15?14:29??10TCPServer
?????目錄??????????0??2013-01-15?14:29??10TCPClient
-----------?---------??----------?-----??----
???????????????489721????????????????????27
............此處省略0個文件信息
評論
共有 條評論