




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
深 圳 大 学 实 验 报 告 课程名称: 计算机网络 实验项目名称: windows socket编程 学院: 专业: 指导教师: 报告人: 学号: 班级: 实验时间: 2014.12.19 实验报告提交时间: 2014.12.26 教务处制实验目的与要求: 实验目的熟悉和掌握socket编程的基本理论和方法。掌握基于TCP和UDP的工作原理以及Socket编程的一般方法,能够编写简单的网络应用程序。实验要求采用无连接协议的异步模式,编写服务端及客户端应用程序,实现两者之前文字信息的传输。方法、步骤:分别建立两个项目,server和client,在VC6.0-新建-工程-win32 application-输入项目名-选择 a typical hello world win32 application。并按照图片中的要求添加代码。2. 调试程序。3. 两台电脑实现链接。实验过程及内容: 1.分别建立两个项目,server和client,在VC6.0-新建-工程-win32 application-输入项目名-选择 a typical hello world win32 application。并按照图片中的要求添加代码。2.调试程序。3.两台电脑实现链接。Server端/ server.cpp : Defines the entry point for the application./#include stdafx.h#include resource.h#define MAX_LOADSTRING 10000#include#pragma comment(lib,Ws2_32.lib)WSADATA ws;SOCKET S;struct sockaddr_in lAddr,rAddr;int LPort=6666;char LIP=0.0.0.0;char Msg88;int d,l=sizeof(rAddr);void SetSockAddr(struct sockaddr_in*A,WORD Port,char*IP)A-sin_family=AF_INET;A-sin_port=htons(Port);A-sin_addr.s_addr=inet_addr(IP);TCHAR messageDisplayMAX_LOADSTRING=_T();HWND hSend;HWND hText;/ Global Variables:HINSTANCE hInst;/ current instanceTCHAR szTitleMAX_LOADSTRING;/ The title bar textTCHAR szWindowClassMAX_LOADSTRING;/ The title bar text/ Foward declarations of functions included in this code module:ATOMMyRegisterClass(HINSTANCE hInstance);BOOLInitInstance(HINSTANCE, int);LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM);LRESULT CALLBACKAbout(HWND, UINT, WPARAM, LPARAM);int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) / TODO: Place code here.MSG msg;HACCEL hAccelTable;/ Initialize global stringsLoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);LoadString(hInstance, IDC_SERVER, szWindowClass, MAX_LOADSTRING);MyRegisterClass(hInstance);/ Perform application initialization:if (!InitInstance (hInstance, nCmdShow) return FALSE;hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SERVER);/ Main message loop:while (GetMessage(&msg, NULL, 0, 0) if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg) TranslateMessage(&msg);DispatchMessage(&msg);return msg.wParam;/ FUNCTION: MyRegisterClass()/ PURPOSE: Registers the window class./ COMMENTS:/ This function and its usage is only necessary if you want this code/ to be compatible with Win32 systems prior to the RegisterClassEx/ function that was added to Windows 95. It is important to call this function/ so that the application will get well formed small icons associated/ with it./ATOM MyRegisterClass(HINSTANCE hInstance)WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX); wcex.style= CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc= (WNDPROC)WndProc;wcex.cbClsExtra= 0;wcex.cbWndExtra= 0;wcex.hInstance= hInstance;wcex.hIcon= LoadIcon(hInstance, (LPCTSTR)IDI_SERVER);wcex.hCursor= LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName= (LPCSTR)IDC_SERVER;wcex.lpszClassName= szWindowClass;wcex.hIconSm= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);return RegisterClassEx(&wcex);/ FUNCTION: InitInstance(HANDLE, int)/ PURPOSE: Saves instance handle and creates main window/ COMMENTS:/ In this function, we save the instance handle in a global variable and/ create and display the main program window./BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) HWND hWnd; hInst = hInstance; / Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) return FALSE; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE;/ FUNCTION: WndProc(HWND, unsigned, WORD, LONG)/ PURPOSE: Processes messages for the main window./ WM_COMMAND- process the application menu/ WM_PAINT- Paint the main window/ WM_DESTROY- post a quit message and return/LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)int wmId, wmEvent;PAINTSTRUCT ps;HDC hdc;TCHAR szHelloMAX_LOADSTRING;LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);switch (message) case WM_CREATE:WSAStartup(0x0101,&ws);S=socket(AF_INET,SOCK_DGRAM,0);SetSockAddr(&lAddr,LPort,LIP);bind(S,(struct sockaddr*)&lAddr,sizeof(lAddr);WSAAsyncSelect(S,hWnd,WM_USER+1,FD_READ);hSend=CreateWindow(_T(button),_T(send),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,850,380,100,40,hWnd,(HMENU)1,hInst,NULL);hText=CreateWindow(_T(edit),_T(),WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL|ES_AUTOVSCROLL,10,350,830,100,hWnd,(HMENU)2,hInst,NULL);break;case WM_USER+1:switch(LOWORD(lParam)case FD_READ:d=recvfrom(S,Msg,sizeof(Msg),0,(struct sockaddr*)&rAddr,&l);Msgd=0;char buf88;wsprintf(buf,Client:n%sn,Msg);strcat(messageDisplay,buf);RedrawWindow(hWnd,NULL,NULL,RDW_INVALIDATE|RDW_UPDATENOW|RDW_ERASE);break;break;case WM_COMMAND:wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); / Parse the menu selections:switch (wmId)case 1:TCHAR messageInputMAX_LOADSTRING;GetWindowText(hText,messageInput,MAX_LOADSTRING);strcat(messageDisplay,Server:n);strcat(messageDisplay,messageInput);strcat(messageDisplay,n);RedrawWindow(hWnd,NULL,NULL,RDW_INVALIDATE|RDW_UPDATENOW|RDW_ERASE);sendto(S,messageInput,strlen(messageInput),0,(struct sockaddr*)&rAddr,l);break;case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break;case IDM_EXIT: DestroyWindow(hWnd); break;default: return DefWindowProc(hWnd, message, wParam, lParam);break;case WM_PAINT:hdc = BeginPaint(hWnd, &ps);/ TODO: Add any drawing code here.RECT rt;GetClientRect(hWnd, &rt);DrawText(hdc, messageDisplay, strlen(messageDisplay), &rt, DT_LEFT);EndPaint(hWnd, &ps);break;case WM_DESTROY:WSAAsyncSelect(S,hWnd,0,0);closesocket(S);WSACleanup();PostQuitMessage(0);break;default:return DefWindowProc(hWnd, message, wParam, lParam); return 0;/ Mesage handler for about box.LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)switch (message)case WM_INITDIALOG:return TRUE;case WM_COMMAND:if (LOWORD(wParam) = IDOK | LOWORD(wParam) = IDCANCEL) EndDialog(hDlg, LOWORD(wParam);return TRUE;break; return FALSE;Client端#include#include#pragma comment(lib,Ws2_32.lib)int main(int argc,char*argv)int sockClient;int port;struct sockaddr_in servAddr,myAddr;int n;char buf100;int alen;WSADATA wsaData;WSAStartup(0x0202,&wsaData);memset(char*)&servAddr,0,sizeof(servAddr);servAddr.sin_family=AF_INET;myAddr.sin_family=AF_INET;myAddr.sin_addr.s_addr=INADDR_ANY;myAddr.sin_port=0;if(argc2)servAddr.sin_addr.s_addr=inet_addr(argv1);if(servAddr.sin_addr.s_addr=INADDR_NONE)fprintf(stderr,bad ip address %sn,argv1);exit(1);port=atoi(argv2);if(port0)servAddr.sin_port=htons(u_short)port);elsefprintf(stderr,bad port number %sn,argv2);exit(1);elsefprintf(stderr,input the ip address and port number);exit(1);sockClient=socket(PF_INET,SOCK_DGRAM,0);if(sockClient0)fprintf(stderr,socket creation failedn);exit(1)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 湖南省郴州市2024-2025学年八年级下学期5月期中英语试题(含笔试答案无听力答案、原文及音频)
- 建筑施工特种作业-建筑起重机械安装拆卸工(施工升降机)真题库-3
- 日食月食地理题目及答案
- 国家标准关于《机械制图》的基本规定(二)
- 2023-2024学年山东省滨州市高二下学期7月期末数学试题(解析版)
- 2023-2024学年湖南省株洲市炎陵县高二下学期6月期末考试数学试题(解析版)
- 2023-2024学年河南省安阳市林州市高二下学期期末考试数学试卷(解析版)
- 2025届河南省新乡市高三二模语文试题(解析版)
- 2024-2025学年浙江省杭州市联谊学校高二3月月考语文试题(解析版)
- 江苏阿尔法生物制药有限公司新建制剂、生物发酵及机械加工建设项目环评资料环境影响
- 汽车吊塔吊防碰撞安全技术交底
- 一年级下册音乐教案 (简谱) (演唱)同坐小竹排(7) 湘艺版
- 砂石料加工厂劳务外包服务采购项目
- 列车网络控制技术-复习打印版
- 福建高考名著《红楼梦》填空题+答案
- 商标法期末复习
- 材料力学计算试题(库)完整
- 投资控股集团有限公司安全生产责任制暂行办法
- NGW型行星齿轮传动系统的优化设计
- 三年级上册数学教案-第七单元 分数的初步认识 苏教版
- 2019第五版新版PFMEA 注塑实例
评论
0/150
提交评论