[2008] Connecting VB program to linux program
Ok this is a weird, complicated problem. I have two computers sharing the same network. One is a Windows based machine and the other is a Linux machine. there are about 12 computers on the network currently (4 windows, 8 linux going through a NetGear router), however I only need 2 of them to communicate (one linux, one windows). There isnt a solid network made between both OS's (not yet, working on it). But once I get them seeing each other I need to get my VB.NET program to talk with a Python program (Python programing is going to be conducted by someone else).
I'm asking, were should I begin on researching on networking programs together? Is WinSock what I need to be using? Can WinSock send things to a Python program? They will be sending simple commands back and forth to perform functions on either computer.
Example:
[linux] - "Set these variables as this, and then get me this data"
[Windows] - "I got the data, here you go"
Re: [2008] Connecting VB program to linux program
You should be able to do it using Winsock. VB.NET can send data to and read data from a port and I assume that Python can do the same. It really wouldn't matter what application is on the other end.
Re: [2008] Connecting VB program to linux program
You need to read up on network programming.
An application uses sockets to send and receive data from the network. The way the connection is set up and messages are sent is determined by the protocol in use. I suppose you're going to use the TCP/IP?
The fact that the applications are running on different OS's is no problem at all. The TCP protocol is a defined standard and TCP communication is done the same way, whatever platform is in use.
Winsock is a VB6 component so I'd suggest reading up on the TcpClient and TcpListener classes. There are numerous examples here on Vbforums aswell :)
Re: [2008] Connecting VB program to linux program
I have done some COM Port work before, which wasnt very hard at all. Does winsock work in some what the same way? (looking at ports, reading ports, sending to a port, etc.) Now I understand there will be IP issues to work out. Ok I'll look for some tutorials and overviews on Winsock then. Thankyou
Re: [2008] Connecting VB program to linux program
Atheist just told you to not use Winsock and, instead, use TcpClient and TcpListener...
Re: [2008] Connecting VB program to linux program
Ok well I was able to get something working between 2 windows machines (baby steps here :lol: ) I used a code from a tutorial to setup my Client and Server. I was able to pass strings back and for, well kinda. The code I'm working on now (a very simple IM app) Just to test working with passing strings alittle more directly.
Here's my code
Code:
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect("192.168.55.4", 8000)
Dim networkStream As NetworkStream = tcpClient.GetStream()
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If networkStream.CanWrite And networkStream.CanRead Then
' Do a simple write.
If TextBox1.Text <> "" Then
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(TextBox1.Text)
networkStream.Write(sendBytes, 0, sendBytes.Length)
End If
' Read the NetworkStream into a byte buffer.
' Output the data received from the host to the console.
End If
End Sub
Now I know there is a mistake just under the Public class form. I need this app to open a port, both computer connect on to it, and them be able to send strings back and forth.
The form has 2 textbox'es and one "send" button. I would like it to check the receiving port for any new data, and then post it in a text box. And if you were to type something in the "sending" text box, then click send, It should pump it down to the other computers to it's "received" textbox.
Re: [2008] Connecting VB program to linux program
Whats the question? :)
To read from the networkstream you'd either need to create a worker thread and use the streams Read method, or skip creating a worker thread and use the BeginRead/EndRead asynchronous methods instead.
Re: [2008] Connecting VB program to linux program
Well how would I go about having it where I open the port, leave it open. Then read when I want to, and write when I want to.
To create a thread, how does one go about doing that? (I know you guys/gals hate noobs :cry: )
Well the issues I'm having now are that, I open the port in a sub, then every other sub can't use it (open the port in Main sub, then all other subs cant read or write to the port). I can put it all in one, but then it has to connect each time it needs to send something. The tutorial I'm using only should how to make a single shot program, not something you can continually write and read from.
Re: [2008] Connecting VB program to linux program
Nobody here hates beginners:)
Heres how you'd create a thread:
Private readThread As System.Threading.Thread
vb Code:
Private Sub Form_Load(...)
readThread = New System.Threading.Thread(AddressOf doRead)
readThread.IsBackground = True
readThread.Start()
End Sub
Private Sub doRead()
End Sub
This'll create a and start a thread that will execute the doRead subroutine.
Edit: Regarding your second problem, just connect the TcpClient once and dont disconnect it, and it'll be fine.
Re: [2008] Connecting VB program to linux program
Lucky for u you can connect to an other app and dont have to write your own server.
If you dont know how to thread and invoke and do async calls you can give the .NET version of winsock a try(wich is just a wraper of the system.net.sockets).
www.vwsoftwaresolutions.nl/winsock2005.dll.zip
It can be found there (orginaly from codeproject).
This component olso contains servel events wich are very handy.
Greets
Re: [2008] Connecting VB program to linux program
I figured it out, I'm an idiot!
Quote:
Originally Posted by Atheist
Nobody here hates beginners:)......Edit: Regarding your second problem, just connect the TcpClient once and dont disconnect it, and it'll be fine.
well I'm having issues still tho. Here's my code again
Code:
Public Class Form1
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect("192.168.55.4", 8000)
Dim networkStream As NetworkStream = tcpClient.GetStream()
End Sub
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If NetworkStream.CanWrite And NetworkStream.CanRead Then
If TextBox1.Text <> "" Then
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(TextBox1.Text)
NetworkStream.Write(sendBytes, 0, sendBytes.Length)
End If
End If
End Sub
End Class
Now I have when the form loads, it sets up the IP address, and port number. So my client is running (I think). But everything in the button is giving me errors. 'Networkstream' isnt passing through to the other subs. I'm keep on getting:
Error-"Reference to a non-shared member requires an object reference"
Re: [2008] Connecting VB program to linux program
You are declaring both your TcpClient and your NetworkStream as local variables in the Form_Load subroutine, they will go out of scope once that subroutine finishes and will be marked for garbage collection. You need to declare them at class level, and remember that it is not a good practise to give the variable the same name as the class. Here's how I'd do it:
VB.Net Code:
Public Class Form1
Private client As System.Net.Sockets.TcpClient
Private clientStream As NetworkStream
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
client = New System.Net.Sockets.TcpClient("192.168.55.4", 8000)
clientStream = client.GetStream()
End Sub
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If NetworkStream.CanWrite And NetworkStream.CanRead Then
If TextBox1.Text <> "" Then
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(TextBox1.Text)
NetworkStream.Write(sendBytes, 0, sendBytes.Length)
End If
End If
End Sub
End Class
Re: [2008] Connecting VB program to linux program
Ok I got my client side worked to just about the point I want it (all thanks to you guys with the help)
But now I'm needing to work on my Server side. The issue I'm currently having is when running the program (and it actually works) I'm having an issue with it freezing until the connection is made. I need to to continually look for the connection, trying to establish it, but the program continues until that point. And If connection is lost, then it will begin looking again (just like any server app)
VB.NET Code:
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Const portNumber As Integer = 8000
Dim tcpListener As New TcpListener(portNumber)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show() 'If I dont use this, the form never loads until connection
Application.DoEvents() 'Same as above
tcpListener.Start()
Me.Text = "Waiting for connection..."
Try
Dim tcpCl As TcpClient = tcpListener.AcceptTcpClient() 'It lags here until connection
Me.Text = "Connection Accepted..."
Catch er As Exception
TextBox1.Text = (er.ToString())
End Try
End Sub
End Class
Re: [2008] Connecting VB program to linux program
AcceptTcpClient is a blocking method so it will block the execution in the current thread until someone connects. You need to create a subroutine that houses an "endles"s loop that looks something like this:
vb Code:
Do
Dim client As TcpClient = tcpListener.AcceptTcpClient()
'Additional code to handle the newly connected client.
Loop
You would then need to create a new thread to run this loop, like I showed you in post #9.