Tom Knowlton
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(IAsyncResul t asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncSt ate ;
int iRx = theSockId.thisSocket.EndRe ceive (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.dataB uffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
WaitForData();
}
catch (ObjectDisposedException )
{
System.Diagnostics.Debugge r.Log(0,"1 ","\nOnDat aReceived: Socket has been closed\n");
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
How can I make the client receive the entire string being sent from the server all at once?
public void OnDataReceived(IAsyncResul
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncSt
int iRx = theSockId.thisSocket.EndRe
//char[] chars = new char[iRx + 1];
char[] chars = new char[50];
System.Text.Decoder d = System.Text.Encoding.UTF8.
int charLen = d.GetChars(theSockId.dataB
System.String szData = new System.String(chars);
WaitForData();
}
catch (ObjectDisposedException )
{
System.Diagnostics.Debugge
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
ASKER
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.
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.
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.EndRea d(asyncRes ult)
End SyncLock
If intcount < 1 Then
RaiseEvent Disconnected(Me)
Exit Sub
End If
ByteToString(bClientData, intcount)
SyncLock objClient.GetStream
objClient.GetStream.BeginR ead(bClien tData, 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
Private Sub ReceiveClientData(ByVal asyncResult As IAsyncResult)
Dim intcount As Integer
Try
SyncLock objClient.GetStream
intcount = objClient.GetStream.EndRea
End SyncLock
If intcount < 1 Then
RaiseEvent Disconnected(Me)
Exit Sub
End If
ByteToString(bClientData, intcount)
SyncLock objClient.GetStream
objClient.GetStream.BeginR
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(
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!
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!
BTW, this is the C# topic area. *GRIN*
Bob
Bob
ASKER
>>>BTW, this is the C# topic area. *GRIN*
I know. :)
I know. :)
ASKER
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
ASKER
Bob:
I haven't really tried anything yet.
If someone could port the VB code over to C# that would help.
Tom
I haven't really tried anything yet.
If someone could port the VB code over to C# that would help.
Tom
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
void SendData()
{
try
{
//Object objData = richTextBoxSendMsg.Text;
Object objData = linesFromFile[curLine];
byte[] byData = System.Text.Encoding.ASCII
for(int i = 0; i < m_clientCount; i++)
{
if(m_workerSocket[i] != null)
{
if(m_workerSocket[i].Conne
{
m_workerSocket[i].Send (byData);
}
}
}
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}