Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tutorial][VB.Net] Asynchronized Server/Client Demo(using sockets)
#1
What's up support forums?!?!


I was bored and decided to make a little example of how EASY it is to use Sockets. This is my first vid so bear with me. It ran a little long but it covers both Server and Client. This demonstrates a single client connection. Depending on the feedback I receive I may release another demo that shows how to incorporate multiple clients.

What is this?
This is a demonstration on how to use Sockets when programming Server/Client applications. It can be used in chat programs, naughty black hat stuff, IRC clients etc.

Can I c/p you code?
Sure why not, it is up to you to learn from this. You get from this what you put in. So if you actually go through the code and understand it, you will learn far more than c/p'ing.


Enjoy. Keep SF alive by posting!

Video:


Server Code:
Code:
Imports System.Net, System.Net.Sockets
Public Class frmServer
    Dim server As Socket
    Dim client As Socket
    Dim bytes As Byte() = New Byte(1023) {}
    Private Sub frmServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        server = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim xEndpoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 6969)
        server.Bind(xEndpoint)
        server.Listen(2)
        server.BeginAccept(New AsyncCallback(AddressOf OnAccept), vbNull)
    End Sub
    Private Sub OnAccept(ByVal ar As IAsyncResult)
        client = server.EndAccept(ar)
        client.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), client)
    End Sub
    Private Sub OnRecieve(ByVal ar As IAsyncResult)
        client = ar.AsyncState
        client.EndReceive(ar)
        client.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnRecieve), client)
        Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
        MessageBox.Show(message)
    End Sub
End Class

Client Code:
Code:
Imports System.Net, System.Net.Sockets
Imports System.Text.ASCIIEncoding
Public Class frmClient
    Dim client As Socket
    Dim host As String = "127.0.0.1"
    Dim port As Integer = "6969"
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim IP As IPAddress = IPAddress.Parse(host)
        Dim xIpEndPoint As IPEndPoint = New IPEndPoint(IP, port)
        client.BeginConnect(xIpEndPoint, New AsyncCallback(AddressOf OnConnect), Nothing)
        btnConnect.Enabled = False
    End Sub
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim bytes As Byte() = ASCII.GetBytes(txtMessage.Text)
        client.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnSend), client)
    End Sub
    Private Sub OnConnect(ByVal ar As IAsyncResult)
        client.EndConnect(ar)
        MessageBox.Show("Connected")
    End Sub
    Private Sub OnSend(ByVal ar As IAsyncResult)
        client.EndSend(ar)
    End Sub

End Class
Reply
#2
This is pretty cool man. Smile
Reply
#3
Nice Share , You are so Good In VB Coding Smile. Mind to teach me or share me (or us here on SF) some cool Codes on VB?
[Image: UTLN2.gif]
That's not my Sig . So don't flame me.
Reply
#4
(05-20-2011, 06:36 AM)stephen5565 Wrote: Nice Share , You are so Good In VB Coding Smile. Mind to teach me or share me (or us here on SF) some cool Codes on VB?

If you have any request's or need help, start a thread and I will help you out for sure. That's if another doesn't beat me to it. Pirate
Reply
#5
I followed your video, got most of it down, but how do I impliement the ability for the server to send a message back to the client?

Or at least let the client also receive data.
Reply
#6
Finally something interesting. Thanks.
Reply
#7
(06-03-2011, 12:20 AM)rewter Wrote: I followed your video, got most of it down, but how do I impliement the ability for the server to send a message back to the client?

Or at least let the client also receive data.

The client would have to listen for data to receive similar to how the server does. In the client, create a Sub similar to the server OnRecieve (you could just c/p it over), then in the OnConnect sub of the client, create a new callback to the OnRecieve sub (see the server source).

Let me know of this helps. If not I will go more in depth.
Reply
#8
Hi. Thanks for sharing this. I got a little question: is it possible to connect more than one client to this server, or does it require to create each one in a separate thread? if not, how can multi-threading be implemented within your code?

Also if i want to send for example 3 lists of strings from the client(s) to the server in one transaction how can i do it without overloading it? is it the most efficient way to put string delimiters for each "session" of sending the lists? (in other words, how can i stop the communication between the client and the server after the server received a certain string, WITHOUT initiating further data sending from the client? - basically i want to send those 3 lists of strings "at once" when i click the send button, and the server to somehow wait until it received all of them, be able to split them up, and ONLY then re-allow the client to send them again)

Edit: regarding my above question, your "bytes" array has a length of 1024. What if you send more than this amount? is there a way to make it send ANY amount of data(like a loop until all the data has been sent or a certain string has been encountered)?

Edit2: also, how can i disconnect all the clients and stop the server from listening? Sad

Thank you very much and really sorry about asking so many questions Smile
Reply
#9
Trying now. Not going to c/p the code Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need Help With AIM Client AKL3GEND 2 1,235 09-08-2012, 02:15 PM
Last Post: shadywatcher212
  [TuToRiAl] How to make Offline (No Internet) Captcha (Human Confirmation) [TuToRiAl] Resistance 24 12,527 08-30-2012, 10:14 PM
Last Post: Resistance
  File Profiler Demo - Preview - Developed by Ace AceInfinity 4 1,420 12-30-2011, 08:17 PM
Last Post: AceInfinity
  [VB.net]Learn WPF [WPF Tutorial] ThePrinCe 1 1,282 12-03-2011, 10:54 PM
Last Post: HostGap
  {TuToRiAl} How To Verify If Your App. Was Ran As Administrator {TuToRiAl} Resistance 11 6,013 10-11-2011, 04:30 PM
Last Post: Genuine

Forum Jump:


Users browsing this thread: 1 Guest(s)