Results 1 to 14 of 14

Thread: [2008] Connecting VB program to linux program

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    [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"
    Last edited by MotoMan_Yz400; Feb 19th, 2008 at 02:21 PM.

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    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.

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    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

  5. #5
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2008] Connecting VB program to linux program

    Atheist just told you to not use Winsock and, instead, use TcpClient and TcpListener...
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: [2008] Connecting VB program to linux program

    Ok well I was able to get something working between 2 windows machines (baby steps here ) 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.

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    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 )

    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.
    Last edited by MotoMan_Yz400; Feb 19th, 2008 at 04:48 PM.

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Private Sub Form_Load(...)
    2.     readThread = New System.Threading.Thread(AddressOf doRead)
    3.     readThread.IsBackground = True
    4.     readThread.Start()
    5. End Sub
    6.  
    7. Private Sub doRead()
    8.  
    9. 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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10
    Lively Member
    Join Date
    May 2007
    Posts
    87

    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

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    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"
    Last edited by MotoMan_Yz400; Feb 20th, 2008 at 11:07 AM.

  12. #12
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Public Class Form1
    2.     Private client As System.Net.Sockets.TcpClient
    3.     Private clientStream As NetworkStream
    4.  
    5.     Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         client = New System.Net.Sockets.TcpClient("192.168.55.4", 8000)
    7.         clientStream = client.GetStream()
    8.     End Sub
    9.  
    10.     Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.         If NetworkStream.CanWrite And NetworkStream.CanRead Then
    12.             If TextBox1.Text <> "" Then
    13.                 Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(TextBox1.Text)
    14.                 NetworkStream.Write(sendBytes, 0, sendBytes.Length)
    15.             End If
    16.         End If
    17.     End Sub
    18. End Class
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    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:
    1. Imports System.Net.Sockets
    2. Imports System.Text
    3.  
    4. Public Class Form1
    5.     Const portNumber As Integer = 8000
    6.     Dim tcpListener As New TcpListener(portNumber)
    7.  
    8.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    9.         Me.Show()   'If I dont use this, the form never loads until connection
    10.         Application.DoEvents() 'Same as above
    11.         tcpListener.Start()
    12.         Me.Text = "Waiting for connection..."  
    13.  
    14.         Try
    15.             Dim tcpCl As TcpClient = tcpListener.AcceptTcpClient()    'It lags here until connection
    16.  
    17.             Me.Text = "Connection Accepted..."
    18.         Catch er As Exception
    19.             TextBox1.Text = (er.ToString())
    20.         End Try
    21.     End Sub
    22. End Class
    Last edited by MotoMan_Yz400; Feb 20th, 2008 at 01:26 PM.

  14. #14
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Do
    2.     Dim client As TcpClient = tcpListener.AcceptTcpClient()
    3.     'Additional code to handle the newly connected client.
    4. Loop
    You would then need to create a new thread to run this loop, like I showed you in post #9.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width