2 programs - communicate with each other - How?

robertuk702

Member
Joined
Apr 23, 2006
Messages
15
Programming Experience
Beginner
Hi,

I have two vb.net programs I have to get to talk too each other. How do I do this? I have to keep them seperate.

Then how do I get them to communicate across two seperate PCs? ie one on one pc and the other on another pc.

Thank you for your help. I look forward to your answers/comments.

Thank you.

Regards.
 
What do they need to communicate? You mean you want to setup a constant connection? What kind of data are you sending?

To just exchange data it can be as simple as reading a text file on a network location. Each program keeps it's info in the file and compares it to one another.

Let me take an uneducated guess on what you want. I think you want a TCP connection. Pop these imports into the top of a form. Put the cursor on them and hit F1:

Imports System.Net
Imports System.Net.Sockets

Basically set one to listen and the other to connect.

Admitedely I am not the most expert person to ask about this but I can tell you right off before anyone can help you they will need to know what you want to communicate.
 
ooops missed a bit

Hi,

I wish to pass a user input(integer) from the first program to the second program. The second program sends back an answer to the first program either yes or no depending on the integer.

Does this help you to help me?

The imports - would they go into the first program or 2nd?

Thank you for your help.

Regards.
 
thank you.

Hi,

Thats getting there. I think i may have been misled by a lecturer at uni who said it was approx four lines of code. Grrrr!!!

Wait till i see him Tuesday lol....
 
Honestly I may be missing something entirely. It isn't an area of expertise for me. But here is an example of just the send side using tcp/ip.

Credit: Rimmel©

Imports System.Net
Imports System.Net.Sockets
Imports System.Text
PublicClass Form1
' These vars can be formwide or local (in procedure) depending on how you want to use them.
Dim remoteIPAddress As IPAddress
Dim ep As IPEndPoint
Dim tnSocket As Socket
PrivateSub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
SendCommands(
"127.0.0.1", "23")
EndSub
PrivateSub SendCommands(ByVal PIPAddress AsString, ByVal PPort AsString)
' LCommand: Actual command we are going to send through telnet
' Quite often this is the password or login (or both)
Dim Command AsString = "99"' Example command only - you need to use your own
' LRecvString: data returned from the telnet socet
Dim RecvString AsString = String.Empty
' NumBytes: Number of bytes return from telnet socket (count)
Dim NumBytes AsInteger = 0
' Get the IP Address and the Port and create an IPEndpoint (ep)
remoteIPAddress = IPAddress.Parse(PIPAddress.Trim)
ep =
New IPEndPoint(remoteIPAddress, CType(PPort.Trim, Integer))
' Set the socket up (type etc)
tnSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' Convert the ASCII command into bytes, adding a line termination on (vbCrLf)
Dim SendBytes As [Byte]() = Encoding.ASCII.GetBytes(Command & vbCrLf)
' Create a byte array for recieving bytes from the telnet socket
Dim RecvBytes(255) As [Byte]
Try
' Connect
tnSocket.Connect(ep)
Catch oEX As SocketException
' error
' You will need to do error cleanup here e.g killing the socket
' and exiting the procedure.
ExitSub
EndTry
' If we get to here then all seems good (we are connected)
Try
' Wait a few seconds (3) (depending on connection) telnet can be slow.
Wait(3000)
' Double check we are connected
If tnSocket.Connected Then
' Send the command
tnSocket.Send(SendBytes, SendBytes.Length, SocketFlags.None)
' -------------------------------------------------------------------
' The below "do loop" is not actually needed. This loop is used to
' Provide feedback for the commands you issue. e.g. like a hyperterm
' window. If you simply want to send command with no feedback rem it out
' -------------------------------------------------------------------
' loop getting 256 bytes of data from telnet socket at a time
Do
' RecvBytes with contain 256 bytes if data returned
' numbytes with have the count of bytes returned
NumBytes = tnSocket.Receive(RecvBytes, RecvBytes.Length, 0)
RecvString = RecvString + Encoding.ASCII.GetString(RecvBytes, 0, NumBytes)
LoopWhile NumBytes = 256 ' if less than 256 we are at the end of the recv stream
' Send recieved bytes to the output text box
txtRecv.Text = RecvString
' -------------------------------------------------------------------
Wait(1000)
' Disconnect
tnSocket.Disconnect(False)
EndIf
Catch oEX As Exception
' Error cleanup etc needed
EndTry
' Cleanup
remoteIPAddress = Nothing
ep = Nothing
tnSocket = Nothing
Command = Nothing
RecvString = Nothing

EndSub
PrivateSub Wait(ByVal PMillseconds AsInteger)
' Function created to replace thread.sleep()
' Provides responsive main form without using threading.
Dim TimeNow As DateTime
Dim TimeEnd As DateTime
Dim StopFlag AsBoolean
TimeEnd = Now()
TimeEnd = TimeEnd.AddMilliseconds(PMillseconds)
StopFlag =
False
WhileNot StopFlag
TimeNow = Now()
If TimeNow > TimeEnd Then
StopFlag = True
EndIf
Application.DoEvents()
EndWhile
' Cleanup
TimeNow = Nothing
TimeEnd = Nothing
EndSub
 
Back
Top