TCP Communication Using StreamSocket

TCP Communication Using StreamSocket

JumpyNET

Centurion
Joined
Apr 4, 2005
Messages
196
A simplified version of the MSDN StreamSocket sample (tested and working).

TCP Client (Windows Phone 8 App)
C#:
Imports Windows.Networking
Imports Windows.Networking.Sockets
Imports Windows.Storage.Streams

Partial Public Class MainPage : Inherits PhoneApplicationPage

    Dim Socket As StreamSocket

    Private Async Sub ButtonConnect_Click(sender As Object, e As RoutedEventArgs) Handles ButtonConnect.Click
        Socket = New StreamSocket
        Await Socket.ConnectAsync(New HostName("192.168.1.7"), "1234")
        StartListening()
    End Sub

    Private Async Sub StartListening()
        Dim reader As New DataReader(Socket.InputStream)
        Do
            ' Read first 4 bytes (length of the subsequent string).
            Dim sizeFieldCount As UInteger = Await reader.LoadAsync(CUInt(Runtime.InteropServices.Marshal.SizeOf(New UInteger)))
            If sizeFieldCount <> Runtime.InteropServices.Marshal.SizeOf(New UInteger) Then
                ' The underlying socket was closed before we were able to read the whole data.
                Return
            End If

            ' Read the string.
            Dim stringLength As UInteger = reader.ReadUInt32()
            Dim actualStringLength As UInteger = Await reader.LoadAsync(stringLength)
            If stringLength <> actualStringLength Then
                ' The underlying socket was closed before we were able to read the whole data.
                Return
            End If

            ' Display the string.
            Dim MsgReceived As String = reader.ReadString(actualStringLength)
            System.Diagnostics.Debug.WriteLine(MsgReceived)
        Loop
    End Sub

    Private Async Sub ButtonSend_Click(sender As Object, e As RoutedEventArgs) Handles ButtonSend.Click
        Dim stringToSend As String = "Some Text To Send"
        Dim writer As New DataWriter(Socket.OutputStream)
        writer.WriteUInt32(writer.MeasureString(stringToSend))
        writer.WriteString(stringToSend)
        Await writer.StoreAsync()
        writer.DetachStream()
        writer.Dispose()
    End Sub

End Class

TCP Server (Windows 8 Store App)
Visual Basic:
Imports Windows.Networking
Imports Windows.Networking.Sockets
Imports Windows.Storage.Streams

Public NotInheritable Class MainPage : Inherits Page

    Dim WithEvents ListenerSocket As StreamSocketListener

    Private Async Sub ButtonListen_Click(sender As Object, e As RoutedEventArgs) Handles ButtonListen.Click
        ListenerSocket = New StreamSocketListener
        ' Don't limit traffic to an address or an adapter.
        Await ListenerSocket.BindServiceNameAsync("1234")
    End Sub

    Dim Socket As StreamSocket

    Private Async Sub listener_ConnectionReceived(sender As StreamSocketListener, args As StreamSocketListenerConnectionReceivedEventArgs) Handles ListenerSocket.ConnectionReceived
        ' Invoked once a connection is established.
        Debug.WriteLine("Connection Established")
        Socket = args.Socket
        Dim reader As New DataReader(args.Socket.InputStream)
        Do
            ' Read first 4 bytes (length of the subsequent string).
            Dim sizeFieldCount As UInteger = Await reader.LoadAsync(CUInt(Runtime.InteropServices.Marshal.SizeOf(New UInteger)))
            If sizeFieldCount <> Runtime.InteropServices.Marshal.SizeOf(New UInteger) Then
                Return ' The underlying socket was closed before we were able to read the whole data.
            End If

            ' Read the string.
            Dim stringLength As UInteger = reader.ReadUInt32()
            Dim actualStringLength As UInteger = Await reader.LoadAsync(stringLength)
            If stringLength <> actualStringLength Then
                Return ' The underlying socket was closed before we were able to read the whole data.
            End If

            ' Display the string.
            Dim MsgReceived As String = reader.ReadString(actualStringLength)
            Debug.WriteLine(MsgReceived)
        Loop
    End Sub

    Private Async Sub ButtonSend_Click(sender As Object, e As RoutedEventArgs) Handles ButtonSend.Click
        Dim stringToSend As String = "Some Text Back"
        Dim writer As New DataWriter(Socket.OutputStream)
        writer.WriteUInt32(writer.MeasureString(stringToSend))
        writer.WriteString(stringToSend)
        Await writer.StoreAsync()
        writer.DetachStream()
        writer.Dispose()
    End Sub

End Class

PS: Requesting to be moved to code library, please.
 
Last edited by a moderator:
Back
Top