Resolved Listen and accept connection asynchronously?

Shaun Dreclin

New member
Joined
Jul 11, 2008
Messages
2
Programming Experience
1-3
Problem solved, read second post.

Hey, I've just followed a very nice socket tutorial at [link removed]
There is only one problem - that tutorial only shows you how to make a client, not a server.
Could sombody take a look at the code and tell me how I would add listening capabilities?

VB.NET:
Imports System.Net
Imports System.Net.Sockets

Public Class Form1

    Private ClientSocket As Socket
    Private ASCII As New System.Text.ASCIIEncoding


    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        'Resolve the name to an IP address
        Dim Addr As IPAddress = Dns.GetHostEntry(txtAddress.Text).AddressList(0)
        If Not Addr Is Nothing Then
            'Create an IP Endpoint
            Dim EP As New IPEndPoint(Addr, CInt(txtPort.Text))
            'Create a new socket
            ClientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            'Connect
            ClientSocket.BeginConnect(EP, AddressOf ConnectCallback, Nothing)
        End If
    End Sub

    Private Sub ConnectCallback(ByVal ar As IAsyncResult)
        ClientSocket.EndConnect(ar)
        Dim dlg As New NoParamsDelegate(AddressOf ConnectedUI)
        Me.Invoke(dlg)
        'Begin Receiving Data
        Dim bytes(4095) As Byte
        ClientSocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf ReceiveCallback, bytes)

    End Sub

    Private Delegate Sub NoParamsDelegate()

    Private Sub ConnectedUI()
        Me.Text = "Connected!"
        btnConnect.Enabled = False
        btnSend.Enabled = True
        txtSend.Focus()
        txtLog.Clear()
    End Sub

    Private Sub DisconnectedUI()
        Me.Text = "Not Connected"
        btnConnect.Enabled = True
        btnSend.Enabled = False
        btnConnect.Focus()
    End Sub

    Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
        Dim bytes() As Byte = CType(ar.AsyncState, Byte())
        Dim numbytes As Int32 = ClientSocket.EndReceive(ar)
        If numbytes = 0 Then
            ClientSocket.Shutdown(SocketShutdown.Both)
            ClientSocket.Close()
            Dim dlg As New NoParamsDelegate(AddressOf DisconnectedUI)
            Me.Invoke(dlg)
        Else
            Dim Recv As String = ASCII.GetString(bytes, 0, numbytes)
            'Clear the buffer
            Array.Clear(bytes, 0, bytes.Length)
            'Show in UI
            Dim dlg As New OneStringDelegate(AddressOf DisplayReceivedData)
            Dim args() As Object = {Recv}
            Me.Invoke(dlg, args)
            'Begin Receive again
            ClientSocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf ReceiveCallback, bytes)

        End If
    End Sub

    Private Sub DisplayReceivedData(ByVal Data As String)
        With txtLog
            .SelectionStart = .Text.Length
            .SelectedText = Data
        End With
    End Sub

    Private Delegate Sub OneStringDelegate(ByVal Data As String)

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim bytes() As Byte = ASCII.GetBytes(txtSend.Text)
        ClientSocket.Send(bytes)
        txtLog.Text = txtLog.Text & vbNewLine & ">>" & txtSend.Text
    End Sub
End Class
 
Last edited:
Back
Top