We help IT Professionals succeed at work.

Check if string containts then loops again

desiredforsome
on
Medium Priority
230 Views
Last Modified: 2013-12-16
Ok I have this thing,

It reads a string and checks to see if it has a certain number in it. If the number exists it must re-generate the number and check again to see if its available.

Its sshing into a server and running a netstat |grep command in order to see if the port is avillable. if it is available it connects if it does not find it available it regernreates the port number and checks again.

However i am drawing a blank on this one.



If readport.Contains(portstring) Then
            MsgBox(portstring & "exists")
            Dim randomport As New System.Random()
            Dim randomid As Integer = randomport.Next(600, 6000)
            Dim randomportnumber As Integer = randomport.Next(400, 4000)
            portstring = randomportnumber.ToString()
            TextBox1.Text = "2 465 " & portstring

Open in new window

Comment
Watch Question

Vikram Singh SainiWeb Developer
CERTIFIED EXPERT

Commented:
Would you please let us know what actually you are trying to do?
Senior Software Engineer
CERTIFIED EXPERT
Commented:
You should try like this: (not tested, only logic is mentioned [See comments in code])

dim IsAvailable as Boolean=False

public sub GeneratePortNumber()
   
  while(Not IsAvailable)  ' Loop until port available and contains number you are looking for.

    If readport.Contains(portstring) Then  ' If number available, re-generate it
            MsgBox(portstring & "exists")
            Dim randomport As New System.Random()
            Dim randomid As Integer = randomport.Next(600, 6000)
            Dim randomportnumber As Integer = randomport.Next(400, 4000)
            portstring = randomportnumber.ToString()
            TextBox1.Text = "2 465 " & portstring

            IsAvailable = Available()       ' Check Available or not 
    Else
       portstring = FindPortString()   ' number not found, so find a a new portstring
    End if

  end while

  Connect()  ' Here you are exited from loop, so you have a port number available, connect to it.

end sub

public Function Available() as Boolean
   ' Your code to check availability of some port
   ' Or As you said "running a netstat |grep command"
 
   return True (If port available), False if not available
end sub 


public sub FindPortString()
  ' Your code to find portstring
end sub


public sub Connect()
   ' Your code to connect to some port.
end sub 

Open in new window

Author

Commented:
Hmm I am not quite sure on that.

It reads the output from netstat -ntl |grep "port number"
"port number" is a random generated intiger at runtime.

I have it check the output of netstat and see if the port it generated exists already. if it already exists then it regenerates and checks again if the port is available it connects otherweise it just keeps regenerating and rechecking.

I hope this helps a litle more

Author

Commented:
C# btw
Shahan AyyubSenior Software Engineer
CERTIFIED EXPERT

Commented:
What problem are you facing exactly ? when loop should terminate ?

Your original post seems to be VB code instead of C#. Well if you need c#, don't worry.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.