sockets - c# length of data througth StreamWriter -
i have written code client server using tcp/sockets in c# working fine. problem why able send limited size data client server @ single instance.
following server code
public class asynchioserver { // server port number const int port = 8001; // server ip address const string ip = "127.0.0.1"; const int maxbuffer = 10000; static ipaddress ipaddress = ipaddress.parse(ip); static tcplistener tcplistener = new tcplistener(ipaddress, port); static void listeners() { try { socket socketforclient = tcplistener.acceptsocket(); if (socketforclient.connected) { console.writeline("client : " + socketforclient.remoteendpoint + " connected server."); networkstream networkstream = new networkstream(socketforclient); system.io.streamwriter streamwriter = new system.io.streamwriter(networkstream); system.io.streamreader streamreader = new system.io.streamreader(networkstream); while (true) { string thestring = streamreader.readline(); if (thestring != "exit") { // original message client console.writeline("------------------------------------------------------------------------------"); console.writeline("message recieved client(" + socketforclient.remoteendpoint + ") : " + thestring); // ascii code message client console.write("ascii code message : "); foreach (char c in thestring) { console.write(system.convert.toint32(c) + " "); } // hex value of message client string hex = ""; foreach (char c in thestring) { int tmp = c; hex += string.format("{0:x2}", (uint)system.convert.touint32(tmp.tostring())); } console.writeline(); console.writeline("hex code message client : " + hex); //sending acknowledgement client console.writeline(); socketforclient.send(new asciiencoding().getbytes(/*"the string recieved client(" + socketforclient.remoteendpoint + ") : " + */thestring)); } // end of if loop // if exit client else { console.writeline(); console.writeline("client " + socketforclient.remoteendpoint + " has exited"); break; } } // end of while loop streamreader.close(); networkstream.close(); streamwriter.close(); } // end of if loop socketforclient.close(); console.writeline(); // console.writeline("press key exit server program"); console.readkey(); } // end of try loop catch (exception e) { console.writeline("the process failed: {0}", e.tostring()); console.writeline("message not received client"); } } // end of listener loop // number of clients can connect server public static void main() { tcplistener.start(); console.writeline("********** server program **********"); console.write("number of clients can connect server : "); int numberofclientsyouneedtoconnect = int.parse(console.readline()); (int = 0; < numberofclientsyouneedtoconnect; i++) { thread newthread = new thread(new threadstart(listeners)); newthread.start(); } console.writeline(); } // end of min loop } // end of public class asynchioserver
while client code is
public class client { // acknowledgement server const int maxbuffer = 1000; static public void main(string[] args) { // ip address of server client should connected string ip; // port number @ server listening client connection int port; console.write("enter ip address: "); ip = console.in.readline(); console.write("enter port number: "); port = int.parse(console.in.readline()); tcpclient socketforserver; try { // connect server @ ipaddress ip , port number port socketforserver = new tcpclient(ip, port); } catch { console.writeline("failed connect server @ {0}:{1}", ip , port); console.readline(); return; } // initializing streamreader , streamwriter sending or reading message server networkstream networkstream = socketforserver.getstream(); system.io.streamreader streamreader = new system.io.streamreader(networkstream); system.io.streamwriter streamwriter = new system.io.streamwriter(networkstream); try { console.writeline(); console.writeline("---begin sending message(type 'exit' disconnect server)---"); console.writeline(); console.write("type message : "); string str = console.readline(); while (str != "exit") { streamwriter.writeline(str); streamwriter.flush(); // receiving acknowledgement server byte[] receivebuffer = new byte[maxbuffer]; int k = networkstream.read(receivebuffer, 0, maxbuffer); (int = 0; < k; i++) console.write(/*convert.tochar(*/receivebuffer[i]); console.writeline(); console.writeline("------------------------------------------------------"); console.write("type message : "); str = console.readline(); } // client close connection server if (str == "exit") { streamwriter.writeline(str); streamwriter.flush(); } } // end of try loop catch { console.writeline("exception reading server"); } // exit client networkstream.close(); console.writeline("press key exit client program"); console.readkey(); } // end of main loop } // end of public class client
now when run program enter following binary sent client first message
type message : 11111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111
problem doesn't allow me enter more binary data. can send more binary data if want send pressing enter , sending 2nd message. want send single message. limit send more data @ single instance on tcp/sockets?
that has readline method limiting input 256 characters, see remarks section workaround:
http://msdn.microsoft.com/en-us/library/system.console.readline.aspx
edit: typo
Comments
Post a Comment