c# - Out of memory exception with BeginRead -


the idea behind small project developing chat application difference want send objects instead of plain strings. far, have.

if deserialize on constructor, works fine (userdto has 2 string fields now), however, plan on having multiple clients sending data server anytime wish. i'm having difficulty understanding how works , how fix error (like this, gives "exception of type 'system.outofmemoryexception' thrown." @ deseralize line) after reading ms's documentation , i'd ideas guys.

note whoever tries compile this: binaryformatter has way of doing in: let's userdto has properties string name, string email applying class client , server, must build using class library , add reference of both projects, because somehow binaryformater says tho if create same class in both projects, deserializing claims cannot map object. i'll leaving sample of client using below.

server:

class program { const int serverport = 60967; static list<userconnection> clientlist = new list<userconnection>(); static tcplistener listener; static thread listenerthread;   static void main(string[] args) {         listenerthread = new thread(new threadstart(dolisten));         listenerthread.start();         console.writeline("server started");         //while (true) {             string = console.readline()         //}    }  static void dolisten() {         try {             listener = new tcplistener(system.net.ipaddress.any, serverport);             listener.start();             console.writeline("listening [...]");             {                 userconnection client = new userconnection(listener.accepttcpclient());                 //clientlist.add(client);                 console.writeline("new connection found");              } while (true);         }         catch (exception ex) {             console.writeline(ex.tostring());         }     } }    public class userconnection { private tcpclient clientinfo; private byte[] readbuffer = new byte[2000]; const int read_buffer_size = 2000;  public userconnection(tcpclient client) {     clientinfo = client;     clientinfo.getstream().beginread(readbuffer, 0, read_buffer_size, new asynccallback(streamreceiver), null); }  private void streamreceiver(iasyncresult ar) {     try     {         if (client.getstream().canread) {         lock (clientinfo.getstream()) {             var strm = clientinfo.getstream();             int bytesread = clientinfo.getstream().endread(ar);             binaryformatter formatter = new binaryformatter();             var mydat = (userdto)formatter.deserialize(strm);         }         lock (clientinfo.getstream()) {             clientinfo.getstream().beginread(readbuffer, 0, read_buffer_size, new asynccallback(streamreceiver), null);         }     }      catch (exception e) {      console.writeline(ex.tostring());     } } 

client:

class program {     static void main(string[] args) {             connectresult("localhost", 60967);             console.readline();         }     }      static string connectresult(string ip, int port) {         try {             tcpclient client = new tcpclient(ip, port);             attemptlogin(client);             return "connection succeeded";         }         catch (exception ex) {             return "server not active.  please start server , try again.      " + ex.tostring();         }     }      static void attemptlogin(tcpclient client) {         userdto obj = new userdto("email", "username");         iformatter formatter = new binaryformatter();         var stream = client.getstream();          formatter.serialize(stream, obj);         console.writeline("sent object");     } } 

instead of doing of beginread() calls, try taking stream , passing binaryformatter.deserialize() method.

public userconnection(tcpclient client) {     clientinfo = client;     //clientinfo.getstream().beginread(readbuffer, 0, read_buffer_size, new asynccallback(streamreceiver), null);     var strm = clientinfo.getstream();     binaryformatter formatter = new binaryformatter();     var mydat = (userdto)formatter.deserialize(strm); } 

my guess stream position moved, if not @ end. when pass deserialize(), there isn't anymore data read. in fact, byte[] readbuffer has of data wanted if dto can't hold more 2000 bytes. if case, should able use bytes in readbuffer deserialize instead.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -