Chars received
-
Hi I am trying to figure out why my method keeps only showing me the first character from a sent string. I would like it to show all the characters that were sent.
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;int iRx = 0; iRx = socketData.currentSoc.EndReceive(asyn); char\[\] chars = new char\[iRx\]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0); System.String szData = new System.String(chars); MessageBox.Show(szData);<<<<<<<<<<<
-
Hi I am trying to figure out why my method keeps only showing me the first character from a sent string. I would like it to show all the characters that were sent.
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;int iRx = 0; iRx = socketData.currentSoc.EndReceive(asyn); char\[\] chars = new char\[iRx\]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0); System.String szData = new System.String(chars); MessageBox.Show(szData);<<<<<<<<<<<
-
Instead of
System.String szData = new System.String(chars)
you could try
StringBuilder str = new StringBuilder();
for (int x = 0; x < charLen; x++)
{
str.Append(chars[x]);
}I haven't had a problem with that.
Brent
Hate to break it you, but this had no effect.
-
Hi I am trying to figure out why my method keeps only showing me the first character from a sent string. I would like it to show all the characters that were sent.
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;int iRx = 0; iRx = socketData.currentSoc.EndReceive(asyn); char\[\] chars = new char\[iRx\]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0); System.String szData = new System.String(chars); MessageBox.Show(szData);<<<<<<<<<<<
What happens when you put a break point at socketData.currentSoc.EndReceive and then step over the code? What do you see when you watch the variables? [Edit]I see you don't like debugging - too much like hard work is it?[/Edit]
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Onyx
modified on Saturday, October 23, 2010 6:47 AM
-
Hi I am trying to figure out why my method keeps only showing me the first character from a sent string. I would like it to show all the characters that were sent.
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;int iRx = 0; iRx = socketData.currentSoc.EndReceive(asyn); char\[\] chars = new char\[iRx\]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0); System.String szData = new System.String(chars); MessageBox.Show(szData);<<<<<<<<<<<
Several things could be wrong here, however I can't tell which one(s) it will be as you have shown only a fraction of the relevant code. Here are the main possibilities: 1. you may be invoking a receive with a small buffer; if you were to ask for one byte, then you would only get one byte. 2. you might have an encoding mismatch between sender and receiver. e.g. if the sender uses Unicode, regular characters would look like real (ANSI) bytes and NULLs intertwined; an 8-bit encoder would react badly on the NULLs. 3. what EndReceive returns is the number of bytes, not characters, hence
char[] chars = new char[iRx];
is wrong. IMO you should use [EDIT]Encoding.GetString()
right away to convert bytes into a string.[/EDIT] :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
modified on Friday, October 22, 2010 10:04 PM