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 );
                  }
            }      
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
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
 
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.