Solved

Windows CE 2 way communication using TCP/IP

Posted on 2007-04-11
5
376 Views
Last Modified: 2013-12-27
We are using Windows CE based Pocket PC Phone HP IPaq RX 5720. We have GPRS enabled on the mobile. We want to send information to this mobile from central server using TCP/IP , and also send response back from mobile to Server.
Can you help us?
0
Comment
Question by:semaphoreindia
  • 2
5 Comments
 
LVL 9

Expert Comment

by:checoo
ID: 18898201
To start with you need to use the TCPListen and TCPClient, and to make it thread safe you need to use a Synclock. Start the listening process on a seperate process, look at the samples below --

Server
=====

Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Collections

Public Class SocketServer

    Private ClientCol As Hashtable
    Private objThread As Thread
    Private objListner As TcpListener
    Private intListeningPort As Integer

    Public Sub New(ByVal intPort As Integer)
        ClientCol = New Hashtable
        intListeningPort = intPort
        objThread = New Thread(AddressOf DoListen)
        objThread.Start()
    End Sub

    Private Sub DoListen()
        Dim newClient As Client
        Try
            objListner = New TcpListener(IPAddress.Any, intListeningPort)
            objListner.Start()
aaa:        Do
                newClient = New Client(objListner.AcceptTcpClient, strConnectionString)
                ClientCol.Add(newClient.ClientID, newClient)
                AddHandler newClient.Connected, AddressOf onConnected
                AddHandler newClient.Disconnected, AddressOf onDisconected
                AddHandler newClient.DataReceived, AddressOf onDataReceived
            Loop Until False
        Catch e As Exception
            GoTo aaa
        End Try
    End Sub

    Private Sub onConnected(ByVal Sender As Client)
        ClientCol.Add(Sender.ClientID, Sender)
    End Sub

    Private Sub onDisconected(ByVal Sender As Client)
        Sender.DisposeClient()
        ClientCol.Remove(Sender.ClientID)
    End Sub

    Private Sub onDataReceived(ByVal Sender As Client, ByVal strData As String)
        Try
            'write code to process the inputs
            Sender.Send(strResponseString)
        Catch e As Exception
            Sender.Send(e.Message.ToString)
        End Try
    End Sub

    Public Sub DisposeServer()
       'clean up code
    End Sub

End Class

Client
====

Imports System.Net.Sockets
Imports System.Text

Public Class Client
    Public Event Connected(ByVal sender As Client)
    Public Event Disconnected(ByVal sender As Client)
    Public Event DataReceived(ByVal sender As Client, ByVal strData As String)

    Private myID As Guid
    Private objClient As TcpClient
    Private bClientData(1024) As Byte
    Private objText As New StringBuilder

    Public Sub New(ByVal Client As TcpClient, ByVal strConn As String)
        Dim strResponse As String
        objClient = Client
        objClient.NoDelay = True
        myID = Guid.NewGuid
    End Sub

    Public ReadOnly Property ClientID() As String
        Get
            Return myID.ToString
        End Get
    End Property

    Private Sub ReceiveClientData(ByVal asyncResult As IAsyncResult)
        Dim intcount As Integer
        Try
            SyncLock objClient.GetStream
                intcount = objClient.GetStream.EndRead(asyncResult)
            End SyncLock

            If intcount < 1 Then
                RaiseEvent Disconnected(Me)
                Exit Sub
            End If

            ByteToString(bClientData, intcount)

            SyncLock objClient.GetStream
                objClient.GetStream.BeginRead(bClientData, 0, 1024, AddressOf ReceiveClientData, Nothing)
            End SyncLock

        Catch e As Exception
            RaiseEvent Disconnected(Me)
        End Try
    End Sub

    Private Sub ByteToString(ByVal Bytes() As Byte, ByVal intCount As Integer)
        Dim intIndex As Integer
        For intIndex = 0 To intCount - 1
            If Bytes(intIndex) = 94 Then
                RaiseEvent DataReceived(Me, objText.ToString)
                objText = New StringBuilder
            Else
                objText.Append(ChrW(Bytes(intIndex)))
            End If
        Next
    End Sub

    Public Sub Send(ByVal strData As String)
        SyncLock objClient.GetStream
            Dim wri As New IO.StreamWriter(objClient.GetStream)
            wri.Write(strData)
            wri.Flush()
        End SyncLock
        If blnOracleError Then
            blnOracleError = False
            objconn = Nothing
            RaiseEvent Disconnected(Me)
        End If
    End Sub

    Public Sub Dispose()
      'Clean up code here
    End Sub
End Class
0
 

Author Comment

by:semaphoreindia
ID: 18900327
I am not able to understand the code above.
Please explain that where to write Server code and where to write Client code?

What does this code indicate? A service or DLL? or an application?

Do you mean that this code will run Server as TCP listenere on the Windows Mobile.? if yes, then how to connect to this server (on windos mobile) from external computer ./
We assurme that this will be on internet.
0
 
LVL 9

Accepted Solution

by:
checoo earned 500 total points
ID: 18903432
The above is just a sample code which can be used as a guidance. In your scenario you might want to implement the TCP listener on the server as a Windows Service. The clients will be running on the device.

Things will be more complicated in your case, and the implementation you have thought of may not be ideal. You will need to host the server on a public IP address so that the same can be accessible from the clients running on devices using GPRS. This will have the hassle of firewall, authentication, security etc. An alternative can be to use a VPN client on the device to hook on to your network then connect to an your TCP server on an internal IP. The best alternative may be to check the feasibility of using web services.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now