c# 4.0 - Reading Data from Network Stream -


i have simple tcp client , listener code. want pass filename along file data. there way send filename along file data , reading filename alone network stream without corrupting file data?

here way of sending both filename , data shouldn't corrupted. if use serialization don't have manually write , read file name , contents separately can send , read single object , have framework deal serializing , deserializing individual properties.

[serializableattribute] class data {     public string filename;     public byte[] filedata; } 

client

string filepath = @"c:\temp\myfile.txt"; tcpclient client = new tcpclient(); client.connect("localhost", 8888);  data data = new data(); data.filename = path.getfilename(filepath); data.filedata = file.readallbytes(filepath);  binaryformatter formatter = new binaryformatter(); formatter.serialize( client.getstream(), data); client.close(); 

server

tcplistener listener = new tcplistener(ipaddress.loopback, 8888); listener.start(); tcpclient client = listener.accepttcpclient();  binaryformatter formatter = new binaryformatter(); data data = (data)formatter.deserialize(client.getstream()); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -