网络协议与套接字编程.ppt_第1页
网络协议与套接字编程.ppt_第2页
网络协议与套接字编程.ppt_第3页
网络协议与套接字编程.ppt_第4页
网络协议与套接字编程.ppt_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第9章网络协议与套接字编程 9 1进程和线程9 2C 中线程的开发9 3TCP IP网络协议9 4套接字编程技术9 5数据流 9 1进程和线程 基本概念进程在Windows中 系统能够同时运行多个程序 每一个正在运行的程序称为一个进程线程同一个进程又可以分成若干个独立的执行流 我们称之为线程 线程是操作系统向其分配处理器时间的基本单位 线程可执行进程的任何一部分代码 包括当前由另一线程执行的部分 并行性 并发处理 并发处理指计算机真正的或者是表面上呈现的同时处理 进程 线程和并行性在程序开发中的作用 程序实例 进程的使用 创建一个C Windows应用程序 设计界面如图 按钮事件 privatevoidbuttonStart Click objectsender System EventArgse 启动Notepad exe进程 myProcess Start privatevoidbuttonStop Click objectsender System EventArgse Process myProcesses 创建新的Process组件的数组 并将它们与指定的进 程名称 Notepad 的所有进程资源相关联 myProcesses Process GetProcessesByName Notepad foreach ProcessinstanceinmyProcesses 设置终止当前线程前等待的毫秒数instance WaitForExit 1000 instance CloseMainWindow privatevoidbuttonView Click objectsender System EventArgse this listBox1 Items Clear Process processes 创建Process类型的数组 并将它们与系统内所有进程相关联processes Process GetProcesses foreach Processpinprocesses 将每个进程名和进程开始时间加入listBox1中this listBox1 Items Add p ProcessName p StartTime ToShortTimeString 9 2C 中线程的开发 基本概念 操作 1 启动线程2 让线程休眠3 销毁线程4 ThreadState属性5 线程的优先级6 线程池 程序实例 启动线程 usingSystem usingSystem Threading classTestThread publicstaticvoidMain Threadthread1 newThread newThreadStart Method1 Threadthread2 newThread newThreadStart Method2 thread1 Start thread2 Start Console Read publicstaticvoidMethod1 for inti 0 i 1000 i Console Write a publicstaticvoidMethod2 for inti 0 i 1000 i Console Write b 9 3TCP IP网络协议 TCP IP协议实际上是一组协议的总称 它是一个4层的网络体系结构 应用层 传输层 网络层 网络接口层 这个4层的网络体系结构包括100多个相互关联的协议 由于IP是网络层最主要的协议 TCP是传输层最主要的协议 因此简称为TCP IP协议 9 4套接字编程技术 基本概念IP地址和端口IP地址端口套接字面向连接的套接字在面向连接的套接字中 使用TCP协议来建立两个IP地址端点之间的会话 一旦建立了这种连接 就可以在设备之间可靠的传输数据 为了建立面向连接的套接字 服务器和客户端必须分别进行编程 面向无连接的套接字无连接的套接字不需要在网络设备之间发送连接信息 因此 很难确定谁是服务器谁是客户机 面向连接的套接字 上图 面向无连接的套接字编程 下图 9 4套接字编程技术 基本概念无阻塞编程技术无阻塞套接字当套接字是非阻塞模式的时候 就不会一直等待一个I O方法的完成 多路传输套接字Socket类提供的Select 方法 这个方法用于多路传输多个套接字实例 以便找到一个已经准备好进行读或写的套接字 异步套接字编程技术 9 4套接字编程技术 程序实例一 IPEndPoint类的使用usingSystem usingSystem Net classTestIPEndPoint publicstaticvoidMain IPAddressip IPAddress Parse 127 0 0 1 IPEndPointiep newIPEndPoint ip 8000 Console WriteLine TheIPEndPointis 0 iep ToString Console WriteLine TheAddressis 0 iep Address Console WriteLine TheAddressFamilyis 0 iep AddressFamily Console WriteLine Themaxportnumberis 0 IPEndPoint MaxPort Console WriteLine Theminportnumberis 0 IPEndPoint MinPort Console Read 9 4套接字编程技术 程序实例二 使用套接字usingSystem usingSystem Net usingSystem Net Sockets classTestSocket publicstaticvoidMain IPAddressip IPAddress Parse 127 0 0 1 IPEndPointiep newIPEndPoint ip 8000 Socketsocket newSocket AddressFamily InterNetwork SocketType Stream ProtocolType Tcp Console WriteLine Blocking 0 socket Blocking Console WriteLine Connected 0 socket Connected socket Bind iep Console WriteLine LocalEndPoint 0 socket LocalEndPoint ToString Console Read 9 5数据流 基本概念流文件流网络流数据流的操作读写 9 5数据流 程序实例获取本地所有磁盘string drives Directory GetLogicalDrives for inti 0 i drives Length i 左对齐 宽度5位Console Write 0 5 drives i 输出结果类似如下形式 A C D E 9 5数据流 程序实例获取C盘根目录下的所有子目录string dirs Directory GetDirectories C for inti 0 i dirs Length i Console WriteLine dirs i 程序实例获取指定目录下的所有文件string files Directory GetFiles C for inti 0 i files Length i Console WriteLine files i 获取当前目录stringcurrentPath Directory GetCurrentDirectory 程序实例文本文件的读写usingSystem usingSystem IO publicclassTestFileStream staticvoidMain StreamWritersw newStreamWriter MyFile txt true System Text Encoding Unicode sw WriteLine 第一条语句 sw

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论