By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,323 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

Client / Server 'Shall I put a sock(et) in it?'

P: n/a
I'm sorry to bother you with this question but it was either this or
giving up and trying to go for a simpler solution (which I will if
this is not gonna work out..)

I'm afraid this is way out of my league but then again why not give it
a shot...

I've used the Pocket PC Signature Application Sample from:
http://msdn.microsoft.com/library/de...gnatureApp.asp

and I wanted to not only being able to send something from the client
to the server but also back from server to client.
So I started with sending a string 'helo' from client to server and
expecting a string back with 'helo2u2'.
If I send the string the server responds and sends back the 'helo2u2'
string with a terminator.
I've copied the code (with the while loop in it) from the server to
the client which resides in the 'ReceiveCallback' function to collect
all data send from the server **[including the terminator? / is the
loop necesary?]**

The Client side code however returns as far as I can see 3 kinds of
strings
- an empty one
- a right one ("message box is being displayed")
- a "helo2u2helo2u2" string

Sometimes the code just 'stops/hangs' at:
bytesRead = _socket.Receive(buf)

I guess there's one percent of sockets I know and 99% I don't know but
if a Socket Guru can point me in the right direction I will be more
then gratefull..

Kind regards,

Mike

//Code:

[Server side]

If System.Text.Encoding.ASCII.GetString(_encryptData) = "helo" Then
Dim StrByte As Byte() = _
System.Text.Encoding.ASCII.GetBytes("helo2u2")
_server.Send(StrByte)
End If

' Send data to the server.
Public Sub Send(ByVal data() As Byte)
Dim _asyncEvent As ManualResetEvent = New
ManualResetEvent(True)
' send the data
_socket.BeginSend(data, 0, data.Length, _
SocketFlags.None, Nothing, Nothing)

' send the terminator
_asyncEvent.Reset()
_socket.BeginSend(Network.TerminatorBytes, 0, _
Network.TerminatorBytes.Length, SocketFlags.None, _
AddressOf SendCallback, True)
End Sub
[Client side]

' Read data from server.
Public Sub Receive()
_asyncEvent.Reset()
_socket.BeginReceive(_readBuffer, 0, _readBuffer.Length, _
SocketFlags.None, AddressOf ReceiveCallback, Nothing)
End Sub

Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
Try
_socket.EndReceive(ar)

' sit in this loop until connection is broken
' handle client commands and send back response
Dim bListen As Boolean = True
While bListen
' reset everything for the next read message
Dim bReadMessage As Boolean = True
Dim bytesRead As Integer = 0
Dim totalBytes As Integer = 0

' hold incoming data
Dim streamRead As New MemoryStream
Dim TempString As String

While bReadMessage
Dim buf(1023) As Byte

' loop that reads incoming message
' buf is temp holder and the MemoryStream
' contains all of the bits

bytesRead = _socket.Receive(buf)
If bytesRead > 0 Then
streamRead.Write(buf, 0, bytesRead)
bReadMessage = Not
Network.CheckForTerminator(streamRead.ToArray())
totalBytes += bytesRead
Else
' client disconnected
Throw New Exception("Client disconnected.")
End If
End While

' done reading incoming message, now process the
command
ProcessCommand(streamRead)
streamRead.Close()
End While

Catch ex As Exception
End Try
End Sub

' Process the command that was received from the client.
Private Sub ProcessCommand(ByVal streamRead As MemoryStream)
Try
' remove message terminator
streamRead.SetLength((streamRead.Length -
Network.Terminator.Length))
' get the command data
streamRead.Position = 0
Dim data As Byte() = streamRead.ToArray()

If System.Text.Encoding.ASCII.GetString(data) = "helo2u2"
Then
MsgBox(System.Text.Encoding.ASCII.GetString(data))
End If

Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi Mike,

Without Bill Ryan, I expect he sees this and normaly he answer than., There
are not so much who use the pocket PC programming in this newsgroup.

However there is a special newsgroup

microsoft.public.dotnet.framework.compactframework

I advice you to ask it there as well.

Cor
Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.