Problem with getting string from bytes
-
Ok here is the problem. I am developing a server in C and a client in C#. The server send streams of data to the client, and the client is responsible for decoding and interpreting these stream. So, if I send a simple "Hello world" from the server to the client, it works. If I send a byte representing an integer (I use memcpy) I can decode it in the client. But, the problems start when after some integers, I also put a string. If I put 2 bytes, each representing some number, I am still able to decode the string that follows using: System.Text.Encoding.ASCII.GetString(buffer,2,length) But if I say 3 bytes, and then the string, I don't get anything (yes, I said buffer,3,length to reflect the server changes), I get garbage.. Can anyone please help me? Thank you.
-
Ok here is the problem. I am developing a server in C and a client in C#. The server send streams of data to the client, and the client is responsible for decoding and interpreting these stream. So, if I send a simple "Hello world" from the server to the client, it works. If I send a byte representing an integer (I use memcpy) I can decode it in the client. But, the problems start when after some integers, I also put a string. If I put 2 bytes, each representing some number, I am still able to decode the string that follows using: System.Text.Encoding.ASCII.GetString(buffer,2,length) But if I say 3 bytes, and then the string, I don't get anything (yes, I said buffer,3,length to reflect the server changes), I get garbage.. Can anyone please help me? Thank you.
-
What kind of "garbage" do you get? I think that we have to see some code in order to tell what's wrong with the code...
--- single minded; short sighted; long gone;
Okay then. Here is the server memcpy(msg,&type,1); memcpy(msg+1,&nr,1); memcpy(msg+2,&online,1); memcpy(msg+3,&p,sizeof(p)); type is 2, nr is 4 online is 0 and p is "Hello world". And on the client I get some unrecognizable characters (those squares you sometimes get). BUT, when online is 1 the string stays in one piece.. What am I supposed to make of this?
-
Okay then. Here is the server memcpy(msg,&type,1); memcpy(msg+1,&nr,1); memcpy(msg+2,&online,1); memcpy(msg+3,&p,sizeof(p)); type is 2, nr is 4 online is 0 and p is "Hello world". And on the client I get some unrecognizable characters (those squares you sometimes get). BUT, when online is 1 the string stays in one piece.. What am I supposed to make of this?
if only you had shown the client code and the server code in one message... and, if at all possible, the hex representation of these data bytes arriving at the client.
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
if only you had shown the client code and the server code in one message... and, if at all possible, the hex representation of these data bytes arriving at the client.
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
But I already showed more than enough. Since I am able to transmit messages between the server and the client THERE IS NO PROBLEM HERE. Anyway, the server uses fprintf and the client uses BeginReceive - EndReceive (IAsync bla bla). The problem is the way I am handling the array of bytes coming from the server. I suspect EndReceive thinks that a 0 means \0 (as in end of string). So it stops there. I don't know why this happens..
-
But I already showed more than enough. Since I am able to transmit messages between the server and the client THERE IS NO PROBLEM HERE. Anyway, the server uses fprintf and the client uses BeginReceive - EndReceive (IAsync bla bla). The problem is the way I am handling the array of bytes coming from the server. I suspect EndReceive thinks that a 0 means \0 (as in end of string). So it stops there. I don't know why this happens..
nc3b wrote:
But I already showed more than enough
Sorry for trying to help you. :(( Debug Rule #1: when the system does not perform as designed, at least one element is wrong, and it could be anything; not a single assumption is beyond scrutiny.
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
But I already showed more than enough. Since I am able to transmit messages between the server and the client THERE IS NO PROBLEM HERE. Anyway, the server uses fprintf and the client uses BeginReceive - EndReceive (IAsync bla bla). The problem is the way I am handling the array of bytes coming from the server. I suspect EndReceive thinks that a 0 means \0 (as in end of string). So it stops there. I don't know why this happens..
nc3b wrote:
But I already showed more than enough.
I haven't seen a single line of the code that recieves the message.
nc3b wrote:
Since I am able to transmit messages between the server and the client THERE IS NO PROBLEM HERE.
Just because some code works in some cases doesn't automatically mean that it works in all cases.
nc3b wrote:
I suspect EndReceive thinks that a 0 means \0 (as in end of string). So it stops there. I don't know why this happens..
I don't know why it happens either, as I don't know if you use BeginRecieve and EndRecieve correctly.
--- single minded; short sighted; long gone;
-
nc3b wrote:
But I already showed more than enough.
I haven't seen a single line of the code that recieves the message.
nc3b wrote:
Since I am able to transmit messages between the server and the client THERE IS NO PROBLEM HERE.
Just because some code works in some cases doesn't automatically mean that it works in all cases.
nc3b wrote:
I suspect EndReceive thinks that a 0 means \0 (as in end of string). So it stops there. I don't know why this happens..
I don't know why it happens either, as I don't know if you use BeginRecieve and EndRecieve correctly.
--- single minded; short sighted; long gone;
-
The thing is that it's a lot of code.. Anyways, let's see... :) The BeginReceive
sock.BeginReceive(buffer,0,buffer.Length,SocketFlags.None,receive,_sock);
The EndReceiveint stop=_sock.EndReceive(result);
Is it not correct? :confused: -
That code is correct, but I have no idea if the surrounding code is correct, so that the buffer, receive, _sock and result variables are correct.
--- single minded; short sighted; long gone;
I might as well archive the project and attach it :laugh: But anyways. I don't mind:)
private System.Byte[] buffer=new Byte[1024]; private void SetupReceiveCallback(Socket _sock) { AsyncCallback receive=new AsyncCallback(OnReceive); _sock.BeginReceive(buffer,0,buffer.Length,SocketFlags.None,receive,_sock); } private void OnReceive(IAsyncResult result) { try { Socket _sock=(Socket)result.AsyncState; int stop=_sock.EndReceive(result); String str=System.Text.Encoding.ASCII.GetString(buffer,0,stop); MessageBox.Show(str); process(buffer,stop); SetupReceiveCallback(_sock); } catch { MessageBox.Show("Out"); this.Close(); } }
So? :laugh: