Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

VB
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  

please help me...................
[/edit]
Posted
Updated 25-Nov-12 21:52pm
v2
Comments
OriginalGriff 24-Nov-12 1:35am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 26-Nov-12 3:56am    
You should move all the code out of your Form1_Load into the Button1_Click method, and then call Button1_Click from Form1_Load. Alternatively move all the tcp code into a separate function that can be called from the other two.

1 solution

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.
 
Share this answer
 
Comments
chandinig 26-Nov-12 0:17am    
Text moved to OP's original post.
chandinig 26-Nov-12 2:49am    
i have another problem can we add button click event with in form load...
Richard MacCutchan 26-Nov-12 4:01am    
Why don't you try it instead of waiting for someone to tell you the answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900