Socket Socket TcpClient Socket.Connect TcpClient.Connect Socket.Send / Receive NetworkStream 6-5

Similar documents
Microsoft Word - CH06.doc

(Methods) Client Server Microsoft Winsock Control VB 1 VB Microsoft Winsock Control 6.0 Microsoft Winsock Control 6.0 1(a). 2

/ / (FC 3)...

Java Access 5-1 Server Client Client Server Server Client 5-2 DataInputStream Class java.io.datainptstream (extends) FilterInputStream InputStream Obj

Chap6.ppt

其中,addressFamily 参数指定 Socket 使用的寻址方案,socketType 参数指定 Socket 的类型,protocolType 参数指定 Socket 使用的协议 下面的示例语句创建一个 Socket, 它可用于在基于 TCP/IP 的网络 ( 如 Internet) 上通

VoIP Make a Rtp Call VoIP Abstract... 2 VoIP RTP...3 Socket IP...9 Config Two Voice-hub

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

2 WF 1 T I P WF WF WF WF WF WF WF WF 2.1 WF WF WF WF WF WF

PIC_SERVER (11) SMTP ( ) ( ) PIC_SERVER (10) SMTP PIC_SERVER (event driven) PIC_SERVER SMTP 1. E-

中 文 摘 要 我 們 所 製 作 的 專 題 稱 為 網 路 多 人 連 線 遊 戲 大 廳 (Multi person Segment game lobby) 主 要 目 的 是 在 這 個 以 網 路 世 界 為 主 現 實 為 輔 的 時 代 中, 可 以 讓 各 地 的 網 友 在 這 新

09 (File Processes) (mkdir) 9-3 (createnewfile) 9-4 (write) 9-5 (read) 9-6 (deletefile) 9-7 (deletedir) (Exercises)

UDP 8.2 TCP/IP OSI OSI 3 OSI TCP/IP IP TCP/IP TCP/IP Transport Control Protocol TCP User Datagram Protocol UDP TCP TCP/IP IP TCP TCP/IP TC

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

untitled

概述

TCP/IP TCP/IP OSI IP TCP IP IP TCP/IP TCP/IP

JavaIO.PDF

Learning Java

ch08.PDF

W. Richard Stevens UNIX Sockets API echo Sockets TCP OOB IO C struct C/C++ UNIX fork() select(2)/poll(2)/epoll(4) IO IO CPU 100% libevent UNIX CPU IO

ebook67-1

ebook140-8


ARP ICMP

内 容 简 介 本 书 是 一 本 关 于 语 言 程 序 设 计 的 教 材, 涵 盖 了 语 言 的 基 本 语 法 和 编 程 技 术, 其 中 包 含 了 作 者 对 语 言 多 年 开 发 经 验 的 总 结, 目 的 是 让 初 学 的 读 者 感 受 到 语 言 的 魅 力, 并 掌

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

untitled

Visual Basic D 3D

chp6.ppt


2 奋 斗 的 历 程 丰 硕 的 成 果 ( 二 ) 化 进 程 图 l 清 华 大 学 综 合 教 务 系 统 数 据 管 理 流 程 二 教 务 系 统 的 一 级 管 理 模 式 教 务 系 统 配 合 学 校 体 制 改 革, 将 二 级 教 务 管 理 变 为 一 级 管 理 模 式 减


(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

Microsoft Word - 小心翼翼的二十一點N.doc

穨CAS1042中文手冊.doc

untitled

Microsoft Word - 46.doc

Microsoft Word - 第3章.doc

Microsoft Word - kangqiang.doc

ebook12-1

新・解きながら学ぶJava

SL2511 SR Plus 操作手冊_單面.doc

前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii

30.00% 25.00% 25.00% 22.50% 20.00% 15.00% 12.50% 15.00% 12.50% 10.00% 7.50% 5.00% 2.50% 2.50% 0.00% 文 学 理 学 工 学 法 学 教 育 学 管 理 学 历 史 学 艺 术 学 ( 三 ) 学 生

27 :OPC 45 [4] (Automation Interface Standard), (Costom Interface Standard), OPC 2,,, VB Delphi OPC, OPC C++, OPC OPC OPC, [1] 1 OPC 1.1 OPC OPC(OLE f

CHAPTER VC#

<4D F736F F F696E74202D20A1B6CFEEC4BFD2BB20B3F5CAB6BCC6CBE3BBFACDF8C2E7A1B7C8CECEF1C8FD20CAECCFA A1A24950D0ADD2E9BACD4950B5D8D6B72E707074>

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

國家圖書館典藏電子全文

多層次傳銷與獎金系統

C6_ppt.PDF

IP505SM_manual_cn.doc

Java 1 Java String Date

EJB-Programming-4-cn.doc

untitled

RunPC2_.doc

叮当旺业通

Microsoft Word - ch04三校.doc

FileMaker 15 ODBC 和 JDBC 指南

ebook140-11

嵌入式Linux块设备驱动开发解析

TC35短信发送程序设计

穨IC-1000

自由軟體教學平台

ThreeDtunnel.doc

一个开放源码的嵌入式仿真环境 ― SkyEye

epub

第 1 章 概 述 1.1 计 算 机 网 络 在 信 息 时 代 中 的 作 用 1.2 计 算 机 网 络 的 发 展 过 程 *1.2.1 分 组 交 换 的 产 生 *1.2.2 因 特 网 时 代 *1.2.3 关 于 因 特 网 的 标 准 化 工 作 计 算 机 网 络 在

Data Server_new_.doc

9 Internet 10 Internet

untitled

Chapter 9: Objects and Classes

(HMI) IO A

R3105+ ADSL

计 算 机 系 统 应 用 年 第 25 卷 第 4 期 线 程 的 复 用 [2,3]. 通 常 情 况 下, 服 务 器 端 程 序 在 启 动 时 创 建 若 干 数 量 的 线 程 对 象 并 缓 存 起 来, 此 时 它 们 处 于

2 Java 语 言 程 序 设 计 教 程 简 单 性 Java 语 言 的 语 法 与 C 语 言 和 C++ 语 言 很 接 近, 使 得 大 多 数 程 序 员 很 容 易 学 习 和 使 用 Java 另 一 方 面,Java 丢 弃 了 C++ 中 很 少 使 用 的 很 难

1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6:

mv t ï Catch ex As Exception MessageBox.Show(" «t ï ") Finally ' myconnection.close() End Try SqlConnection(ByVal connectionstring As String) pâ nç á

untitled

untitled

3 Driver do Microsoft Access (*.mdb) hisdata IFIX 1.4

Basic System Administration

PowerPoint Presentation

handsome-招股书-新规则-final-version-0422.PDF








1

02

C3_ppt.PDF

05_06_浙江省发展和改革委员会网上并联审批系统实施案例.PDF

C H A P T E R 7 Windows Vista Windows Vista Windows Vista FAT16 FAT32 NTFS NTFS New Technology File System NTFS

untitled

Transcription:

6 6-1 6-2 Socket 6-2-1 Socket 6-2-2 TcpClient 6-3 6-3-1 Socket.Connect 6-3-2 TcpClient.Connect 6-4 6-4-1 Socket.Send / Receive 6-4-2 NetworkStream 6-5 6-5-1 Socket.Close 6-5-2 TcpClient.Close 6-6 DateTime

Socket Microsoft.NET Framework Socket TcpListener Microsoft.NET Framework 6-1 Microsoft.NET Framework System.Net.Sockets.Socket System.Net.Sockets.TcpClient System.Net.Sockets.Socket 6-2

6 System.Net.Sockets.TcpClient Socket 1. Socket DNS IP Internet 2. 3. 4. Socket Microsoft.NET Framework Socket TcpClient 6-2 Socket System.Net.Sockets Socket TcpClient 6-3

6-2-1 Socket System.Net.Sockets.Socket Bind Listen Accept Connect System.Net.Sockets.Socket Socket 5-2-1 Public Sub New (socketinformation As SocketInformation) Public Sub New (addressfamily As AddressFamily, _ sockettype As SocketType,protocolType As ProtocolType) Socket TCP Imports System.Net.Sockets Dim clientsocket As New Socket( _ AddressFamily.InterNetwork, SocketType.Stream, _ ProtocolType.Tcp) Catch ex As SocketException End Socket Socket IP Internet Socket Socket System.Net.IPEndPoint IP 6-4

6 Imports System.Net Dim serverip As IPAddress = _ Dns.Resolve("Server").AddressList(0) Dim serverhost As New IPEndPoint(serverIP, 80) Catch ex As ArgumentException End 6-2-2 TcpClient System.Net.Sockets.Socket Socket System.Net.Sockets.TcpClient TCP TcpClient Socket TCP Public Sub New () Public Sub New (family As AddressFamily) Public Sub New (localep As IPEndPoint) Public Sub New (hostname As String, port As Integer) 6-5

1. family System.Net.Sockets.AddressFamily IP 4-1-1 2. localep System.Net.IPEndPoint IP Socket localep 4-1-2 3. hostname DNS IP DNS ArgumentNullException 4. port Internet port String Int32.Parse() 4-1-2 System.ArgumentException family AddressFamily InterNetwork InterNetworkV6 System.ArgumentNullException localep hostname Nothing System.ArgumentOutOfRangeException port MinPort MaxPort System.Net.Sockets.SocketException TcpClient Socket ' TcpClient Dim tcpclient As New TcpClient() ' IP Dim ipaddress As IPAddress = _ Dns.Resolve(Dns.GetHostName()).AddressList(0) Dim LocalEP As New IPEndPoint(ipAddress, < >) 6-6

6 Dim tcpclient As New TcpClient(LocalEP) Catch e As Exception End ' Dim tcpclient As New TcpClient("< >", 80) Catch e As Exception End Socket TcpClient TCP Socket TcpClient Socket TcpClient Socket 6-3 Socket TcpClient Socket Socket TcpClient 6-3-1 Socket.Connect Socket Connect Public Sub Connect (remoteep As EndPoint) Public Sub Connect (address As IPAddress, port As Integer) 6-7

Public Sub Connect (addresses As IPAddress(), port As Integer) Public Sub Connect (host As String, port As Integer) 1. remoteep System.Net.IPEndPoint IP 4-1-2 2. address IP System.Net.IPAddress 4-1-1 3. host DNS 4. port 4-1-2 System.ArgumentNullException remoteep Nothing System.ArgumentOutOfRangeException port MinPort MaxPort System.Net.Sockets.SocketException Socket System.InvalidOperationException Socket System.NotSupportedException System.ObjectDisposedException Socket System.Security.SecurityException Socket Imports System.Net Imports System.Net.Sockets Dim clientsocket As New Socket( _ AddressFamily.InterNetwork, _ SocketType.Stream, ProtocolType.Tcp) Dim serverip As IPAddress = _ Dns.Resolve("Server").AddressList(0) 6-8

6 Dim serverhost As New IPEndPoint(serverIP, 80) clientsocket.connect(serverhost) Catch ex As SocketException End System.Net.Sockets.Socket Berkeley Socket Distribution API Socket Connect BSD Synchronous I/O Asynchronous Socket BeginConnect EndConnect BeginConnect Public Function BeginConnect (remoteep As EndPoint, _ callback As AsyncCallback, state As Object _ ) As IAsyncResult Public Function BeginConnect (address As IPAddress, _ port As Integer, requestcallback As AsyncCallback, _ state As Object) As IAsyncResult Public Function BeginConnect (addresses As IPAddress(), _ port As Integer, requestcallback As AsyncCallback, _ state As Object) As IAsyncResult Public Function BeginConnect (host As String, _ port As Integer, requestcallback As AsyncCallback, _ state As Object) As IAsyncResult 6-9

1. remoteep System.Net.IPEndPoint IP 4-1-2 2. address IP System.Net.IPAddress 4-1-1 3. host DNS 4. port 4-1-2 5. callback AsyncCallback Callback 6. state IAsyncResult System.ArgumentNullException remoteep Nothing System.Net.Sockets.SocketException Socket System.ObjectDisposedException Socket Dim clientsocket As New Socket( _ AddressFamily.InterNetwork, _ SocketType.Stream, ProtocolType.Tcp) Dim serverip As IPAddress = _ Dns.Resolve("Server").AddressList(0) Dim serverhost As New IPEndPoint(serverIP, 80) clientsocket.beginconnect(serverhost, _ New AsyncCallback( _ AddressOf Async_Send_Receive.Connect_Callback), _ clientsocket) 6-10

6 Catch ex As Exception End EndConnect Public Sub EndConnect (asyncresult As IAsyncResult) asyncresult System.ArgumentNullException asyncresult Nothing System.ArgumentException asyncresult BeginConnect System.Net.Sockets.SocketException Socket System.ObjectDisposedException Socket 6-3-2 TcpClient.Connect Socket Connect TcpClient Connect Public Sub Connect (remoteep As IPEndPoint) Public Sub Connect (address As IPAddress, port As Integer) Public Sub Connect (hostname As String, port As Integer) 6-11

Public Sub Connect (ipaddresses As IPAddress(), port As Integer) 1. remoteep System.Net.IPEndPoint IP 4-1-2 2. address IP System.Net.IPAddress 4-1-1 3. hostname DNS IP DNS ArgumentNullException 4. port 4-1-2 5. ipaddresses IP System.Net.IPAddress System.ArgumentNullException remoteepaddresshostname ipaddresses Nothing System.ArgumentOutOfRangeException port MinPort MaxPort System.Net.Sockets.SocketException Socket System.NotSupportedException System.ObjectDisposedException TcpClient System.Security.SecurityException Socket ' IPEndPoint Dim tcpclient As New TcpClient() Dim ipaddress As IPAddress = _ Dns.Resolve("<Server>").AddressList(0) Dim remoteep As New IPEndPoint(ipAddress, < >) 6-12

6 tcpclient.connect(remoteep) Catch e As Exception End ' IP Dim tcpclient As New TcpClient() Dim ipaddress As IPAddress = _ Dns.Resolve("<Server>").AddressList(0) tcpclient.connect(ipaddress, < >) Catch e As Exception End ' DNS Dim tcpclient As New TcpClient() tcpclient.connect("<server>", < >) Catch e As Exception End 6-13

Socket TcpClient TCP Socket TcpClient Connect Connect BeginConnect - EndConnect - 6-4 Request Response Socket TcpClient 6-4-1 Socket.Send / Receive 5-4-1 Socket Socket 1. Send Socket SendTo IP SendFile Socket 2. Receive Socket ReceiveFrom IP ReceiveMessageFrom Socket 5-4-1 6-14

6 6-4-2 NetworkStream Socket TcpClient TcpClient TcpClient TcpClient GetStream Input/Output Stream 5-4-2 Dim tcpclient As New TcpClient() Dim networkstream As NetworkStream = tcpclient.getstream() If networkstream.canread Then Dim bytes(tcpclient.receivebuffersize) As Byte networkstream.read(bytes, 0, _ CInt(tcpClient.ReceiveBufferSize)) Dim msg As String = Encoding.ASCII.GetString(bytes) End If Catch ex As Exception End 6-15

Dim tcpclient As New TcpClient() Dim networkstream As NetworkStream = tcpclient.getstream() If networkstream.canwrite Then Dim msg As [Byte]() = Encoding.ASCII.GetBytes("") networkstream.write(msg, 0, msg.length) End If Catch ex As Exception End NetworkStream Public Sub New (socket As Socket) Public Sub New (socket As Socket, ownssocket As Boolean) Public Sub New (socket As Socket, access As FileAccess) Public Sub New (socket As Socket, _ access As FileAccess, ownssocket As Boolean) Socket Socket Socket SendSendToSendFileReceiveReceiveFrom ReceiveMessageFrom NetworkStream Dim clientsocket As New Socket( _ AddressFamily.InterNetwork, _ SocketType.Stream, ProtocolType.Tcp) 6-16

6 Dim serverip As IPAddress = _ Dns.Resolve("Server").AddressList(0) Dim serverhost As New IPEndPoint(serverIP, 80) clientsocket.connect(serverhost) Dim networkstream As New NetworkStream(clientSocket) Catch ex As SocketException End Socket TcpClient TCP Socket TcpClient Receive ReceiveFrom ReceiveMessageFrom Send SendFile SendTo Read ReadByte Write WriteByte - Close 6-17

6-5 Socket TcpClient 6-5-1 Socket.Close Socket Socket Close Socket Public Sub Close Public Sub Close (timeout As Integer) timeout Socket Close Socket Socket Connected False Public ReadOnly Property Connected As Boolean Socket Shutdown Socket Public Sub Shutdown (how As SocketShutdown) how System.Net.Sockets.SocketShutdown SocketShutdown.Send SocketShutdown.Receive SocketShutdown.Both System.Net.Sockets.SocketException Socket System.ObjectDisposedException Socket 6-18

6 clientsocket.shutdown(socketshutdown.both) clientsocket.close() If clientsocket.connected Then End If 6-5-2 TcpClient.Close TcpClient TcpClient TcpClient Close Public Sub Close() System.Net.Sockets.SocketException 6-19

Dim tcpclient As New TcpClient() tcpclient.close() 6-6 DateTime DateTime Socket TcpClient 5-8 5-9 Client/Server 6-1 DateTimeClient.vb Imports System Imports System.IO Imports System.Net Imports System.Net.Sockets Imports System.Text 6-20

6 Public Class DateTimeClient Public Shared Sub Main() Dim args As String() = Environment.GetCommandLineArgs() If (args.length < 2) Then Console.WriteLine("Usage: _ DateTimeClient [DateTimeServer DNS/IP]") Exit Sub End If Dim serverhost As String = args(1) Dim hostadd As IPAddress = _ Dns.Resolve(serverHost).AddressList(0) Dim EPhost As New IPEndPoint(hostadd, 13) Dim clientsocket As New Socket( _ AddressFamily.InterNetwork, _ SocketType.Stream, ProtocolType.Tcp) clientsocket.connect(ephost) Catch ex As Exception Console.WriteLine(ex.ToString()) End Dim recvbytes(1024) As Byte Dim i As Integer = clientsocket.receive( _ recvbytes, 0, clientsocket.available, _ SocketFlags.None) Dim datetime As String = _ Encoding.ASCII.GetString(recvbytes, 0, i) Console.WriteLine("Receive {0} bytes.", i) Console.WriteLine("Current server date/time: {0}", _ datetime) 6-21

clientsocket.shutdown(socketshutdown.both) clientsocket.close() Catch ec As Exception Console.WriteLine(ec.StackTrace.ToString()) Exit Sub End Console.WriteLine("Press any key to exit.") Console.Read() End Sub End Class Socket DateTime 13 Socket Connect remoteep 13 DOS DOS DNS IP System.Environment GetCommandLineArgs Dim args As String() = Environment.GetCommandLineArgs() Dim serverhost As String = args(1) System.Environment GetCommandLineArgs Java main() args public static void main(string[] args) { String host; host = args[0]; } 6-22

6 Socket Receive Receive Encoding.ASCII.GetString Byte String Dim recvbytes(1024) As Byte Dim i As Integer = clientsocket.receive( _ recvbytes, 0, clientsocket.available, _ SocketFlags.None) Dim datetime As String = Encoding.ASCII.GetString(_ recvbytes, 0, i) 6-1 1. 5-8 5-9 DateTimeServer.exe 2. DOS 6-1\Bin IP 127.0.0.1 localhost DateTimeClient 127.0.0.1 3. C:\6-1\bin>DateTimeClient 127.0.0.1 Receive 21 bytes. 6-23

Current server date/time: 2008/5/10 10:26:46 Press any key to exit. 4. C:\5-8\bin>DateTimeServer DateTime server started at: 127.0.0.1:13 Client: 127.0.0.1:1031 Server: 127.0.0.1:13 To Client: 127.0.0.1:1031: 2008/7/9 10:26:46 6-2 TcpClient DateTime DNS IP TcpClient TcpClient Connect GetStream NetworkStream tcpclient.connect(serverhost, 13) networkstream = tcpclient.getstream() Catch exc As Exception End NetworkStream Read Read Encoding.ASCII.GetString Byte String Dim recvbytes(tcpclient.receivebuffersize) As Byte Dim i As Integer = networkstream.read( _ recvbytes, 0, CInt(tcpClient.ReceiveBufferSize)) Dim datetime As String = Encoding.ASCII.GetString(_ recvbytes, 0, i) 6-24

6 6-2 Client/Server Client/Server [1] Microsoft Developer Network. [2] Stevens W. Richard, UNIX Network Programming, Prentice Hall, 1991. [3] Stevens W. Richard, TCP/IP Illustrated Volume I, Addison-Wesley, 1996. 6-25

6-26