??概要 ????本文分步介绍了如何在?Microsoft?Visual?Basic?.NET?中为?Microsoft?Windows?Sockets?(Winsock)?接口和网络连接编程。这个示例中使用了一个名为?TestTCPClient?的简单的传输控制协议?(TCP)?客户端应用程序和一个名为?TestTCPServer?的简单的?TCP?服务器端(侦听器)应用程序,它们使用?Windows?Sockets?接口相互进行通信。 一,创建?TCP?客户端应用程序? 1.启动?Microsoft?Visual?Studio?.NET。 2.在“文件”菜单上,单击“新建”,然后单击“项目”。 3.在“项目类型”下,单击“Visual?Basic?项目”。 4.在“模板”下,单击“控制台应用程序”。 将该项目命名为?TestTCPClient,然后单击“确定”。 默认情况下,系统会创建?Module1.vb。 5.使用下面的代码替换?Module1.vb?中的代码:Imports?System.Net.Sockets Imports?System.Text Class?CTestTCPClient ????Shared?Sub?Main() ????????Dim?tcpClient?As?New?System.Net.Sockets.TcpClient ????????'"Localhost"?string?is?used?when?the?client?and?the?listener?are?on?the?same?computer. ????????'If?the?listener?is?listening?at?a?computer?that?is?different?from?the?client,?provide?the?host?name?of?the?computer ????????'where?the?listener?is?listening. ????????tcpClient.Connect("Localhost",?8000) ????????Dim?networkStream?As?NetworkStream?=?tcpClient.GetStream() ????????If?networkStream.CanWrite?And?networkStream.CanRead?Then ????????????'?Do?a?simple?write. ????????????Dim?sendBytes?As?[Byte]()?=?Encoding.ASCII.GetBytes("Is?anybody?listening...") ????????????networkStream.Write(sendBytes,?0,?sendBytes.Length) ????????????'?Read?the?NetworkStream?into?a?byte?buffer. ????????????Dim?bytes(tcpClient.ReceiveBufferSize)?As?Byte ????????????networkStream.Read(bytes,?0,?CInt(tcpClient.ReceiveBufferSize)) ????????????'?Output?the?data?received?from?the?host?to?the?console. ????????????Dim?returndata?As?String?=?Encoding.ASCII.GetString(bytes) ????????????Console.WriteLine(("TCP?Server?returned:?"?+?returndata)) ????????Else ????????????If?Not?networkStream.CanRead?Then ????????????????Console.WriteLine("Could?not?write?data?to?data?stream") ????????????????tcpClient.Close() ????????????Else ????????????????If?Not?networkStream.CanWrite?Then ????????????????????Console.WriteLine("Could?not?read?data?from?data?stream") ????????????????????tcpClient.Close() ????????????????End?If ????????????End?If ????????End?If ????????'?Pause?to?let?the?user?view?the?console?output. ????????Console.ReadLine() ????End?Sub End?Class ? ???这一代码将创建“tcpClient”类的一个新实例,调用“Connect”方法,然后使用“NetworkStream”类的“GetStream()”方法访问下层数据流。系统将把消息转换为一个字节数组,将其发送到数据流,然后读取来自?TCP?服务器(侦听器)应用程序响应的数据流。 ??创建?TCP?服务器端(侦听器)应用程序 1.启动?Microsoft?Visual?Studio?.NET。 2.在“文件”菜单上,单击“新建”,然后单击“项目”。 3.在“项目类型”下,单击“Visual?Basic?项目”。 4.在“模板”下,单击“控制台应用程序”。 将该项目命名为?TestTCPServer,然后单击“确定”。 默认情况下,系统会创建?Moudle1.vb。 5.使用下面的代码替换?Module1.vb?中的代码:Imports?System.Net.Sockets Imports?System.net Imports?System.Text Imports?System.Net.DnsPermissionAttribute Imports?System.Security.Permissions 'DnsPermissionAttribute?specifies?permission?to?request?information?from?Domain?Name?Servers.
?Class?CTestTCPServer
????Shared?Sub?Main()
????????'Listening?must?be?on?the?same?port?that?the?client?is?connected?on.?
????????Const?portNumber?As?Integer?=?8000
????????'"Localhost"?string?is?used?when?the?client?and?the?listener?are?on?the?same?computer. ????????'If?the?listener?is?listening?at?a?computer?that?is?different?from?the?client,?then?provide?the?host?name?of?the?computer ????????'where?the?listener?is?listening. ????????Dim?tcpListener?As?New?TcpListener(CType(Dns.Resolve("Localhost").AddressList(0),?IPAddress),?portNumber) ????????'Comment?the?previous?line?and?uncomment?the?following?line?if?you?are?using?Visual?Basic?.NET?(2003). ????????'Dim?tcpListener?As?New?TcpListener(portNumber) ????????tcpListener.Start() ????????Console.WriteLine("TCP?Server?is?up?and?waiting?for?Client?connection...") ????????Try ????????????''Accept?the?pending?client?connection?and?return?a?TcpClient?for?communication.? ????????????Dim?tcpClient?As?TcpClient?=?tcpListener.AcceptTcpClient() ????????????Console.WriteLine("Connection?accepted.") ????????????'?Get?the?data?stream. ????????????Dim?networkStream?As?NetworkStream?=?tcpClient.GetStream() ????????????'?Read?the?data?stream?into?a?byte?array. ????????????Dim?bytes(tcpClient.ReceiveBufferSize)?As?Byte ????????????networkStream.Read(bytes,?0,?CInt(tcpClient.ReceiveBufferSize)) ????????????'?Return?the?data?received?from?the?client?to?the?console. ????????????Dim?clientdata?As?String?=?Encoding.ASCII.GetString(bytes) ????????????Console.WriteLine(("Client?sent:?"?+?clientdata)) ????????????Dim?responseString?As?String?=?"Successfully?connected?to?TCP?server." ????????????Dim?sendBytes?As?[Byte]()?=?Encoding.ASCII.GetBytes(responseString) ????????????networkStream.Write(sendBytes,?0,?sendBytes.Length) ????????????Console.WriteLine(("Message?Sent?by?TCP?Server?/>?:?"?+?responseString)) ????????????'Close?TcpListener?and?TcpClient. ????????????tcpClient.Close() ????????????tcpListener.Stop() ????????????Console.WriteLine("Exit") ????????????Console.ReadLine() ????????Catch?e?As?Exception ????????????Console.WriteLine(e.ToString()) ????????????Console.ReadLine() ????????End?Try ????End?Sub End?Class TestTCPServer?应用程序将为该端口创建一个“tcpListener”类,调用“Start()”方法,然后使用“AcceptTcpClient()”方法接受挂起的客户端请求。“AcceptTcpClient()”方法返回您可以用来发送和接收数据的“tcpClient”对象。 ???测试示例 要测试示例,请生成并运行?TestTCPServer?应用程序,然后生成并运行?TestTCPClient?应用程序。控制台窗口中的消息表明?TestTCPClient?应用程序和?TestTCPServer?应用程序正在使用?Windows?Sockets?接口进行通信。
来源:学海 作者:penglsh |