|
TCP连接入门
当用TCP控件创建应用程序的时候,必须首先明确你的程序是作为服务端还是客户端。创建服务端程序就意味着你的程序能够在指定的端口进行“监听”,而客户端则能够提出请求,服务端能够接受请求并实现连接。一旦连接建立起来,客户端和服务端就能够自由地进行通信。 创建服务端程序
下面是创建一个简单服务端程序的步骤:
·创建一个标准EXE工程;
·把默认窗体(Default form)的名字改为frmServer;
·把form的标题(caption)改为TCP Server;
·把Winsock控件拉到窗体中,并命名为tcpServer;
在窗体中添加2个文本框,分别命名为txtSendData和txtOutput
'在窗体中加入下列代码; Private Sub Form_Load() ' Set the LocalPort property to an integer. ' Then invoke the Listen method. tcpServer.LocalPort = 1001 tcpServer.Listen frmClient.Show ' Show the client form. End Sub Private Sub tcpServer_ConnectionRequest _ (ByVal requestID As Long) ' Check if the control's State is closed. If not, ' close the connection before accepting the new ' connection. If tcpServer.State <> sckClosed Then _ tcpServer.Close ' Accept the request with the requestID ' parameter. tcpServer.Accept requestID End Sub
Private Sub txtSendData_Change() ' The TextBox control named txtSendData ' contains the data to be sent. WhenEVEr the user ' types into the textbox, the string is sent ' using the SendData method. tcpServer.SendData txtSendData.Text End Sub
Private Sub tcpServer_DataArrival _ (ByVal bytesTotal As Long) ' Declare a variable for the incoming data. ' Invoke the GetData method and set the Text ' property of a TextBox named txtOutput to ' the data. Dim strData As String tcpServer.GetData strData txtOutput.Text = strData End Sub | 上面就是创建一个简单的服务端应用程序的过程。然而,要完成整个过程,你还得创建一个客户端程序。
创建TCP客户端程序
·在工程中添加一个新的窗体(form),并命名为frmClient;
·将窗体的标题(caption)改为TCP Client;
·添加一个Windsock控件到窗体中,命名为tcpCllient;
·添加2个文本框控件到frmClient窗体,分别命名为txtSend和txtOutput;
·添加一个按钮控件(CommandButton)到窗体,命名为cmdConnecti;
·将按钮控件标题(caption)改为Connect;
在窗体中添加下面代码:
注:确保将远程主机属性(RemoteHost property)改为你的计算机别名。
Private Sub Form_Load() ' The name of the Winsock control is tcpClient. ' Note: to specify a remote host, you can use ' either the IP address (ex: "121.111.1.1") or ' the computer's "friendly" name, as shown here. tcpClient.RemoteHost = "RemoteComputerName" tcpClient.RemotePort = 1001 End Sub
Private Sub cmdConnect_Click() ' Invoke the Connect method to initiate a ' connection. tcpClient.Connect End Sub
Private Sub txtSendData_Change() tcpClient.SendData txtSend.Text End Sub
Private Sub tcpClient_DataArrival _ (ByVal bytesTotal As Long) Dim strData As String tcpClient.GetData strData txtOutput.Text = strData End Sub | 以上代码就能创建一个简单的客/服应用程序。要试着让两者建立连接,可以运行工程,单击Connect。在任意一个txtSendData文本框中输入文本,同样的文本信息就会出现在另一个窗体的txtOutput文本框中出现。
接受多个连接请求
上面介绍的服务端程序智能接受一个连接请求。但是,通过创建一组控件,并使用同样的控件来接受多个连接请求也是可能的。在这种情况下,你不需要关闭连接,只要创建新的控件实例(通过配置它的索引属性),调用新的实例中的接受方法。
下面的代码中,假定在一个叫sckServer的窗体中有一个Winsock控件,它的索引属性设置为0。这样这个控件就是控件数组的一部分。在声明段中,生命一个模块级变量intMax。在窗体的载入事件中,intMax被设置为0,数组中第一个控件的本地端口属性被设置为1001。
监听方法在控件中被调用,它被作为“监听控件”。每个连接请求到来时,代码会测试看它的索引(Index)是否为0(监听控件的值),如果是0,监听控件中intMax值增1,并用这个值创建新的控件实例。新的控件实例被用来接受连接请求。
Private intMax As Long
Private Sub Form_Load() intMax = 0 sckServer(0).LocalPort = 1001 sckServer(0).Listen End Sub
Private Sub sckServer_ConnectionRequest _ (Index As Integer, ByVal requestID As Long) If Index = 0 Then intMax = intMax + 1 Load sckServer(intMax) sckServer(intMax).LocalPort = 0 sckServer(intMax).Accept requestID Load txtData(intMax) End If End Sub |
上一页 [1] [2] [3] 下一页
点此下载免费游戏更新软件
本文关键字:WinSock
作者:[佚名] 来源:[BLOG]
★★★
|