c# - system.argumentoufofrange exception :lenght cannot be less than 0 (line 32) overflow exception( Line 29) -


sup people im creating program listen incoming string works...well have coded far works problem if send string program throws argument out of range exception

        using system;         using system.collections.generic;         using system.linq;         using system.text;         using system.threading.tasks;         using system.net.sockets;    namespace stringlistener   {   class program   {     static void main(string[] args)     {         tcplistener serversocket = new tcplistener(my port number);         int requestcount = 0;         tcpclient clientsocket = default(tcpclient);         serversocket.start();         console.writeline(" >> server started");         clientsocket = serversocket.accepttcpclient();         console.writeline(" >> accept connection client");         requestcount = 0;          while ((true))         {             try             {                 requestcount = requestcount + 1;                 networkstream networkstream = clientsocket.getstream();                 byte[] bytesfrom = new byte[10000000000025];                 networkstream.read(bytesfrom, 0, (int)clientsocket.receivebuffersize);                 string datafromclient = system.text.encoding.ascii.getstring(bytesfrom);                 datafromclient = datafromclient.substring(0, datafromclient.indexof("$"));                 console.writeline(" >> data client - " + datafromclient);                 string serverresponse = "last message client" + datafromclient;                 byte[] sendbytes = encoding.ascii.getbytes(serverresponse);                 networkstream.write(sendbytes, 0, sendbytes.length);                 networkstream.flush();                 console.writeline(" >> " + serverresponse);             }             catch (exception ex)             {                 console.writeline(ex.tostring());             }             clientsocket.close();             serversocket.stop();             console.writeline(" >> exit");             console.readline();          }       } } 

}

 string: <?xml version="1.0" encoding="utf-8"?> <faf>             <hah>                             <gr>00.00</gr>                             <yy>000000000</yy>             </hah>             <ee>                             <gr>00.00</gr>                             <yy>000000000</yy>             </ee> </faf> 

any ideas? problem side if how fix it?
explanation , answer appreciated wish understand stop same problem recurring in future

thanks in advance

string.substring(int, int) expects starting index in first parameter , length in second parameter. indexof return -1 if search string not found in supplied string.

so code:

datafromclient.substring(0, datafromclient.indexof("$")) 

is looking "$" in string. if datafromclient doesn't contain "$", indexof returns -1, makes substring this:

datafromclient.substring(0, -1) 

the documentation (linked above) states argumentoutofrangeexception thrown when startidnex plus length indicates position not within instance or startindex or length less zero.

the length must greater 0, , -1 not. hence error.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -