Hey, i have an Async nonblocking client/server application which should send strings to each other. Only the problem is that the string comes in in parts.
So i send the following string with the client to the server:
Hello
Then the server recieves it like this:
H
e
l
l
o
But it is important that i can send a complete strings to the server. Anyone any idea why this isn't happening now??
Client, send string button
Server: Receive string and display in messageboxCode:try { Object objData = "Hello"; byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString()); m_socClient.Send(byData); } catch (SocketException se) { MessageBox.Show(se.Message); }
Code:public void WaitForData(System.Net.Sockets.Socket soc) { try { if (pfnWorkerCallBack == null) { pfnWorkerCallBack = new AsyncCallback(OnDataReceived); } CSocketPacket theSocPkt = new CSocketPacket(); theSocPkt.thisSocket = soc; // now start to listen for any data... soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt); } catch (SocketException se) { MessageBox.Show(se.Message); } } public void OnDataReceived(IAsyncResult asyn) { try { CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState; //end receive... int iRx = 0; iRx = theSockId.thisSocket.EndReceive(asyn); char[] chars = new char[iRx + 1]; 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); //Displays the string one letter at a time... MessageBox.Show(szData); WaitForData(m_socWorker); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n"); } catch (SocketException se) { MessageBox.Show(se.Message); } }




Reply With Quote