动态提供IP地址和端口号 [英] giving ip address and port number dynamically
问题描述
大家好,
您能帮助我如何在tcp客户端编程中动态给定IP地址和端口号吗(例如,对于按钮单击和表单加载,一次给定IP地址和端口号).请帮助我.
[根据OP的回复编辑]
我的确切问题是我在tcp客户端编程中使用了表单加载和按钮单击事件,因为我给了ipaddress和端口号2次(即,一次用于表单加载事件,一次给按钮单击事件).但是我想给ipaddress和一次动态加载表单端口和按钮单击事件的端口号.
我的代码是
Hi all,
can you help me how to give ip address and port number dynamically in tcp client programming(ie for button click and form load give ipaddress and port number one time).please help me.
[edit from OP''s response]
Whats my exact issue is I used Form load and button click events in tcp client programming in that i gave ipaddress and port number 2 times(ie one time for form load event and one time for button click event).But i want give ipaddress and port number dynamically for form load and button click event at one time.
my code is
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim RxResponse, RxResponse1, RxResponse2, RxResponse3, RxResponse4 As String
Dim TxCommand1 As String
Dim Buffer1() As Byte
Dim InBuff(1532) As Byte
Try
client.Connect(IpAddress, PortNum)
Catch ex As Exception
MsgBox("Connection with the server failed, and the following code was returned:" & ex.Message, 0, "connection error")
Exit Sub
End Try
TxCommand1 = "01ff000a44200000046a0200"
Buffer1 = System.Text.Encoding.Default.GetBytes(TxCommand1.ToCharArray)
client.GetStream().Write(Buffer1, 0, Buffer1.Length)
While Not client.GetStream().DataAvailable()
Application.DoEvents()
End While
If client.GetStream().DataAvailable() Then
client.GetStream().Read(InBuff, 0, InBuff.Length)
RxResponse = System.Text.Encoding.Default.GetString(InBuff)
RxResponse1 = Mid(RxResponse, 5, 8)
RxResponse2 = Mid(RxResponse1, 1, 4)
RxResponse3 = Mid(RxResponse1, 5, 4)
RxResponse4 = RxResponse3 & RxResponse2
TextBox4.Text = CStr(ConvertHexToSingle(RxResponse4))
End If
client.GetStream().Close()
'client.Close()
' End Sub
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Buffer(), Buffer1() As Byte
Dim TxCommand3, TxCommand2, TxCommand4, TxCommand5 As String
Dim sal, smart As Integer
Dim sal1, sal2, trak2, trak1, trak3, trak4 As String
Try
client.Connect(IpAddress, PortNum)
Catch ex As Exception
MsgBox("Connection with the server failed, and the following code was returned:" & ex.Message, 0, "connection error")
Exit Sub
End Try
Dim trak As Single
sal = CInt(Val(TextBox3.Text))
sal1 = Hex$(sal)
smart = Len(sal1)
Select Case smart
Case 1
sal2 = "000" & sal1
Case 2
sal2 = "00" & sal1
Case 3
sal2 = "0" & sal1
Case Else
sal2 = sal1
End Select
TxCommand2 = "03ff000a4420000004560100"
TxCommand3 = TxCommand2 & sal2
Buffer = System.Text.Encoding.Default.GetBytes(TxCommand3.ToCharArray)
'Sending a read command
client.GetStream().Write(Buffer, 0, Buffer.Length)
'Waiting for a response from an Ethernet adapter
While Not client.GetStream().DataAvailable()
Application.DoEvents()
End While
trak = CSng(TextBox2.Text)
trak1 = ConvertSingleToHex(trak)
trak2 = Mid(trak1, 1, 4)
trak3 = Mid(trak1, 5, 4)
trak4 = trak3 & trak2
TxCommand4 = "03ff000a4420000000000200"
TxCommand5 = TxCommand4 & trak4
Buffer1 = System.Text.Encoding.Default.GetBytes(TxCommand5.ToCharArray)
'Sending a read command
client.GetStream().Write(Buffer1, 0, Buffer1.Length)
'Waiting for a response from an Ethernet adapter
While Not client.GetStream().DataAvailable()
Application.DoEvents()
End While
client.GetStream().Close()
client.Close()
End Sub
请帮帮我...................
[/edit]
please help me...................
[/edit]
推荐答案
(sal) 聪明= Len(sal1) 选择 案例聪明 案例 1 sal2 = " &萨尔1 案例 2 sal2 = " &萨尔1 案例 3 sal2 = " &萨尔1 案例 其他 sal2 = sal1 结束 选择 TxCommand2 = " TxCommand3 = TxCommand2&萨尔2 Buffer = System.Text.Encoding.默认 .GetBytes(TxCommand3.ToCharArray) ' 发送读取命令 client.GetStream().Write(Buffer, 0 ,Buffer.Length) ' 等待来自以太网适配器的响应 同时 不是 client.GetStream().DataAvailable() Application.DoEvents() 结束 同时 trak = CSng (TextBox2.Text) trak1 = ConvertSingleToHex(trak) trak2 = Mid(trak1, 1 , 4 ) trak3 = Mid(trak1, 5 , 4 ) trak4 = trak3& trak2 TxCommand4 = " TxCommand5 = TxCommand4& trak4 Buffer1 = System.Text.Encoding.默认 .GetBytes(TxCommand5.ToCharArray) ' 发送读取命令 client.GetStream().Write(Buffer1, 0 ,Buffer1.Length) ' 等待来自以太网适配器的响应 同时 不是 client.GetStream().DataAvailable() Application.DoEvents() 结束 同时 client.GetStream().Close() client.Close() 结束 子
(sal) smart = Len(sal1) Select Case smart Case 1 sal2 = "000" & sal1 Case 2 sal2 = "00" & sal1 Case 3 sal2 = "0" & sal1 Case Else sal2 = sal1 End Select TxCommand2 = "03ff000a4420000004560100" TxCommand3 = TxCommand2 & sal2 Buffer = System.Text.Encoding.Default.GetBytes(TxCommand3.ToCharArray) 'Sending a read command client.GetStream().Write(Buffer, 0, Buffer.Length) 'Waiting for a response from an Ethernet adapter While Not client.GetStream().DataAvailable() Application.DoEvents() End While trak = CSng(TextBox2.Text) trak1 = ConvertSingleToHex(trak) trak2 = Mid(trak1, 1, 4) trak3 = Mid(trak1, 5, 4) trak4 = trak3 & trak2 TxCommand4 = "03ff000a4420000000000200" TxCommand5 = TxCommand4 & trak4 Buffer1 = System.Text.Encoding.Default.GetBytes(TxCommand5.ToCharArray) 'Sending a read command client.GetStream().Write(Buffer1, 0, Buffer1.Length) 'Waiting for a response from an Ethernet adapter While Not client.GetStream().DataAvailable() Application.DoEvents() End While client.GetStream().Close() client.Close() End Sub
请帮帮我...................
[/edit]
please help me...................
[/edit]
在您的表单中添加两个文本框,并要求用户填写详细信息.单击按钮后,您可以阅读这些项目,将它们转换为正确的格式,然后照常进行.
Add two text boxes to your form and ask the user to fill in the details. When the button is clicked you can read those items, convert them to the correct format and proceed as normal.
这篇关于动态提供IP地址和端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!