c# - Unable to read beyond the end of the stream is caused when use readbinary -
i trying send value socket .so have 2 parts in project client , server .
the client sends value server using code :
networkstream networkstream = socketforserver.getstream(); system.io.binarywriter binarywriter = new system.io.binarywriter(networkstream); //------ int messagesource = 0; int messagedesitination = 0; int interlockingid = 0; int trackid = 0; int trainid = 2; int direction = 0; int messagetype = 0; int informationtype = 0; int datetime = 0; foreach (sensor leftsensorlist in leftsensor) { binarywriter.write(messagesource); binarywriter.write(messagedesitination); binarywriter.write(interlockingid); binarywriter.write(trackid); binarywriter.write(trainid); binarywriter.write(direction); binarywriter.write(messagetype); binarywriter.write(informationtype); binarywriter.write(datetime); binarywriter.flush(); binarywriter.close(); thread.sleep(4000); }
in server part should read binary values :
static void listeners() { socket socketforclient = tcplistener.acceptsocket(); if (socketforclient.connected) { console.writeline("client:" + socketforclient.remoteendpoint + " connected server."); networkstream networkstream = new networkstream(socketforclient); while (true) { timetablerepository objtimetablerepository = new timetablerepository(); sensorrepository objsensorrepository = new sensorrepository(); arrivaltimerepository objarrivaltimerepository=new arrivaltimerepository(); trainrepository objtrainrepository = new trainrepository(); using (var reader = new binaryreader(networkstream, encoding.default, true)) { int messagesource = reader.readint32(); int messagedesitination = reader.readint32(); int interlockingid = reader.readint32(); int trackid = reader.readint32(); int trainid = reader.readint32(); int direction = reader.readint32(); int messagetype =reader.readint32(); int informationtype = reader.readint32(); int datetime = reader.readint32(); } } networkstream.close(); } socketforclient.close(); console.writeline("press key exit server program"); console.readkey(); }
so when client send value can value in server part first time ,when client sends values server got error :
unable read beyond end of stream
why ?
i'd rewrite listener method this. nb : untested code.
static void listeners() { socket socketforclient = tcplistener.acceptsocket(); if (socketforclient.connected) { using (var reader = new binaryreader(new networkstream(socketforclient), encoding.default, true)) { while (((int)reader.peekchar()) != -1) { list<int> variables = new list<int>(); int messagesource = reader.readint32(); int messagedesitination = reader.readint32(); int interlockingid = reader.readint32(); int trackid = reader.readint32(); int trainid = reader.readint32(); int direction = reader.readint32(); int messagetype =reader.readint32(); int informationtype = reader.readint32(); int datetime = reader.readint32(); } } }
edit : upon closer inspection noticed few things. try this:
networkstream networkstream = socketforserver.getstream(); system.io.binarywriter binarywriter = new system.io.binarywriter(networkstream); //------ int messagesource = 0; int messagedesitination = 0; int interlockingid = 0; int trackid = 0; int trainid = 2; int direction = 0; int messagetype = 0; int informationtype = 0; int datetime = 0; foreach (sensor leftsensorlist in leftsensor) { binarywriter.write(messagesource); binarywriter.write(messagedesitination); binarywriter.write(interlockingid); binarywriter.write(trackid); binarywriter.write(trainid); binarywriter.write(direction); binarywriter.write(messagetype); binarywriter.write(informationtype); binarywriter.write(datetime); binarywriter.flush(); thread.sleep(4000); } //hint : changes here binarywriter.close();
Comments
Post a Comment