Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

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 );
                  }
            }      
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

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 );
                  }
            }
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.
Avatar of checoo
checoo

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
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!
Avatar of Bob Learned
BTW, this is the C# topic area. *GRIN*

Bob
>>>BTW, this is the C# topic area. *GRIN*

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

Bob
Bob:

I haven't really tried anything yet.

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

Tom
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial