如何用VB程序编写,实现局域网(TCPIP)对话,简单就行,不需要太多的代码

如何用VB程序编写,实现局域网(TCPIP)对话,简单就行,不需要太多的代码,第1张

要用到winsock控件,在窗体中用text控件用来发送信息,在设置一个text用来放接收者的IP.用Command画一个发送按钮.

双击窗体,在\"load\"事件中裂伏写上,me.caption=winsock1.localIPwinsock.Localprot=大点的数字

在发送按钮的\肆纤携"clock\"事件中写,ip=inputbox \"输入对方IP\",\"提示\"

在winsock1的控件中的\"Connect\"竖租事件中写,winsock1.RemoteHostIP=ip

winsock1.SendData=text and winsock1.localIP

OK!

就可以了,我有一段不太成熟的代码迹陆卜:

Imports System.Net

Imports System.Net.Sockets

Imports System.Threading

Imports System.IO

Public Class Form1

Dim TCPListenerRun, TCPClientRun As Boolean

Dim ControlTCPClient, ServerClient As TcpClient

Dim ControlTCPListener As TcpListener

Dim TCPClientT, TCPListenerT As Thread

Private ControlStream, SControlStream As NetworkStream

Private ControlReadStream, SControlReadStream As StreamReader

Private ControlWriteStream, SControlWriteStream As StreamWriter

'异姿穗步委托

Private Delegate Sub InvokeDelegate(ByVal iMessage As String)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try

If TCPListenerRun Then

TCPListenerT.Abort()

ControlTCPListener.Stop()

TCPListenerRun = False

Button1.Text = "启动服务端 "

TextBox2.AppendText( "服务器已停止! " & vbCrLf)

Else

ControlTCPListener = New TcpListener(IPAddress.Parse( "127.0.0.1 "), 13000)

ControlTCPListener.Start()

TCPListenerT = New Thread(AddressOf Listen)

TCPListenerT.Start()

TextBox2.AppendText( "等悉汪待连接... " & vbCrLf)

TextBox2.AppendText( "服务器已启动! " & vbCrLf)

TCPListenerRun = True

Button1.Text = "停止服务端 "

End If

Catch ex As Exception

TextBox2.AppendText(ex.Message & vbCrLf)

End Try

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Try

If TCPClientRun Then

TCPClientT.Abort()

ControlTCPClient.Close()

TCPClientRun = False

Button2.Text = "启动客户端 "

Else

ControlTCPClient = New TcpClient(New IPEndPoint(IPAddress.Parse( "127.0.0.1 "), 13001))

ControlTCPClient.Connect(New IPEndPoint(IPAddress.Parse( "127.0.0.1 "), 13000))

TCPClientRun = True

Button2.Text = "停止客户端 "

TCPClientT = New Thread(AddressOf Receive)

TCPClientT.Start()

ControlStream = ControlTCPClient.GetStream()

ControlReadStream = New StreamReader(ControlStream)

ControlWriteStream = New StreamWriter(ControlStream)

End If

Catch ex As SocketException

'If ex.ErrorCode = 10048 Then

ControlTCPClient.Close()

'End If

TCPClientRun = False

Button2.Text = "启动客户端 "

TextBox2.AppendText(ex.Message & vbCrLf)

End Try

End Sub

Private Sub Listen()

ServerClient = ControlTCPListener.AcceptTcpClient

TextBox2.BeginInvoke(New InvokeDelegate(AddressOf InvokeMethod_Receive), "成功连接... ")

SControlStream = ServerClient.GetStream()

SControlReadStream = New StreamReader(SControlStream)

SControlWriteStream = New StreamWriter(SControlStream)

While True

TextBox2.BeginInvoke(New InvokeDelegate(AddressOf InvokeMethod_Receive), SControlReadStream.ReadLine())

End While

End Sub

Private Sub Receive()

While True

TextBox2.BeginInvoke(New InvokeDelegate(AddressOf InvokeMethod_Receive), ControlReadStream.ReadLine())

'Console.WriteLine(ControlTCPClient.Available)

End While

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

TCPListenerRun = False

TCPClientRun = False

End Sub

Private Sub InvokeMethod_Receive(ByVal iMessage As String)

TextBox2.AppendText(iMessage & vbCrLf)

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

ControlWriteStream.WriteLine(TextBox1.Text)

ControlWriteStream.Flush()

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

SControlWriteStream.WriteLine(TextBox3.Text)

SControlWriteStream.Flush()

End Sub

End Class


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12563227.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存