c# - Multithreaded TCP server and client -


i search around before asking problem, looking example of tcp server , client in c#.

i found this link,i used given code link unfortunately there's problem , i'm stocked!

the following code complete code server (console)

using system; using system.collections.generic; using system.linq; using system.text; using system.net.sockets;  namespace tcp_server_console {     class program     {         static void main(string[] args)         {             tcplistener serversocket = new tcplistener(8888);             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[10025];                     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();         }     } }   /*handleclient class */ using system; using system.collections.generic; using system.linq; using system.net.sockets; using system.text; using system.threading; using system.threading.tasks;  namespace tcp_server_console {     public class handleclient     {         tcpclient clientsocket;         string clno;         public void startclient(tcpclient inclientsocket, string clineno)         {             this.clientsocket = inclientsocket;             this.clno = clineno;             thread ctthread = new thread(dochat);             ctthread.start();         }          private void dochat()         {             int requestcount = 0;             byte[] bytesfrom = new byte[10025];             string datafromclient = null;             byte[] sendbytes = null;             string serverresponse = null;             string rcount = null;             requestcount = 0;             while ((true))             {                 try                 {                     requestcount = requestcount + 1;                     networkstream networkstream = clientsocket.getstream();                     networkstream.read(bytesfrom, 0, (int)clientsocket.receivebuffersize);                     datafromclient = system.text.encoding.ascii.getstring(bytesfrom);                     datafromclient = datafromclient.substring(0, datafromclient.indexof("$"));                     console.writeline(" >> " + "from client-" + clno + datafromclient);                     rcount = convert.tostring(requestcount);                     serverresponse = "server clinet(" + clno + ") " + rcount;                     sendbytes = encoding.ascii.getbytes(serverresponse);                     networkstream.write(sendbytes, 0, sendbytes.length);                     networkstream.flush();                     console.writeline(" >> " + serverresponse);                 }                 catch (exception ex)                 {                     console.writeline(" >> " + ex.tostring());                 }             }         }     } } 

the following code complete code client(windows form):

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.net.sockets; using system.text; using system.threading.tasks; using system.windows.forms;  namespace tcp_client_sample {     public partial class form1 : form     {          system.net.sockets.tcpclient clientsocket = new system.net.sockets.tcpclient();         networkstream serverstream;         public form1()         {             initializecomponent();         }         private void form1_load(object sender, eventargs e)         {             label_status.text = "client started, connecting...";             try {                 clientsocket.connect("127.0.0.1", 8888);                 if (clientsocket.connected)                 {                     label_status.text = "connected";                     label_status.forecolor = color.green;                 }              }               catch (exception xe) {                   messagebox.show("oops!!! server must started first! \n\n" + xe.tostring());               }          }          //function send message server (on button click)         private void btn_send_click(object sender, eventargs e)         {             try {                 networkstream serverstream = clientsocket.getstream();                 byte[] outstream = system.text.encoding.ascii.getbytes("message client$");                 serverstream.write(outstream, 0, outstream.length);                 serverstream.flush();                 byte[] instream = new byte[10025];                 serverstream.read(instream, 0, (int)clientsocket.receivebuffersize);                 string returndata = system.text.encoding.ascii.getstring(instream);                 chatbox.appendtext(">> server: " + returndata);             }             catch (exception ex) {                 messagebox.show("unable send data: " + ex);             }         }     } } 

everytime execute code of server works , started. when execute client, server given me exception error "specified argument out of range".

here complete exception error:

system.argumentoutofrangeexception: specified argument out of range of valid values. parameter name: size    @ system.net.sockets.networkstream.read(byte[] buffer, int32 offset, int32 size)    @ tcp_server_console.program.main(string[] args) in c:\users\user\documents\visual studio 2015\projects\tcp_server_console\tcp_server_console\program.cs:line 50 

i'm getting same error everytime click btn_send client form.

i stock here , don't know what's problem since first time i'm work tcp socket.

anynone can me?

the problem lies in code used 2 different sizes

byte[] instream = new byte[10025]; serverstream.read(instream, 0, (int)clientsocket.receivebuffersize); 

in this. youve reserved space 10025 bytes, recievebuffersize maybe size, bigger or smaller, if bigger, error.

if make read call call same length byte array wont have problem.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -