c# - TCP sending images client and server -
i have client application , server application. client application activates function every 40 milliseconds: *note nstream networkstream instance.
private void sendscreen(networkstream nstream) { streamwriter author = new streamwriter(nstream); screencapture sc = new screencapture(); system.drawing.image img = sc.capturescreen(); memorystream ms = new memorystream(); img.save(ms, system.drawing.imaging.imageformat.bmp); byte[] buffer = new byte[ms.length]; ms.seek(0, seekorigin.begin); ms.read(buffer, 0, buffer.length); console.writeline(buffer.length); writer.writeline(buffer.length); writer.flush(); nstream.write(buffer, 0, buffer.length); }
and server application code receiving image:
private async void showimage() { while (true) { string num = await reader.readlineasync(); console.writeline(num); int ctbytes = int.parse(num); byte[] buffer = new byte[ctbytes]; await stream.readasync(buffer, 0, buffer.length); memorystream ms = new memorystream(buffer); image img = image.fromstream(ms); bitmap bmp = new bitmap(img); this.height = bmp.height; this.width = bmp.width; this.backgroundimage = bmp; } }
i can't think of parts disturbs these actions. in first iteration of server application, works (although see black screen instead of screenshot - number of bytes sent , received- matches.) @ sec iteration when writeline num, shows jibberish , stops (because cannot parsed int).
essentially suspect have sync problem had, when attempted reproduce problem (successfully).
be header
or single integer
announces upcomming bitmap
transmission. it's terribly easy out of sync.
unless want dive network synchronization , flow command recommend utilize single transmissionobject
every transmission between server , client (and maybe vice versa)
that object has potential store data has transmitted. (via inheritance)
finally when putting object on network stream may utilize consistent serializer
the serializer
work of syncing info needed (meta info length , content aswell)
well in repro project came this class hold bitmap.
as illustration how utilize on server:
public void send(bitmap bmp) { // prepare bitmap bitmaptransmission bt = new bitmaptransmission(bmp); // seek transmitting object seek { // lock exclude other threads of using stream lock (m_objsendlock) { bt.send(m_stream); } } grab (bitmaptransmissionexception ex) { // grab exception thrown in bt.send(...) debug.print("bitmapheaderexception: " + ex.message); ///bitmaptransmissionexception provides property `closeconnection` /// inform whether exception recoverable } }
where m_objsendlock
object prevend concurrent access on network stream. m_stream
networkstream connected client. echo
logging mechanism.
the client may receive such transmissions via
public void receive() { bitmaptransmission bt = bt.receive(m_stream); // check exceptions maybe? picturebox1.image = bt.bitmap; }
where 1 time again m_stream
networkstream connected server.
if want have @ (messy working) repro project tell so.
c# image networking tcp client
No comments:
Post a Comment