TCP/IP 技術實驗報告書 專 業:[通 信 工 程] 學生姓名:[張 世 超] 完成時間:2020 年 7 月 15 日
實驗一 網絡應用程序基礎 實驗目的:
通過實驗,使學生熟悉并掌握運用 TCP/IP 技術進行網絡編程的基本知識,加深對課堂教學內容的理解,掌握套接字網絡通信編程技術,能夠運用 VC++為開發工具編程解決網絡通信中的實際問題,進行一些簡單的網絡應用程序設計。實驗內容:
1,Winsock 的啟動與終止。
2,Winsock 的創建及綁定和關閉。
3,建立通信連接 listen 及 accept 和 connect。
4,數據的傳輸。
5,簡單的客戶機/服務器之間的通信。
要求:通過在 SDK 模式下完成數據通信的過程,掌握 Windows Socket 的常用函數的形式和使用方法,理解數據通信的過程。
實驗步驟:
1,打開 VC 環境 1,使用向導為客戶端創建工程:選擇可執行程序,選擇使用 wsa 環境,單文檔環境,其他的選擇默認設置 2,在文件中添加代碼 3,編譯調試 4,使用向導為服務器端創建工程:選擇可執行程序,選擇使用 wsa 環境,單文檔環境,其他的選擇默認設置 5,在文件中添加代碼 6,編譯調試 7,分別打開兩個系統命令窗口中,并分別在其中運行客戶端和服務器端程序。
8,在客戶端側輸入字符,可以看到服務器收到字符串 參考代碼:課本 156 頁--160 頁 實驗結果:
Client: #include<Winsock2.h> #include<stdio.h> //服務器端口號為 5050 #define DEFAULT_PORT 5050 #define DATA_BUFFER 1024 void main(int argc,char *argv[]) {
WSADATA wsaData;
SOCKET sClient;
int iPort=DEFAULT_PORT;
//從服務器端接收的數據長度
int iLen;
//接收數據的緩沖
char buf[DATA_BUFFER];
//服務器端地址
struct sockaddr_in ser;
//判斷輸入的參數是否正確
if(argc<2)
{
//提示在命令行中輸入服務器 IP 地址
printf("Usage:client [server IP address]\n");
return;
}
//接收數據的緩沖區初始化
memset(buf,0,sizeof(buf));
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Failed to load Winsock.\n");
return;
}
//填寫要連接的服務器地址信息
ser.sin_family=AF_INET;
ser.sin_port=htons(iPort);
//inet_addr()函數將命令行的點分IP地址轉換為用二進制表示的網絡字節順序的 IP 地址
ser.sin_addr.s_addr=inet_addr(argv[1]);
//建立客戶端流式套接口
sClient=socket(AF_INET,SOCK_STREAM,0);
if(sClient==INVALID_SOCKET)
{
printf("socket() Failed:%d\n",WSAGetLastError());
return;
}
//請求與服務器端建立 TCP 連接
if(connect(sClient,(struct sockaddr*)&ser,sizeof(ser))==INVALID_SOCKET)
{
printf("connect() Failed:%d\n",WSAGetLastError());
return;
}
else
{
//從服務器端接收數據
iLen=recv(sClient,buf,sizeof(buf),0);
if(iLen==0)
return;
else if(iLen==SOCKET_ERROR)
{
printf("recv() Failed:%d",WSAGetLastError());
return;
}
printf("recv() data from server:%s\n",buf);
}
closesocket(sClient);
WSACleanup(); }
Server: #include<Winsock2.h> #include<stdio.h> #include<stdlib.h> #pragma comment(lib,"ws2_32.lib") //服務器使用的端口號為 5050 #define DEFAULT_PORT 5050 void main() {
int iPort=DEFAULT_PORT;
WSADATA wsaData;
SOCKET sListen,
sAccept;
//客戶端地址長度
int iLen;
//發送的數據長度
int iSend;
//要發送給客戶端的信息
char buf[]="I am a server.";
//服務器和客戶端的 IP 地址
struct sockaddr_in ser,
cli;
printf("---------------------------\n");
printf("Server waiting\n");
printf("---------------------------\n");
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Failed to load Winsock.\n");
return;
}
//創建服務器端套接口
sListen=socket(AF_INET,SOCK_STREAM,0);
if(sListen==INVALID_SOCKET)
{
printf("socket() Failed:%d\n",WSAGetLastError());
return;
}
//以下建立服務器端地址
ser.sin_family=AF_INET;
//htons()函數把一個雙字節的主機直接順序的數據轉換為網絡直接順序的數
ser.sin_port=htons(iPort);
//htonl()函數把一個四字節的主機直接順序的數據轉換為網絡直接順序的數
//使用系統制定的 IP 地址 INADDR_ANY
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sListen,(LPSOCKADDR)&ser,sizeof(ser))==SOCKET_ERROR)
{
printf("bind() Failed: %d\n",WSAGetLastError());
return;
}
//進入監聽狀態
if(listen(sListen,5)==SOCKET_ERROR)
{
printf("listen() Failed:%d\n",WSAGetLastError());
return;
}
//初始化客戶端地址長度參數
iLen=sizeof(cli);
//進入一個無限循環,等待客戶的連接請求
while(1)
{
sAccept=accept(sListen,(struct sockaddr*)&cli,&iLen);
if(sAccept==INVALID_SOCKET)
{
printf("accept() Failed: %d\n",WSAGetLastError());
break;
}
//輸出客戶 IP 地址和端口號
printf("Accepted client IP:[%s],port:[%d]\n",inet_ntoa(cli.sin_addr),ntohs(cli.sin_port));
//給建立連接的客戶發送信息
iSend=send(sAccept,buf,sizeof(buf),0);
if(iSend==SOCKET_ERROR)
{
printf("send() Failed: %d\n",WSAGetLastError());
break;
}
else if(iSend==0)
break;
else
{
printf("send() byte:%d\n",iSend);
printf("---------------------------\n");
}
closesocket(sAccept);
}
closesocket(sListen);
WSACleanup(); } 實驗截圖:
實驗二 基于 TCP 協議的客戶/服務器通信程序 實驗目的:
通過實驗,使學生熟悉并掌握運用 TCP/IP 技術進行網絡編程的基本知識,加深對課堂教學內容的理解,掌握套接字網絡通信編程技術,能夠運用 VC++為開發工具編程解決網絡通信中的實際問題,進行一些簡單的網絡應用程序設計。
實驗內容:
1,主機間 TCP 的性能測試之一:回程時延。
2,服務器端能從客戶端接收數據并立即將接收到的數據原樣返回給客戶方。
3,客戶端能往服務器端發送數據,然后立即接受從服務器端原樣返回的數據。
理解 TCP 通信程序設計過程,并結合特定應用場景(如創建留言板程序、創建多客戶端服務器/客戶通信程序等)完成程序開發。掌握 TCP 服務器程序和客戶程序的創建過程,熟悉單播通信程序中用到的 Winsock 接口,培養學生將所學知識進行靈活運用的能力。
實驗步驟:
1,打開 VC 環境 2,使用向導為客戶端創建工程:選擇可執行程序,選擇使用 wsa 環境,單文檔環境,其他的選擇默認設置 3,在文件中添加代碼 4,編譯調試 5,使用向導為服務器端創建工程:選擇可執行程序,選擇使用 wsa 環境,單文檔環境,其他的選擇默認設置 6,在文件中添加代碼 7,編譯調試 8,分別打開兩個系統命令窗口中,并分別在其中運行客戶端和服務器端程序。
9,在客戶端著輸入字符,可以看到服務器收到字符串
注:可以再實驗一的代碼上修改,自己增加額外的功能,比如取系統時間,計算往返時間等 作完之后,修改通信代碼使用 UDP 來實現網絡通信 實驗結果:
Client: #include<Winsock2.h> #include<stdio.h> #include<stdlib.h> #define DEFAULT_PORT 5050 #define DATA_BUFFER 1024
#pragma comment(lib,"WS2_32.lib") void main(int argc,char* argv[]) {
WSADATA wsaData;
SOCKET
sClient;
int iPort=5050;
int iLen;
int isend,iRecv;
char send_buf[]="Hello! I am a client";
char recv_buf[DATA_BUFFER];
struct sockaddr_in ser;
if(argc<2)
{
printf("輸入服務器的 IP 地址:\n");
return;
}
else
memset(recv_buf,0,sizeof(recv_buf));
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock 環境初始化失敗:\n");
return;
}
sClient=socket(AF_INET,SOCK_DGRAM,0);
if(sClient==INVALID_SOCKET)
{
printf("socket()函數調用失敗:%d\n",WSAGetLastError());
return;
}
ser.sin_family=AF_INET;
ser.sin_port=htons(iPort);
ser.sin_addr.s_addr=inet_addr(argv[1]);
iLen=sizeof(ser);
isend=sendto(sClient,send_buf,sizeof(send_buf),0,(struct sockaddr*)&ser,iLen);
if(isend==SOCKET_ERROR)
{
printf("sendto()函數調用失敗:%d\n",WSAGetLastError());
return;
}
else if(isend==0)
return;
else
printf("sendto()調用成功:\n");
iRecv=recvfrom(sClient,recv_buf,sizeof(recv_buf),0,(struct sockaddr*)&ser,&iLen);
if(iRecv==SOCKET_ERROR)
{
printf("recvfrom()函數調用失敗:%d\n",WSAGetLastError());
return;
}
else if(iRecv==0)
return;
else
{
printf("sendto():%s\n",recv_buf);
printf("-------------------------------\n");
}
closesocket(sClient);
WSACleanup(); }
Server: #include<Winsock2.h> #include<stdio.h> #include<stdlib.h> #define DEFAULT_PORT 5050 #define BUFFER_LENGTH 1024 #pragma comment(lib,"WS2_32.lib") void main() {
int iPort=DEFAULT_PORT;
WSADATA wsaData;
SOCKET sSocket;
int iLen,iRecv,iSend;
struct sockaddr_in ser,cli;
char send_buf[]="Hollo!I am a server";
char recv_buf[BUFFER_LENGTH];
printf("--------------------");
printf("Server waiting");
printf("--------------------");
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("環境初始化錯誤:\n");
return;
}
sSocket=socket(AF_INET,SOCK_DGRAM,0);
if(sSocket==INVALID_SOCKET)
{
printf("socket()函數調用失敗:\n",WSAGetLastError());
return;
}
ser.sin_family=AF_INET;
ser.sin_port=htons(iPort);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sSocket,(LPSOCKADDR)&ser,sizeof(ser))==SOCKET_ERROR)
{
printf("bind()函數調用失敗:\n",WSAGetLastError());
return;
}
iLen=sizeof(cli);
memset(recv_buf,0,sizeof(recv_buf));
while(1)
{
iRecv=recvfrom(sSocket,recv_buf,BUFFER_LENGTH,0,(SOCKADDR*)&cli,&iLen);
if(iRecv==SOCKET_ERROR)
{
printf("recvfrom()函數調用失敗:\n",WSAGetLastError());
break;
}
else if(iRecv==0)
break;
else
{
printf("recvfrom():%d\n",recv_buf);
printf(" 客 戶 端 的 IP 地 址 、 端 口號:%d\n",inet_ntoa(cli.sin_addr),ntohs(cli.sin_port));
}
iSend=sendto(sSocket,send_buf,sizeof(send_buf),0,(SOCKADDR*)&cli,sizeof(cli));
if(iSend==SOCKET_ERROR)
{
printf("sendto()函數調用失敗:\n",WSAGetLastError());
break;
}
else if(iSend==0)
break;
else
{
printf("sendto():調用成功!\n");
}
}
closesocket(sSocket);
WSACleanup(); } 實驗截圖:
實驗三 簡易聊天系統的實現
實驗目的:
通過實驗,使學生熟悉并掌握運用 TCP/IP 技術進行網絡編程的基本知識,加深對課堂教學內容的理解,掌握套接字網絡通信編程技術,能夠運用 VC++為開發工具編程解決網絡通信中的實際問題,進行一些簡單的網絡應用程序設計。
實驗內容:
設計實現包括客戶端和服務器端的簡單聊天系統。
通過編寫簡單的聊天程序,理解 MFC 的 Socket 類同 Socket API 之間的區別以及MFC 的兩種類之間的聯系與區別。
實驗步驟:
1,打開 VC 環境 2,使用向導為客戶端創建工程:選擇 FMC 可執行程序,選擇使用 wsa 環境,選擇單文檔環境,其他的選擇默認設置 3,為對話窗添加控件:右擊工具欄選擇控件,拖拽某個控件到對話框 4,為控件添加變量:使用類向導,選擇要操作的對話窗類,選擇變量 Tab,點擊添加變量按鈕,為變量命名并選擇變量類型。
5,為控件添加代碼:右擊控件添加事件,如點擊,雙擊,右擊。為事件添加代碼,根據教科書添加代碼 6,添加新的對話窗:單機 rousource Tab, 在對話窗出右擊,選擇添加對話窗, 7,為對話窗添加類:右鍵點擊新對話窗,選擇添加類,出現向導,為類命名并選擇父類 8,為心對話窗添加控件和變量 9,為新對話窗添加代碼 10, 編譯調試
參考代碼:課本 224 頁--229 頁 實驗結果:
CsockClient: #include "stdafx.h" #include "CSockClient.h" #include "CSockClientDlg.h"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif
///////////////////////////////////////////////////////////////////////////// // CCSockClientApp
BEGIN_MESSAGE_MAP(CCSockClientApp, CWinApp)
//{{AFX_MSG_MAP(CCSockClientApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
//
DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CCSockClientApp construction
CCSockClientApp::CCSockClientApp() {
// TODO: add construction code here,
// Place all significant initialization in InitInstance }
///////////////////////////////////////////////////////////////////////////// // The one and only CCSockClientApp object
CCSockClientApp theApp;
///////////////////////////////////////////////////////////////////////////// // CCSockClientApp initialization
BOOL CCSockClientApp::InitInstance() {
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
//
of your final executable, you should remove from the following
//
the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls();
// Call this when using MFC in a shared DLL #else
Enable3dControlsStatic(); // Call this when linking to MFC statically #endif
CCSockClientDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
//
dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
//
dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
//
application, rather than start the application"s message pump.
return FALSE; }
CsockServer: #include "stdafx.h" #include "CsockServer.h" #include "CsockServerDlg.h"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE
static char THIS_FILE[] = __FILE__; #endif
///////////////////////////////////////////////////////////////////////////// // CCsockServerApp
BEGIN_MESSAGE_MAP(CCsockServerApp, CWinApp)
//{{AFX_MSG_MAP(CCsockServerApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
//
DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CCsockServerApp construction
CCsockServerApp::CCsockServerApp() {
// TODO: add construction code here,
// Place all significant initialization in InitInstance }
///////////////////////////////////////////////////////////////////////////// // The one and only CCsockServerApp object
CCsockServerApp theApp;
///////////////////////////////////////////////////////////////////////////// // CCsockServerApp initialization
BOOL CCsockServerApp::InitInstance() {
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
//
of your final executable, you should remove from the following
//
the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls();
// Call this when using MFC in a shared DLL #else
Enable3dControlsStatic(); // Call this when linking to MFC statically #endif
CCsockServerDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
//
dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
//
dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
//
application, rather than start the application"s message pump.
return FALSE; } 實驗截圖:
實驗四 WinInet 實現 FTP 客戶端 實驗目的:
通過實驗,使學生熟悉并掌握運用 TCP/IP 技術進行網絡編程的基本知識,加深對課堂教學內容的理解,掌握套接字網絡通信編程技術,能夠運用 VC++為開發工具編程解決網絡通信中的實際問題,進行一些簡單的網絡應用程序設計。
實驗內容:
1,寫出完整的軟件需求說明書。
2,開發 FTP 的客戶端。
3,完成在局域網內的測試,并記錄測試結果。
本實驗涵蓋了 C/S 體系結構和 Socket 編程。通過本實驗深入地了解 FTP 的工作原理以及服務器端和客戶端的工作流程,學習 Socket 在網絡編程中的各種應用,掌握 WinInet 的套接字編程。
實驗步驟:
1,打開 VC 環境 2,使用向導為客戶端創建工程:選擇 MFC 可執行程序,單文檔環境,其他的選擇默認設置 3,為對話窗添加控件:右擊工具欄選擇控件,拖拽某個控件到對話框 4,為控件添加變量:使用類向導,選擇要操作的對話窗類,選擇變量 Tab,點擊添加變量按鈕,為變量命名并選擇變量類型。
5,為控件添加代碼:右擊控件添加事件,如點擊,雙擊,右擊。為事件添加代碼,根據教科書添加代碼 6,編譯調試 實驗結果:
Scan: #include "stdafx.h" #include "scan.h" #include "scanDlg.h"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif
///////////////////////////////////////////////////////////////////////////// // CScanApp
BEGIN_MESSAGE_MAP(CScanApp, CWinApp)
//{{AFX_MSG_MAP(CScanApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
//
DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CScanApp construction
CScanApp::CScanApp() {
// TODO: add construction code here,
// Place all significant initialization in InitInstance }
///////////////////////////////////////////////////////////////////////////// // The one and only CScanApp object
CScanApp theApp;
///////////////////////////////////////////////////////////////////////////// // CScanApp initialization
BOOL CScanApp::InitInstance() {
// Standard initialization
// If you are not using these features and wish to reduce the size
//
of your final executable, you should remove from the following
//
the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls();
// Call this when using MFC in a shared DLL #else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CScanDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
//
dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
//
dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
//
application, rather than start the application"s message pump.
return FALSE; } 實驗截圖:
推薦訪問: 實驗 報告 TCPIP同志們:今天這個大會,是市委全面落實黨要管黨、從嚴治黨要求的一項重大舉措,也是對縣市區委書記履行基層黨建工作第一責任人情況的一次集中檢閱,同時是對全市基層黨建工作的一次再部署、再落實的會議。前面,**
***年,我認真履行領班子、帶隊伍、抓黨員、保穩定的基層黨建工作思路,以學習貫徹習近平新時代中國特色社會主義思想和黨的十九大歷次全會精神為主線,以市局基層黨建工作考核細則為落腳點,落實全面從嚴治黨主體
根據會議安排,現將2022年履行抓基層黨建工作職責情況報告如下:一、履職工作特色和亮點1 突出政治建設,著力在思想認識上提高。牢固樹立抓黨建就是抓政績的理念,以“黨建工作抓引領、社區治理求突破,為民服
2022年以來,在**黨委的正確領導下,堅持以習近平新時代中國特色社會主義思想為指導,深入學習宣傳貫徹黨的二十大精神,以黨建工作為統領,扎實開展夯實“三個基本”活動,以“四化四力”行動為抓手,聚力創建
各位領導,同志們:根據會議安排,現就2022年度抓基層黨建工作情況匯報如下:一、主要做法及成效(一)強化政治引領。一是不斷強化理論武裝。堅持通過黨組會、中心組學習會和“三會一課”,第一時間、第一議題學
2022年度抓基層黨建工作述職報告按照黨委工作部署,現將本人2022年度抓基層黨建工作情況報告如下:一、2022年度抓基層黨建工作情況(一)旗幟鮮明講政治將旗幟鮮明講政治放在全局發展首要位置,積極開展
2022年,是我在數計系黨總支書記這個新崗位上度過的第一個完整的工作年度。回首一年來在校黨委的正確領導下,與數計系領導班子和全體師生共同走過的日子,艱辛歷歷在目,收獲溫潤心田。作為黨總支書記,我始終牢
按照考核要求,現將本人一年來,作為統戰部長履行職責、廉潔自律等方面情況報告如下:一、著眼增強政治素質,不斷深化理論學習堅持把旗幟鮮明講政治作為履職從政的第一位要求,帶領統戰系統干部堅決擁護“兩個確立”
**年,緊緊圍繞黨工委、管委會的決策部署,全體人員團結協作、凝心聚力,緊扣黨工委“**”基本工作思路,全力開拓進取,認真履職盡責,圓滿完成各項工作任務。一、個人思想政治狀況檸檬文苑www bgzjy
按照縣委關于開展抓基層黨建述職評議會議的有關要求,經請示縣委組織部同意,今天,我們在此召開2022年度基層黨組織書記抓基層黨建述職評議會議。1 首先,請**黨委書記,**同志述職。**黨委能夠主動研究