• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Read in string instead of chars

The Server is sending 20 characters at a time...but the client is only receiving a few characters (one or two characters) at a time.


How can I make the client receive the entire string being sent from the server all at once?


public  void OnDataReceived(IAsyncResult asyn)
            {
                  try
                  {
                        SocketPacket theSockId = (SocketPacket)asyn.AsyncState ;
                        int iRx  = theSockId.thisSocket.EndReceive (asyn);
                        //char[] chars = new char[iRx +  1];
                        char[] chars = new char[50];
                        System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                        int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
                        System.String szData = new System.String(chars);

                        
                        WaitForData();
                  }
                  catch (ObjectDisposedException )
                  {
                        System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");
                  }
                  catch(SocketException se)
                  {
                        MessageBox.Show (se.Message );
                  }
            }      
0
Tom Knowlton
Asked:
Tom Knowlton
1 Solution
 
Tom KnowltonWeb developerAuthor Commented:
Here is what the Server side is running:

void SendData()
            {
                  try
                  {
                        //Object objData = richTextBoxSendMsg.Text;
                        Object objData = linesFromFile[curLine];
                        byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
                        for(int i = 0; i < m_clientCount; i++)
                        {
                              if(m_workerSocket[i] != null)
                              {
                                    if(m_workerSocket[i].Connected)
                                    {
                                          m_workerSocket[i].Send (byData);
                                    }
                              }
                        }
                        
                  }
                  catch(SocketException se)
                  {
                        MessageBox.Show (se.Message );
                  }
            }
0
 
Tom KnowltonWeb developerAuthor Commented:
In other words, the Server is sending

BEGIN


The client is receiving:

B


then


E


then


G


then


I


then



N




I want it to grab "BEGIN"   all at once, just as the Server is sending it.
0
 
checooCommented:
the following sample code might be usefull --

   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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
cyberdevil67Commented:
Hi knowlton,

 try converting the byte array to a string then send it as a string instead of a byte array, I have always send these things as a string in these type of cases.

Cheers!
0
 
Bob LearnedCommented:
BTW, this is the C# topic area. *GRIN*

Bob
0
 
Tom KnowltonWeb developerAuthor Commented:
>>>BTW, this is the C# topic area. *GRIN*

I know.  :)
0
 
Tom KnowltonWeb developerAuthor Commented:
Ohhh....that comment was for checoo......
0
 
Bob LearnedCommented:
That was for anyone who noticed that was VB code :)  Did any of that work for you, Tom?

Bob
0
 
Tom KnowltonWeb developerAuthor Commented:
Bob:

I haven't really tried anything yet.

If someone could port the VB code over to C# that would help.

Tom
0
 
Bob LearnedCommented:
private void ReceiveClientData(IAsyncResult asyncResult)
{
 int intcount;
 try {
   lock (objClient.GetStream) {
     intcount = objClient.GetStream.EndRead(asyncResult);
   }
   if (intcount < 1) {
     if (Disconnected != null) {
       Disconnected(this);
     }
return;
   }
   ByteToString(bClientData, intcount);
   lock (objClient.GetStream) {
      // might be wrong, please check
     objClient.GetStream.BeginRead(bClientData, 0, 1024,new EventHandler(ReceiveClientData), null);
   }
 } catch (Exception e) {
   if (Disconnected != null) {
     Disconnected(this);
   }
 }
}

private void ByteToString(byte[] Bytes, int intCount)
{
 for (int intIndex = 0; intIndex <= intCount - 1; intIndex++) {
   if (Bytes(intIndex) == 94) {
     if (DataReceived != null) {
       DataReceived(this, objText.ToString);
     }
     objText = new StringBuilder();
   } else {
     objText.Append(ChrW(Bytes(intIndex)));
   }
 }
}


Bob
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now