EZ P2P (and I do mean easy!)-VBForums
Results 1 to 3 of 3

Thread: EZ P2P (and I do mean easy!)

  1. #1

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    445

    EZ P2P (and I do mean easy!)

    If like me you're onto the next project before you can really hone down a new technique using VB.NET code I recently came across this simple P2P setup. I had been searching for something like this only to find complex, multi-tasking code which was mostly centered around building multi-user chat programs. I had just about given up finding anything this simple, and in fact, I read loads on the topic from one to many articles which said all this fluff, was needed. What I needed was a simple, down to the bone block of code which would stream some text over to another machine and that other machine has the software to turn that stream into something useful. And unlike so many of the articles I read all that fluff was not needed. This is not for absolute beginners but it will start you off on the path to conquering bigger and better things.

    And by the way, other methods I've tried, e-mail, FTP, etc... all of them have worked but I have had some trouble with the preferred method from my client which is e-mail. It's not something easily explained here but suffice to say it's what sent me on this mission to tackle some P2P stuff. Turns out you only need one machine to test this, but to make it really work you need at least two computers, a client and a server. The code was found at dreamincode.net in one of their tutorials on P2P. I modified it into the two components you see below. This is easily taken to the next step once you see how easy it is to make the connection and send the stream over the LAN/WAN. Oh yes, this will most likely set off alarms from within your Windows Firewall that it needs to open a port. And that's a good thing because as with any P2P app, there are security questions to be addressed. Be sure you investigate this before using any P2P app.

    First, open a new Windows Application Form project. In the form, place one textbox, one button and one timer. In the properties for the timer, set the Interval to 100, lower if needed and set Enabled to True.

    SERVER CODE:

    Code:
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.IO
    
    Public Class Form1
    
        Dim Listener As New TcpListener(65535)
        Dim Client As New TcpClient
        Dim Message As String = ""
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
            ListThread.Start()
        End Sub
    
        Private Sub Listening()
            Listener.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Listener.Pending = True Then
                Message = ""
                Client = Listener.AcceptTcpClient()
    
                Dim Reader As New StreamReader(Client.GetStream())
                While Reader.Peek > -1
                    Message = Message + Convert.ToChar(Reader.Read()).ToString
                End While
                MsgBox(Message, MsgBoxStyle.OkOnly)
            End If
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Listener.Stop()
        End Sub
    
    End Class

    CLIENT CODE:

    Code:
    Imports System.Net.Sockets
    Imports System.IO
    
    Public Class Form1
    
        Dim Client As New TcpClient
        Dim Message As String = ""
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Client = New TcpClient("10.0.0.110", 65535) ' Change the IP here to the server's address
            Dim Writer As New StreamWriter(Client.GetStream())
            Writer.Write(TextBox1.Text)
            Writer.Flush()
        End Sub
    
    End Class
    As a minimalist I took out anything not actually needed for this to work.
    Last edited by Vladamir; Jan 2nd, 2013 at 07:31 PM.

  2. #2
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    562

    Re: EZ P2P (and I do mean easy!)

    Looks us full yes I looked into this stuff before and just found nothing us full just loads of code that made no sence this looks pretty simple tho to start off a project.
    I wonder would it be very hard to mod your server part to maybe make something like a basic web server

    Thanks for sharing.

  3. #3
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Re: EZ P2P (and I do mean easy!)

    Thank you ! It's working with LAN, How to make it for WAN ?

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.