PC Review


Reply
Thread Tools Rate Thread

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

 
 
Mike Dole
Guest
Posts: n/a
 
      13th May 2004
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
 
Reply With Quote
 
 
 
 
Cor Ligthert
Guest
Posts: n/a
 
      14th May 2004
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


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with client computer in the client/server network windows server 2003 gogi100 Windows XP Networking 1 1st Sep 2007 05:23 PM
.NET Socket-ReConnection Error-Connecting To UnWorking Server Sock =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= Microsoft VC .NET 0 21st Feb 2007 05:21 PM
.NET Socket-ReConnection Error-Connecting To UnWorking Server Sock =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= Microsoft C# .NET 0 21st Feb 2007 05:21 PM
getting client ip in sock-client on server Ankit Aneja Microsoft C# .NET 14 23rd Jan 2006 11:31 AM
Client / Server 'Shall I put a sock(et) in it?' Mike Dole Microsoft Dot NET Framework 1 14th May 2004 04:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:26 PM.