TCP Communication Using StreamSocket
TCP Communication Using StreamSocket
TCP Communication Using StreamSocket
TCP Communication Using StreamSocket
TCP Communication Using StreamSocket
TCP Communication Using StreamSocket TCP Communication Using StreamSocket TCP Communication Using StreamSocket TCP Communication Using StreamSocket TCP Communication Using StreamSocket TCP Communication Using StreamSocket TCP Communication Using StreamSocket
TCP Communication Using StreamSocket TCP Communication Using StreamSocket
TCP Communication Using StreamSocket
Code Library A repository of sample code

Enjoy an ad free experience by logging in. Not a member yet? Register.
Go Back  Xtreme .NET Talk > > > TCP Communication Using StreamSocket


Reply
 
Thread Tools Display Modes
  #1  
Old 12-31-2013, 09:52 AM
JumpyNET's Avatar
JumpyNET JumpyNET is offline
Centurion

Preferred language:
VB.NET
 
Join Date: Apr 2005
Posts: 195
JumpyNET is on a distinguished road
Thumbs up TCP Communication Using StreamSocket



A simplified version of the MSDN StreamSocket sample (tested and working).

TCP Client (Windows Phone 8 App)
Code:
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)
Code:
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.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Communication through webbrowser. AVS67 General 2 03-10-2009 01:38 PM
C# and VB Communication melkanzi General 3 10-10-2006 06:39 AM
RS232 communication realolman General 6 02-22-2005 01:38 PM
Communication with dialog Malfunction Windows Forms 3 03-10-2004 06:42 AM
communication between two forms frida Windows Forms 2 02-17-2004 01:02 PM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
TCP Communication Using StreamSocket
TCP Communication Using StreamSocket
TCP Communication Using StreamSocket TCP Communication Using StreamSocket
TCP Communication Using StreamSocket
TCP Communication Using StreamSocket
TCP Communication Using StreamSocket TCP Communication Using StreamSocket TCP Communication Using StreamSocket TCP Communication Using StreamSocket TCP Communication Using StreamSocket TCP Communication Using StreamSocket TCP Communication Using StreamSocket
TCP Communication Using StreamSocket
TCP Communication Using StreamSocket
 
TCP Communication Using StreamSocket
TCP Communication Using StreamSocket