help with string!!!

ok i have a project, in my poject  i have a client and a server the client send data to server over tcp/ip on a socket

My data arrives as a bytes and then they are converted to chars..

Here`s the code a little bit

CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;

//end receive...

int iRx = 0;

iRx = theSockId.thisSocket.EndReceive(asyn);

char[] chars = new char[iRx + 1];

// decodes the bytes

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);

command=command+szData.

MessageBox.Show(szData);

WaitForData(m_socWorker);

 

comand is a string type , my problem is here:

command=command+szData. // the command foes not take new value

for example:

first  string command="";

and szData="b";

command=command+szData. // now command is ="b";

szData="c";

command=command+szData. // now command it supposed to be "bc"

well it doesnt it still b;

What am i doing wrong

 

 

 




Answer this question

help with string!!!

  • JonM

    You could think about using the System.Text.StringBuilder class for your command variable. System.String creates a new string every time you append to it, whereas StringBuilder doesn't, which makes it much more efficient. Other than that, I do not know what your problem is.
  • CalBob

    man szData works fine, when he supposed to be "b" its "b" when its supposed to be "c" is "c"

    the realy problem is with command

    commnad =command+szData;

    he only take szData value once, and then he doesnt take the new value of szData



  • mrferg

    public AsyncCallback pfnWorkerCallBack;

    public Socket m_socListener;

    public Socket m_socWorker;

    string commanda = "";

     

    //this is in button 1 click

    try

    {

    //create the listening socket...

    m_socListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 8221);

    //bind to local IP Address...

    m_socListener.Bind(ipLocal);

    //start listening...

    m_socListener.Listen(19);

    // create the call back for any client connections...

    m_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null);

    }

    catch (SocketException se)

    {

    MessageBox.Show(se.Message);

    } // end f button 1 click

     

    public void OnClientConnect(IAsyncResult asyn)

    {

    try

    {

    m_socWorker = m_socListener.EndAccept(asyn);

    WaitForData(m_socWorker);

    }

    catch (ObjectDisposedException)

    {

    System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");

    }

    catch (SocketException se)

    {

    MessageBox.Show(se.Message);

    }

    }

    public class CSocketPacket

    {

    public System.Net.Sockets.Socket thisSocket;

    public byte[] dataBuffer = new byte[1];

    }

    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);

    }

    }

    string b = "";

    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);

    b = b + szData.ToString();

    WaitForData(m_socWorker);

    }

    catch (ObjectDisposedException)

    {

    System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");

    }

    catch (SocketException se)

    {

    MessageBox.Show(se.Message);

    }

    }

     

    this is the server side o a network program, so as u could see the problem is in OnDataRecevice



  • crystalreports

    Maybe there's not enough code to be able to see where the problem is, but it could be inherent to the scope of command reference.
  • VOEagain

    I can only assume that szData is "" when you think it is "c". Debug it and check the value of chars. Is it null terminated
  • help with string!!!