Sending a command to an IP

Jnox

Active member
Joined
Apr 19, 2006
Messages
25
Programming Experience
Beginner
I am supposed to create a program in VB that is able to send a command to an IP with just a click of a button.

It is supposed to be very similar to Telnet, but instead of connecting with Telnet and typing in the commands manually, I would like a program that is able to do it for me.

Here is what it is supposed to do:
Connects to an IP on port 23
and sends a command for example: WD_START, or WD_STOP or WD_SET_PROT 2

I know it doesnt seem very hard, but I am unable to find any info on how to do this in VB.

Would you guys help me with the code, or atleast direct me on where I can find info on this.

THANK YOU!
 
Check out my example. Is this what you needed? There are no comments, but if you don’t understand something then let me know.
 

Attachments

  • Remote Commands.zip
    36 KB · Views: 116
Master Zero,

Thank you so much. That is exactly what I need. I am going through your code and figuring out each step.

Form2 has nothing to do with Form1. Form2 code is only listening for output from the IP (ex. 127.0.0.1) on port 23, am I correct?
 
Last edited:
Yes, you're right. Here is the logical order of the code.

Client
Launch server.
Sleep for one second.
Try to connect with server.
Wait for user input.
Once user has selected a command, encode it and send it to the server.

Server
Listen on port 23 for any incoming connection.
When client connects, start listens for incoming data (commands).
Decode received data and process it.
Depending on what code is received, act accordingly. ß This is there area that you should add your code, the code that will be activated depending on what command is received (WD_START, WD_STOP, WD_SET_PROT). This is the “select case DataArray(0)” area.
 
What I am working on is a program for a weight checker. The weight checker runs on a seperate computer but on the same network. What this program is supposed to do is send a command to the weight checker (for example "WD_START"), what happens next is the weight checker begins constantly returning an article name and a weight value one after another, for example:

Chips, 100g
Chips, 105g
Chips, 98g

Master Zero

I am looking at your Form2 code, and if I am understanding it correctly, what it does it listens on port 23. When you press a command button in Form1, it sends it to 127.0.0.1, thus then it bounces off of 127.0.0.1 and Form2 code picks it up and displays it in the window.

Now I suppose my question would be, how do I edit form2 code to pick up just the output (Chips, 100g..... and next weight value) from that IP instead of just picking up the command sent to it?

Thank you, and please be patient with me as I am a complete newbie when it comes to networking with VB.
 
Ok, I am a bit confused. According to your goal plan:

Client sends “start” command to server.
Client à Server (command)

Server than sends back a list of paired items (chips & 100g…..) one after another.
Client ß Server (list)

Is this correct?

Form your modification request, you want from2 (Server) to receive the list:
Client à Server (command)
Client à Server (list)

Or is this one the correct one?

If the second one is the correct one then all you have to do is set a key separator “|” in between the string (WD_START|chips|100g, or just chips|100g), then just split it at this point “|”. Now the DataArray will have 3 (0, 1, 2) items in it. Using DataArray(insert number here) will bring out the desired entire. Try the example below if you don’t understand:

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000]'Create a string with three variables separated by "|"
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] A [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "WD_START|Chips, 100g|Chips, 105g|Chips, 98g"
[/SIZE][SIZE=2][COLOR=#008000]'This will create an Array, split up the value of A using the 
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'key separator "|", and set B equal to the four values.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] B() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = A.Split("|")
[/SIZE][SIZE=2][COLOR=#008000]'This is how to retrieve the values from the Array.
[/COLOR][/SIZE][SIZE=2][SIZE=2]MessageBox.Show("The command that came with this string is (" & B(0) & _
"); the first articles name and a weight is " & B(1) & _
"; the second one is " & B(2) & "; then comes " & B(3) & _
" which is the last.")
[/SIZE][/SIZE]

If the first one is correct, you will than have to make it listen for the server’s responses. Let me know which one is correct so that I can help you.
 
Master Zero,

This one is correct:

Client sends “start” command to server.
Client
à Server (command)


Server than sends back a list of paired items (chips & 100g…..) one after another.
Client ß Server (list)

The client sends start command, and then listens for data comming from the server.

Hope that helps!
 
Try this one; let me know what you think.
 

Attachments

  • Remote Commands.zip
    38.8 KB · Views: 78
Master Zero said:
Try this one; let me know what you think.

WORKS BEAUTIFULLY!!!!!!!!!!!

Master Zero, thank you very much....... your code is of great help. Now I able to figure out how it works and redo my program.

THANK YOU!

ps. Do you know a good tutorial/link that will help me in storing the data picked up on the client program to an excell spreadsheet?
 
Hi all,
Now i have the same problem. I run the codes (on the zip file), but it could not get any data. I can connect to checkweigher. And after that, i send some strings to set checkweigher ready status. These are:

yazici.WriteLine("WD_SET_PROT 2" & vbCrLf)
yazici.WriteLine("WD_SET_FORMAT 1" & vbCrLf)
yazici.WriteLine("WD_START" & vbCrLf)

But i could not take any data with using a streamreader (as MyReader) in a while loop (and using thread):
MyDataString = myReader.ReadLine()

Can you help me please?


Regards..
 
Hi all,
Now i have the same problem. I run the codes (on the zip file), but it could not get any data. I can connect to checkweigher. And after that, i send some strings to set checkweigher ready status. These are:

yazici.WriteLine("WD_SET_PROT 2" & vbCrLf)
yazici.WriteLine("WD_SET_FORMAT 1" & vbCrLf)
yazici.WriteLine("WD_START" & vbCrLf)

But i could not take any data with using a streamreader (as MyReader) in a while loop (and using thread):
MyDataString = myReader.ReadLine()

Can you help me please?


Regards..

you should not be using a writeline extension to send the commands. ".writeline" is usually used to display commands in a command prompt.

You should be just sending "WD_START" without vbcrlf
 
Here is the send command:

For1_load
Send("WD_START")
end sub


Private Sub Send(ByVal Command As String)

Dim Writer As New IO.StreamWriter(TCP.GetStream)
Writer.Write(Command)
Writer.Flush()

End Sub


Here is the read command:

Private Sub DoRead(ByVal ar As IAsyncResult)

TextBox2.AppendText(Encoding.ASCII.GetString(ReadBuffer, 0, BytesRead) & vbNewLine)

TCP.GetStream.BeginRead(ReadBuffer, 0, ReadBufferSize, AddressOf DoRead, Nothing)
end sub


Hope that helps.

cheers
 
Back
Top