科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道VB中使用WinSock控件编写网络程序

VB中使用WinSock控件编写网络程序

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

WinSock控件能够通过UDP协议(用户数据报协议)或TCP协议(数据传输协议)连接到远程的机器并进行数据交换。

作者:佚名 来源:BLOG 2007年10月14日

关键字:

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共4页)

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

  上面就是创建一个简单的服务端应用程序的过程。然而,要完成整个过程,你还得创建一个客户端程序。
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章