![計(jì)算機(jī)網(wǎng)絡(luò)-課件e3.實(shí)驗(yàn)三sendudp源程序_第1頁](http://file4.renrendoc.com/view/6e844f170c2bcd5ca0713b7cf70e6576/6e844f170c2bcd5ca0713b7cf70e65761.gif)
![計(jì)算機(jī)網(wǎng)絡(luò)-課件e3.實(shí)驗(yàn)三sendudp源程序_第2頁](http://file4.renrendoc.com/view/6e844f170c2bcd5ca0713b7cf70e6576/6e844f170c2bcd5ca0713b7cf70e65762.gif)
![計(jì)算機(jī)網(wǎng)絡(luò)-課件e3.實(shí)驗(yàn)三sendudp源程序_第3頁](http://file4.renrendoc.com/view/6e844f170c2bcd5ca0713b7cf70e6576/6e844f170c2bcd5ca0713b7cf70e65763.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、 PAGE 7/ SendUDP.cpp : Defines the entry point for the application./#include stdafx.h#include resource.h#include #include #pragma comment(lib, ws2_32)#include TCPIP.h#define MAX_LOADSTRING 100int SendUdp(char *, int , char *, int);void ShowWsaErrorString(char *);void GetHostAddress(SOCKADDR_IN *);vo
2、id CreateAddrPortEditRect(HWND, char *, int);void GetAddrPortEditInt(HWND, char *, int *);void CreateTextEditRect(HWND);void SendTextEdit(HWND);void ExitTextEdit(HWND);SOCKADDR_IN addr_in, addr_out;int SrcPort = 64321;int DstPort = 61234;char DstAddr16 = 192.168.0.103;char PacketText256 = UDP;BOOL S
3、endPacketText = FALSE;/ Global Variables::LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam):switch (message) case WM_COMMAND:wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); / Parse the menu selections:switch (wmId)case IDM_SENDUDP:SendUdp(DstAddr, DstPort, PacketText,
4、strlen(PacketText);break;case IDM_SETDSTADDRPORT:EnableMenuItem(GetMenu(hWnd), IDM_SETPACKETTEXT, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);EnableMenuItem(GetMenu(hWnd), IDM_SENDUDP, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);CreateAddrPortEditRect(hWnd, DstAddr, DstPort);break; case IDM_GETDSTADDRPORT:G
5、etAddrPortEditInt(hWnd, DstAddr, &DstPort);EnableMenuItem(GetMenu(hWnd), IDM_SETPACKETTEXT, MF_BYCOMMAND | MF_ENABLED);EnableMenuItem(GetMenu(hWnd), IDM_SENDUDP, MF_BYCOMMAND | MF_ENABLED);break;case IDM_SETPACKETTEXT:EnableMenuItem(GetMenu(hWnd), IDM_SETDSTADDRPORT, MF_BYCOMMAND | MF_DISABLED | MF_
6、GRAYED);EnableMenuItem(GetMenu(hWnd), IDM_SENDUDP, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);CreateTextEditRect(hWnd);break; case IDM_SENDPACKETTEXT:SendTextEdit(hWnd);SendUdp(DstAddr, DstPort, PacketText, strlen(PacketText);break;case IDM_EXITPACKETTEXT:ExitTextEdit(hWnd);EnableMenuItem(GetMenu(hWnd)
7、, IDM_SETDSTADDRPORT, MF_BYCOMMAND | MF_ENABLED);EnableMenuItem(GetMenu(hWnd), IDM_SENDUDP, MF_BYCOMMAND | MF_ENABLED);break;: return 0;:int InitIpHeader(char *ipBuffer, char *SrcAddr, char *DstAddr, int PayLoadLen)/ IP 頭初始化 IP *ip=NULL; ip = (IP *)ipBuffer; ip-Version = (4 ServiceType = 0; ip-Total
8、Len= htons(sizeof(IP) + PayLoadLen); ip-ID= 0; ip-FragOff = 0; ip-TimeToLive= 6; ip-Protocol= 0 x11; ip-HdrChksum = 0; ip-SrcAddr= inet_addr(SrcAddr); ip-DstAddr= inet_addr(DstAddr);/ ip-HdrChksum; return sizeof(IP);int InitUdpHeader(char *Buffer, int SrcPort, int DstPort, int PayLoadLen) UDP *udp =
9、 NULL; udp = (UDP *)Buffer; udp-SrcPort = htons(SrcPort); udp-DstPort = htons(DstPort); udp-TotalLen= htons(sizeof(UDP) + PayLoadLen);/udp-Chksum return sizeof(UDP);char snString1024;int SendUdp(char *DstAddr, int DstPort, char *UdpBuffer, int UdpBufferSize) WSADATA wsaData; SOCKET SendSocket; charS
10、endBuffer65535=0;intIpHdrLen, UdpHdrLen, TotalSize;int flag; intret; TotalSize = sizeof(IP) + sizeof(UDP) + UdpBufferSize;/ 檢查 Winsock 版本號(hào),WSAData為WSADATA結(jié)構(gòu)對(duì)象 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) ShowWsaErrorString(Send WSAStartup); return -1; / 創(chuàng)建原始套接字SendSocket = socket(AF_INET, SOCK_RAW,
11、 IPPROTO_UDP);if (SendSocket = INVALID_SOCKET) ShowWsaErrorString(Send Socket);goto retSend; / 設(shè)置IP頭, 包括optionflag = 1; if (setsockopt(SendSocket, IPPROTO_IP, IP_HDRINCL, (char *)&flag, sizeof(flag) = SOCKET_ERROR) ShowWsaErrorString(Send opt);goto retSend; GetHostAddress(&addr_in); if (bind(SendSoc
12、ket, (PSOCKADDR)&addr_in, sizeof(addr_in) = SOCKET_ERROR) ShowWsaErrorString(Send bind);goto retSend; / 初始化IP頭IpHdrLen = InitIpHeader(SendBuffer, inet_ntoa(addr_in.sin_addr), DstAddr, UdpBufferSize);/ 發(fā)給指定主機(jī)/IpHdrLen = InitIpHeader(SendBuffer, inet_ntoa(addr_in.sin_addr), inet_ntoa(addr_in.sin_addr)
13、, UdpBufferSize);/ 發(fā)給本機(jī) / 初始化UDP頭 UdpHdrLen = InitUdpHeader(&SendBufferIpHdrLen, SrcPort, DstPort, UdpBufferSize); / Copy the payload to the end of the header memcpy(&SendBufferIpHdrLen + UdpHdrLen, UdpBuffer, UdpBufferSize); addr_out.sin_family = AF_INET; addr_out.sin_port = htons(DstPort); addr_ou
14、t.sin_addr.s_addr = inet_addr(DstAddr); ret = sendto(SendSocket, SendBuffer, TotalSize, 0, (const struct sockaddr*)&addr_out, sizeof(addr_out); if (ret = SOCKET_ERROR) ShowWsaErrorString(Send to);goto retSend; else wsprintf(snString, sourAddr=%s, inet_ntoa(addr_in.sin_addr);wsprintf(snString, %s, ds
15、tAddr=%s, snString, inet_ntoa(addr_out.sin_addr);wsprintf(snString, %s, sourPort=%d, dstPort=%d, sendsize=%d, snString, ntohs(addr_in.sin_port), ntohs(addr_out.sin_port), ret); if (!SendPacketText) MessageBox(NULL, snString, sent OK, MB_OK); retSend: closesocket(SendSocket) ; WSACleanup() ; return 0
16、;void ShowWsaErrorString(char *ErrorType)int i, WSAErrorID;char WSAErrorType128;WSAErrorID = WSAGetLastError();wsprintf(WSAErrorType, 錯(cuò)誤代碼:%d, WSAErrorID);for (i=0; isin_addr = *(in_addr *)pHost-h_addr_list0; /IPaddrin-sin_family = AF_INET;addrin-sin_port = htons(SrcPort);HWND hAddrEdit, hPortEdit,
17、hButton; RECT hAddrPortEditRect = 170, 100, 300, 130;void CreateAddrPortEditRect(HWND hWnd, char *SetDstAddr, int SetDstPort)char hEditString16;HDC hDC;hDC = GetDC(hWnd);TextOut(hDC, hAddrPortEditRect.left-120, hAddrPortEditRect.top+4, 目的主機(jī)IP地址:, 16);TextOut(hDC, hAddrPortEditRect.left-120, hAddrPor
18、tEditRect.top+44, 目的端口號(hào): , 16);ReleaseDC(hWnd, hDC);hAddrEdit = CreateWindow(EDIT, NULL, WS_CHILD|WS_VISIBLE|ES_LEFT|WS_THICKFRAME, hAddrPortEditRect.left, hAddrPortEditRect.top, hAddrPortEditRect.right-hAddrPortEditRect.left, hAddrPortEditRect.bottom-hAddrPortEditRect.top, hWnd, NULL, hInst, NULL);
19、 hPortEdit = CreateWindow(EDIT, NULL, WS_CHILD|WS_VISIBLE|ES_LEFT|WS_THICKFRAME, hAddrPortEditRect.left, hAddrPortEditRect.top+40, hAddrPortEditRect.right-hAddrPortEditRect.left, hAddrPortEditRect.bottom-hAddrPortEditRect.top, hWnd, NULL, hInst, NULL); SetWindowText(hAddrEdit, SetDstAddr);itoa(SetDs
20、tPort, hEditString, 10);SetWindowText(hPortEdit, hEditString);hButton = CreateWindow(BUTTON, 確定, WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, hAddrPortEditRect.left-50, hAddrPortEditRect.top+100, hAddrPortEditRect.right-hAddrPortEditRect.left-40, hAddrPortEditRect.bottom-hAddrPortEditRect.top, hWnd, (HMENU)ID
21、M_GETDSTADDRPORT, hInst, NULL); void GetAddrPortEditInt(HWND hWnd, char *GetDstAddr, int *GetDstPort)char hEditString16;HDC hDC; GetWindowText(hAddrEdit, hEditString, 15); if (strlen(hEditString) 4) return;strcpy(GetDstAddr, hEditString); GetWindowText(hPortEdit, hEditString, 15); if (strlen(hEditSt
22、ring) 1) return;*GetDstPort = atoi(hEditString);DestroyWindow(hAddrEdit);DestroyWindow(hPortEdit);DestroyWindow(hButton);hDC = GetDC(hWnd);TextOut(hDC, hAddrPortEditRect.left-120, hAddrPortEditRect.top+4, , 16);TextOut(hDC, hAddrPortEditRect.left-120, hAddrPortEditRect.top+44, , 16);ReleaseDC(hWnd,
23、hDC);HWND hTextEdit, hTextButton, hTextExitButton; RECT hTextEditRect = 100, 100, 1000, 130;void CreateTextEditRect(HWND hWnd)HDC hDC;hDC = GetDC(hWnd);TextOut(hDC, hTextEditRect.left, hTextEditRect.top+10, 請(qǐng)輸入要傳送的報(bào)文:, 20);ReleaseDC(hWnd, hDC);hTextEdit = CreateWindow(EDIT, NULL, WS_CHILD|WS_VISIBLE|ES_LEFT|WS_THICKFRAME, hTextEditRect.left, hTextEditRect.top+40, 1000, 30, hWnd, NULL, hInst, NULL); hTextButton = CreateWindow(BUTTON, 發(fā)送, WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, (hTextEditRect.left+hTextEditRect.right)/2-
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年臨時(shí)租賃協(xié)議范例(三篇)
- 2025年度安全生產(chǎn)標(biāo)準(zhǔn)化體系建設(shè)合同
- 2025年度公共場(chǎng)所安全保衛(wèi)勞務(wù)分包合同
- 智能停車場(chǎng)裝修合同范本
- 教育培訓(xùn)基地租賃合同
- 學(xué)校教室內(nèi)墻裝修工程合同
- 家電產(chǎn)品國內(nèi)物流配送協(xié)議
- 大宗商品運(yùn)輸承運(yùn)合同范本
- 主題美容院裝修協(xié)議模板
- 水產(chǎn)加工污泥轉(zhuǎn)運(yùn)協(xié)議模板
- 人教版六年級(jí)數(shù)學(xué)下冊(cè)完整版教案及反思
- 少兒財(cái)商教育講座課件
- (八省聯(lián)考)云南省2025年普通高校招生適應(yīng)性測(cè)試 物理試卷(含答案解析)
- 2025藥劑科工作人員工作計(jì)劃
- 春節(jié)節(jié)后安全教育培訓(xùn)
- 2025年新高考數(shù)學(xué)一輪復(fù)習(xí)第5章重難點(diǎn)突破02向量中的隱圓問題(五大題型)(學(xué)生版+解析)
- 水土保持方案投標(biāo)文件技術(shù)部分
- 印刷品質(zhì)量保證協(xié)議書
- 2023年浙江省公務(wù)員錄用考試《行測(cè)》題(A類)
- CQI-23模塑系統(tǒng)評(píng)估審核表-中英文
- 南方日?qǐng)?bào)圖片管理系統(tǒng)開發(fā)項(xiàng)目進(jìn)度管理研究任務(wù)書
評(píng)論
0/150
提交評(píng)論