c# - Reading text file from Windows and writing on a Linux Server in C -
i working on client-server application. client project designed in vs 2012 c#, , server side coding done in c. basic aim client app read file , send server , server app write on server. text file sent contains data
ls.db
abs.tst=8745566 xyz.xys=2239482 kpy.llk=0987789
but when written on server written
abs.tst=8745566xyz.xys=2239482kpy.llk=0987789
the application reading number of sent bytes. there bytes missing @ end when file written. server size file has 6bytes missing when checked properties of client file , server file have given client , server code below kindly guide me how can solve issue
client code in c#
string[] lines = system.io.file.readalllines("local.db"); var binwriter = new system.io.binarywriter(system.io.file.openwrite("l2.db")); foreach (string line1 in lines) { sslstream.write(encoding.ascii.getbytes(line1.tostring())); } sslstream.write(encoding.ascii.getbytes("eof"));
linux based server code in c
file * file = fopen("local2.db","w+"); memset(buff,0,sizeof(buff)); if(file!=null){ num = ssl_read(ssl,buff,sizeof(buff)); while(num>0){ if(strcmp(buff,"eof")==0){ fclose(file); break; }else{ fwrite(buff,1,num,file); memset(buff,0,sizeof(buff)); num = ssl_read(ssl,buff,sizeof(buff)); } } }
sslstream.write()
not insert line break (\n
or \r\n
) characters. file.readalllines()
does not include line-end characters either.
so writing 1 long sequence of bytes without separating line terminators. should easy enough fix:
string[] lines = system.io.file.readalllines("local.db"); var binwriter = new system.io.binarywriter(system.io.file.openwrite("l2.db")); foreach (string line1 in lines) { // write line unix-style end-of-line character sslstream.write(encoding.ascii.getbytes(line1.tostring() + "\n")); } sslstream.write(encoding.ascii.getbytes("eof"));
if want exact copy of file, don't use readalllines()
. use readallbytes()
:
byte[] filedata = system.io.file.readallbytes("local.db"); sslstream.write(filedata);
Comments
Post a Comment