要求不高.只要能发送文字就行. 越简单越好 如果有详细注释的我可以在加分 不要拿网上的那写代码给我 我用的VB版本是VB6.0精简版 Private Sub Command1_Click()
Winsock1.localport = 2048
Winsock1.listen
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Form_Unload(Cancel As Integer)
Winsock1.Close
End Sub
Private Sub Timer1_Timer() Label1.Caption = Winsock1.State End Sub ==================== Private Sub Command1_Click()
Winsock1.RemoteHost = "222.243.90.87"
Winsock1.RemotePort = 2048
Winsock1.Connect
End Sub
Private Sub Form_Load()
End Sub
Private Sub Timer1_Timer() Label1.Caption = Winsock1.State End Sub
Private Sub Form1_Unload()
Winsock1.Close
End Sub 这段代码有错吗 为什么WINSOCK1.STATE总是6(错误)?
最新回答
梦在深巷。
2024-10-02 08:25:00
这个是我以前学习的一个实例。希望对你有帮助!!!
用VB实现客户——服务器(TCP/IP)编程实例
- 现在大多数语言都支持客户-服务器模式编程,其中VB给我们提供了很好的客户-服务器编程方式。下面我们用VB来实现TCP/IP网络编程。 TCP/IP协议是Internet最重要的协议。VB提供了WinSock控件,用于在TCP/IP的基础上进行网络通信。当两个应用程序使用Socket进行网络通信时,其中一个必须创建Socket服务器侦听,而另一个必须创建Socket客户去连接服务器。这样两个程序就可以进行通信了。 1.创建服务器,首先创建一个服务端口号。并开始侦听是否有客户请求连接。 建立一窗体,并向其增加一个Winsock控件(可在工程菜单中的部件项来添加此控件) 添加两文本框Text1,Text2,和一按钮Command1 Private Sub Form_Load() SockServer.LocalPort = 2000 ′服务器端口号,最好大于1000 SockServer.Listen ′开始侦听 End Sub Private Sub Form_Unload(Cancel As Integer) SockServer.Close End Sub Private Sub SockServer_Close() SockServer.Close End Sub Private Sub SockServer_ConnectionRequest(ByVal requestID As Long) SockServer.Close SockServer.Accept requestID ′表示客户请求连接的ID号 End Sub ′当客户向服务器发送数据到达后,产生DataArrival事件,在事件中接收数据,GetData方法接收数据。 Private Sub SockServer_Data Arrival(ByVal bytesTotal As Long) Dim s As String SockServer.GetData s Text1.Text = s End Sub 当我需要向客户发送数据时,只需调用SendData方法。 Private Sub Command1_Click() SockServer .SendData Text2.Text text1.text = text2.text text2.text = "" End Sub 2.创建客户。要创建客户连接服务器,首先设置服务器主机名,如IP地址、域名或计算机名,然后设置服务器端口,最后连接服务器。 建立一窗体,并向其增加一个Winsock控件(可在工程菜单中的部件项来添加此控件),取名为:SockC1。添加两文本框Text1,Text2,和一按钮Command1 Private Sub Form_Load() dim my as string my = SockCl.RemoteHostIP SockCl.RemoteHost = my ′表示服务器主机名 SockCl.RemotePort = 2000 ′表示服务器端口名 SockCl.Connect ′连接到服务器 End Sub Private Sub Form_Unload(Cancel As Integer) SockCl.Close End Sub Private Sub SockCl_Close() SockCl.Close End Sub Private Sub SockCl_DataArrival(ByVal bytesTotal As Long) Dim s As String SockCl.GetData s ′接收数据到文本框中 Text1.Text = s End Sub Private Sub Command1_Click() SockCl.SendData Text2.Text ′向服务器发送数据 text1.text = text2.text text2.text = "" End Sub 3.进行通信。把这两个窗体分别编译成两个EXE文件,服务器Server.exe和客户Client.exe程序,并把它们分别安装在服务器端和客户端,这样就可以实现两者通信了。
Private Sub Form_Load() Dim my As String my = SockCl.RemoteHostIP SockCl.RemoteHost = my SockCl.RemotePort = 2000 SockCl.Connect End Sub Private Sub Form_Unload(Cancel As Integer) SockCl.Close End Sub Private Sub SockCl_Close() SockCl.Close End Sub Private Sub SockCl_DataArrival(ByVal bytesTotal As Long) Dim s As String SockCl.GetData s Text1.Text = s End Sub Private Sub Command1_Click() SockCl.SendData Text2.Text Text1.Text = Text2.Text Text2.Text = "" End Sub
Private Sub Form_Load() SockServer.LocalPort = 2000 SockServer.Listen End Sub Private Sub Form_Unload(Cancel As Integer) SockServer.Close End Sub Private Sub SockServer_Close() SockServer.Close End Sub Private Sub SockServer_ConnectionRequest(ByVal requestID As Long) SockServer.Close SockServer.Accept requestID End Sub Private Sub Command1_Click() SockServer.SendData Text2.Text Text1.Text = Text2.Text Text2.Text = "" End Sub Private Sub SockServer_DataArrival(ByVal bytesTotal As Long) Dim s As String SockServer.GetData s Text1.Text = s End Sub
Private Sub Command1_Click() '发送数据 Winsock1.SendData Text1.Text End Sub Private Sub Form_Load() '通信 ‘设置端口 Winsock1.LocalPort = 1001 Winsock1.Listen '监听 Form2.Show End Sub Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) If Winsock1.State <> sckClosed Then''如果不是关闭状态
Winsock1.Close End If Winsock1.Accept requestID '接收客户端发送的连接请求 End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim getstr As String Winsock1.GetData getstr '获取对方发送过来的信息 Text1.Text = "" Text1.Text = getstr End Sub
FORM2:
Private Sub Command1_Click() '发送数据 Winsock1.SendData Text1.Text End Sub Private Sub Form_Load() Winsock1.RemoteHost = "127.0.0.1" '服务器IP Winsock1.RemotePort = 1001 '服务器端口 Winsock1.Connect '发送连接 End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim getstr As String Winsock1.GetData getstr Text1.Text = "" Text1.Text = getstr End Sub