Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,429 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,484 people online right now. Registration is fast and FREE... Join Now!




P2P Connections

 
Reply to this topicStart new topic

> P2P Connections, VB.NET

Rating  5
the_hangman
Group Icon



post 28 Nov, 2006 - 02:06 PM
Post #1


NOTE: This tutorial is for VB.NET

This tutorial will show how to create a P2P (Peer To Peer) connection with another user, and transfer data in the form of strings. This is only a basic tutorial, but the logic can be extended to anything. I've already used this to create an Instant Messager, a few multi player online games, and some other basic information exchange programs.

First things first, let's start a form to work with. Obviously you don't need all or any of these items on your form, and you may even want to add more (such as a field to enter an IP address). Create a form and add 1 Button, 1 TextBox, and 1 Timer. Make the textbox big enough to type in. For the timer set the interval property in the properties box to 1 and the enabled property to True. It should look something like this:
Attached Image

Now let's take a look at those codes. Bring up the Code window for Form 1. We are going to need to import a few namespaces in the area before Public Class Form1.

CODE
Imports System.Net.Sockets
Imports System.Threading
Imports System.IO

Public Class Form1


The System.Net.Sockets namespace will allow us to make and listen to connections from and with other users.
The System.Threading namespace will allow us to do several things at the same time without interfering with one another
The System.IO namespace will allow us to send and receive information over our connection

Next we will need a few variables

CODE
Dim Listener As New TcpListener(65535)
Dim Client As New TcpClient
Dim Message As String = ""


The variable Listener is a TcpListener that listens for incoming connections (data someone is sending you) at a specific port. A port is just the gateway that you want information to flow through. It doesn't really matter what port you use, as long as it's not already being used. There are 65,535 ports to choose from. The lower numbers are used more often and the higher are usually open, so you will be more likely to find an open port if you pick a high number. We are going to use the last port, for the sake of it.

The variable Client is a TcpClient, which is basically the other computer that you are sending information.

The variable Message is just a String to hold incoming messages. We are going to make it empty when the form first starts, because there is no message yet.


When Form1 first loads we want to start a separate thread to take care of some repetitive tasks that would otherwise freeze your application.

CODE
    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)) 'Creates the thread
        ListThread.Start() 'Starts the thread
    End Sub


This part should be pretty easy to understand. Dim ListThread creates the thread. AddressOf is the Address (sub) that you want the thread to start working on. And ListThread.Start() starts the thread.

Obviously the sub Listening doesn't exist yet, so we need to create it. The only thing this sub needs to do is start our TcpListener (remember it was called Listener?)

CODE
    Private Sub Listening()
        Listener.Start()
    End Sub


The reason we added this to the thread is because if we didn't it would eventually freeze up your application.
Now we need to do something when the TcpListener hears a connection.
NOTE: At this part I use the timer. I have yet been unsuccessful in creating a loop that will listen for a connection without causing problems somewhere. If you know of a way, it will work here just fine I'm sure, and maybe you could even help me out?

CODE
    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


What this does is checks 1000 times a second (if the timer interval is set to 1) to see if the TcpListener heard a connection. If it does, then it proceeds to collect the message.

First we have to make sure variable Message is empty. No message has come in yet remember? Then we need to accept the connection (sort of like answering the phone). We'll use the TcpClient variable that we created earlier (Client) to hold the client (other computer).

Next we need to start a stream for the data to come in. So we create a StreamReader that can read the stream. The argument for the StreamReader is what stream to read. We initiate the stream from the client with Client.GetStream() as the argument.

Now it's time to read from the stream. Since we can't read a messages that isn't there, we need to make sure that there is something to read. To do this we will read one character at a time, but we will use Reader.Peek() to peek ahead one space and make sure there is a letter there, which will return the index of the next letter. If there is no letter it will return -1, therefore, as long as Reader.Peek() comes back greater than -1 we can keep going.
Next we want to save the message we are receiving in our String variable Message. Since we are only getting one letter at a time we want to add each letter onto the end of what is already there. So we use Message = Message + the next letter. And finally we read the next letter with Reader.Read() This letter is going to come in as machine data that is pretty much gibberish to people, so we will have to convert it to readable characters first.
Also, we want to see the message so we are just going to display it in a message box.

The last thing we need to do to complete our P2P connection, is send a message to someone, which we will do when we click on Button1

CODE
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Client = New TcpClient("127.0.0.1", 65535)

        Dim Writer As New StreamWriter(Client.GetStream())
        Writer.Write(TextBox1.Text)
        Writer.Flush()
    End Sub


Once again we are going to use the variable Client to establish a connection with the other computer. But this time we need to determine who we are sending the data to, and what port we are using. Since this is a demo we are just going to connect to ourselves (and yes, talk to ourselves), by using the IP 127.0.0.1 (the universal home address in case you didn't know). You may want to add a TextBox to type in a different address and use that as the IP to connect to (something along the lines of TextBox2.Text). Then we want use the same port to make sure the info gets where it's going.

Now let's create a stream to write our data on. Create a StreamWrite that can write to streams. Like the StreamReader, it has an argument of which stream to write to, and we will use Client.GetStream() again. After that we write the text from TextBox1 into the stream. And when it's all done we flush it, which is, for all intensive purposes, like flushing a toilet to make everything go down the pipe where it needs to go.

Oh and make sure you close the code with:
CODE

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Listener.Stop()
    End Sub
End Class


You should be able to run the application now, and send yourself messages. I've included a textfile with the typed code for you to look at if you need help. Have fun!


For the record:
- StreamWriter and StreamReader are part of the System.IO namespace
- TcpClient and TcpListener are part of the System.Net.Socket namespace
- Thread is part of the System.Threading namespace

This post has been edited by the_hangman: 29 Nov, 2006 - 05:06 AM


Attached File(s)
Attached File  P2P_Connection_Code.txt ( 1.46k ) Number of downloads: 1201
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

brottmayer
**



post 8 Apr, 2007 - 07:00 PM
Post #2
QUOTE(the_hangman @ 28 Nov, 2006 - 03:06 PM) *



Quick question? When selecting a different IP address, this would be the other PC's IP that I would input into TextBox2.Text and it will go into here like so:

CODE

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Client = New TcpClient(TextBox2.Text, 60798)
        Dim Writer As New StreamWriter(Client.GetStream())
        Writer.Write(TextBox1.Text)
        Writer.Flush()
    End Sub

Like so?

And another question? How can I set this up to have the messages sent and received show up in textbox1.text, just like the ones of msn and yahoo? Thanks. and By the way, AWESOME tutorial!! Learned a lot!

brottmayer

Go to the top of the page
+Quote Post

Codezilla
*



post 14 Jun, 2007 - 12:25 PM
Post #3
This tutorial is by far the best I've seen for an introductory explanation on the use of TCPListener/Client. I've been looking for something like this for 2 days now, and thanks to you I now have the basic rudimentary knowledge I need to really start getting knees-deep into this without getting an aneurism. Thank you.
Go to the top of the page
+Quote Post

dmonroe
*



post 7 Nov, 2007 - 11:05 AM
Post #4
This tutorial was marvelous. Thank you.
One quick question, any idea how to do a broadcast with this? I have tried using 255.255.255.255, but it returns an unhandled socket exception error.

This has really helped with a project I am working on.[size=1]
Go to the top of the page
+Quote Post

HAX
*



post 12 Nov, 2007 - 12:17 PM
Post #5
Can somebody help me plz
i folow the tutorial to the letter and i get an error message saying target machine activly refused

plz help
Go to the top of the page
+Quote Post

Rozie0910
*



post 15 Oct, 2008 - 05:33 PM
Post #6
i already try this code..
i dont get an error and no output came out...
could u help me..
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/22/08 09:57AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month