i have written a little "Update Programm" to keep an .exe up to date for the rest of my dev team. It used to work fine, but suddenly it stopped working.
I already noticed the problem: my remote stream does not start to read.
Uri patch = new Uri("http://********/*********/" + GetVersion().ToString() + ".exe");
Int64 patchsize = PatchSize(patch);
var CurrentPath = String.Format("{0}\\", Environment.CurrentDirectory);
Int64 IntSizeTotal = 0;
Int64 IntRunning = 0;
string strNextPatch = (version + ".exe");
using (System.Net.WebClient client = new System.Net.WebClient())
{
using (System.IO.Stream streamRemote = client.OpenRead(patch))
{
using (System.IO.Stream streamLocal = new FileStream(CurrentPath + strNextPatch, FileMode.Create, FileAccess.Write, FileShare.None))
{
int intByteSize = 0;
byte[] byteBuffer = new Byte[IntSizeTotal];
while ((intByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
streamLocal.Write(byteBuffer, 0, intByteSize);
IntRunning += intByteSize;
double dIndex = (double)(IntRunning);
double dTotal = (double)byteBuffer.Length;
double dProgressPercentage = (dIndex / dTotal);
int intProgressPercentage = (int)(dProgressPercentage * 100);
worker.ReportProgress(intProgressPercentage);
}
streamLocal.Close();
}
streamRemote.Close();
GetVersion() only returns the current version number of the current server version of the .exe.
The problem lies here:
while ((intByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
My streamRemote just does not return any bytes so this while clause is not filled.
Any advice for me?
I believe the problem is on the server. I'd run some checks:
Has anything changed on the configuration of the web server that stops you from downloading executables?
Are you connecting through a proxy?
Can you manually get to the same URL (under the same user credentials of your application)?
Related
I have created a simple client to send HL7 formatted messages to a server. Unfortunately I do not have access to the server to see how the messages are coming in and what is being sent back. Right now my message sends but when I wait for a message back (ACK), it just sits there waiting.
Any advice is greatly appreciated.
try {
using (var client = new TcpClient())
{
client.Connect("10.25.60.8", 10000);
using (Stream stm = client.GetStream())
{
byte[] ba = Encoding.ASCII.GetBytes(segment1 + segment2 + segment3 + segment4 + segment5);
Console.WriteLine("Transmitting.....");
stm.Write(ba, 0, ba.Length);
Console.WriteLine("MessageSend reading response");
byte[] bb = new byte[1000];
int k = stm.Read(bb, 0, 1000);
for (int i = 0; i < k; i++)
Console.WriteLine(Convert.ToChar(bb[i]));
client.Close();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
This is how I create the message segments:
clnt p = new clnt();
patientAdmit r = new patientAdmit();
r.admitMessage = "MSH|^~\\&|MP|M1|MP|M2|201701011500||ADT^A01|HL7MSG00001|P|2.3|EVN|A01|201701011500||PID|||MRN222222||TEST^MICHAEL||19890101|M||C|1 MP STREET^^MARK^ON^L4C|GL|(416)123-1234|(647)123-1234|||||||NK1|1|TEST^BARBARA|WIFE||||||NK^NEXT OF KIN|PV1|1|I|20^201^01||||123456^TEST^DOC|||SUR||||ADM|A0|";
string vt = Convert.ToChar(11).ToString();
string cr = Convert.ToChar(13).ToString();
string fs = Convert.ToChar(28).ToString();
int startSegment2 = p.GetNthIndex(r.admitMessage, Convert.ToChar("|"), 12) + 1;
int startSegment3 = p.GetNthIndex(r.admitMessage, Convert.ToChar("|"), 16) + 1;
int startSegment4 = p.GetNthIndex(r.admitMessage, Convert.ToChar("|"), 16 + 21) + 1;
int startSegment5 = p.GetNthIndex(r.admitMessage, Convert.ToChar("|"), 16 + 21 + 10) + 1;
string segment1 = vt + r.admitMessage.Substring(0, startSegment2);
string segment2 = cr + r.admitMessage.Substring(startSegment2, startSegment3 - startSegment2);
string segment3 = cr + r.admitMessage.Substring(startSegment3, startSegment4 - startSegment3);
string segment4 = cr + r.admitMessage.Substring(startSegment4, startSegment5 - startSegment4);
string segment5 = cr + r.admitMessage.Substring(startSegment5, r.admitMessage.Length - startSegment5) + cr + fs;
You can simulate a server with most HL7 Editors. For example, grab the free trial of HL7 soup from their website, and create a new receiver on your desired port.
When you send in your messages they will respond straight back with an ACK message generated from your message.
In the reciever settings, you can even force the response to be an error or reject ACK so you can test your client.
I am re-developing an app for a scanner used for stocktakes to allow it to work while offline. In order to do so, I need to be able to download a file from a laptop which is acting as a server. I got to a point at which it works, but only downloads that are of size 9.53mb max. How can I tweak the code to allow for larger files. I would need to allow for a maximum size of around 30mb.
Here is my code:
try
{
string full_url = App.prouductUrl + App.stStocktakeId + ".db";
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(full_url);
httpRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
System.IO.Stream dataStream = httpResponse.GetResponseStream();
// Dim str As Stream = cdsMobileLibrary2.http_download.getfile(filename)
//50 meg
byte[] inBuf = new byte[10000001];
int bytesToRead = Convert.ToInt32(inBuf.Length);
int bytesRead = 0;
while (bytesToRead > 0)
{
int n = dataStream.Read(inBuf, bytesRead, bytesToRead);
if (n == 0)
{
break; // TODO: might not be correct. Was : Exit While
}
bytesRead += n;
bytesToRead -= n;
}
FileStream fstr = new FileStream(#"\productdb\" + App.stStocktakeId + ".db", FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(inBuf, 0, bytesRead);
dataStream.Close();
fstr.Close();
string size = loginRes.getFromJSON("size");
FileInfo fi = new FileInfo(#"\productdb\" + App.stStocktakeId + ".db");
MessageBox.Show("File Size is:" + fi.Length + "Compared to:" + size);
}
catch { }
I have auto-upload application from ftp server and two progressbar's to update overall and current download state.
First one works fine (update% = currentFile as int / allFilesToDownload *100%). But i'd like to upload current file downloading.
My code:
Uri url = new Uri(sUrlToDnldFile);
int inde = files.ToList().IndexOf(file);
string subPath = ...
bool IsExists = System.IO.Directory.Exists(subPath);
if (!IsExists)
System.IO.Directory.CreateDirectory(subPath);
sFileSavePath = ...
System.Net.FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(file));
System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)request.GetResponse();
response.Close();
long iSize = response.ContentLength;
long iRunningByteTotal = 0;
WebClient client = new WebClient();
Stream strRemote = client.OpenRead(url);
FileStream strLocal = new FileStream(sFileSavePath, FileMode.Create, FileAccess.Write, FileShare.None);
int iByteSize = 0;
byte[] byteBuffer = new byte[1024];
while ((iByteSize = strRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
strLocal.Write(byteBuffer, 0, iByteSize);
iRunningByteTotal += iByteSize;
//THERE I'D LIKE TO UPLOAD CURRENT FILE DOWNLOAD STATUS
string a = iByteSize.ToString();
double b = double.Parse(a.ToString()) / 100;
string[] c = b.ToString().Split(',');
int d = int.Parse(c[0].ToString());
update(d);
//update(int prog) { bgWorker2.ReportProgress(prog); }
}
double dIndex = (double)(iRunningByteTotal);
double dTotal = (double)iSize;
// THIS CODE COUNTING OVERAL PROGRESS - WORKS FINE
double iProgressPercentage1 = double.Parse(ind.ToString()) / double.Parse(files.Count().ToString()) * 100;
ind++;
string[] tab = iProgressPercentage1.ToString().Split(',');
int iProgressPercentage = int.Parse(tab[0]);
currentFile = file;
bgWorker1.ReportProgress(iProgressPercentage);
strRemote.Close();
Unfortunately i still getting error, that I cant update progressBar2, becouse another process using it.
Is there any way to do it?
Thanks
update values thru dispatcher.BeginInvoke Methods something like.
Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(()=>
{
progressbar2.value = newvalue;
}));
This Dispatcher Will push you work to the main thread which is holding the Progressbar2.
First,I uploaded a file in a external server and got a url from that server.Now i want to download that uploaded file from the external server through that url which i got from that server through asp.net and c#.
I wrote a c# code to download that file, but during that process i got an exception
"Exception of type 'System.OutOfMemoryException' was thrown".
The following is the c# code which i wrote to download:
double dlsize=0;
string Url = "http://www.sample.com/download.zip"; \\File Size: 75MB
int lastindex = Url.LastIndexOf("/");
string TempUrlName = Url.Substring(lastindex + 1, Url.Length - (lastindex + 1));
WebRequest oWebRequest = WebRequest.Create(Url);
oWebRequest.Timeout = -1;
WebResponse oWebResponse = oWebRequest.GetResponse();
if (oWebResponse.ContentType != "text/html; charset=UTF-8")
{
string myFile = oWebResponse.Headers.Get("Content-Length");
int TempmyFile = Convert.ToInt32(myFile);
double bytes_total = Convert.ToDouble(TempmyFile);
dlSize = Convert.ToDouble(bytes_total / (1024 * 1024));
System.IO.MemoryStream oMemoryStream = new MemoryStream();
byte[] buffer = new byte[4096];
using (System.IO.Stream oStream = oWebResponse.GetResponseStream())
{
int Count = 0;
do
{
Count = oStream.Read(buffer, 0, buffer.Length);
oMemoryStream.Write(buffer, 0, Count);
} while (Count != 0);
}
System.IO.FileStream oFileStream = new System.IO.FileStream("C:\Documents and Settings\arun\Desktop\New Folder\download.zip", System.IO.FileMode.Create);
oMemoryStream.WriteTo(oFileStream);
oFileStream.Flush();
oFileStream.Close();
oMemoryStream.Flush();
oMemoryStream.Close();
oWebResponse.Close();
}
It would be easier to do it like this:
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteFileUrl, localFileName);
im doing a application in which i split a wmv file and transfer it to otherlocation(in 'x' kbs) .after the transfer gets completed the file doesnt play,it gives a message as the format is not supported.is there anyother way to do it.
sory i will explain what im doing now
i wrote an remote application,i want to transfer a .wmv file from one machine to other,i want to split the .wmv and send it to the remote machine and use it there.if i try to send the complete file means it will take lot of memory that seems very bad.so i want to split it and send it.but the file doesnt gets played it raises an exception the format is not supported.
the following is the code im doing i just done it in the local machine itself(not remoting):
try
{
FileStream fswrite = new FileStream("D:\\Movie.wmv", FileMode.Create);
int pointer = 1;
int bufferlength = 12488;
int RemainingLen = 0;
int AppLen = 0;
FileStream fst = new FileStream("E:\\Movie.wmv", FileMode.Open);
int TotalLen = (int)fst.Length;
fst.Close();
while (pointer != 0)
{
byte[] svid = new byte[bufferlength];
using (FileStream fst1 = new FileStream("E:\\Movie.wmv", FileMode.Open))
{
pointer = fst1.Read(svid, AppLen, bufferlength);
fst1.Close();
}
fswrite.Write(svid, 0, pointer);
AppLen += bufferlength;
RemainingLen = TotalLen-AppLen;
if(RemainingLen < bufferlength)
{
byte[] svid1 = new byte[RemainingLen];
using (FileStream fst2 = new FileStream("E:\\Movie.wmv", FileMode.Open))
{
pointer = fst2.Read(svid1, 0, RemainingLen);
fst2.Close();
}
fswrite.Write(svid, 0, pointer);
break;
}
}
fswrite.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
You'll probably find Good way to send a large file over a network in C# helpful.
Im going to make the assumtion your spliting the file when your sending it, and not trying to have the wmv in 3 different files on the remote machine.
When your sending the file what you basicly do is this:
Local machine
1) Read 16k bytes ( Or whatever number you prefere )
2) Send those 16k bytes over the network
3) Repeat above steps untill done
Remote machine
1) Listen for a connection
2) Get 16k bytes
3) Write 16k bytes
4) Repeat untill done.
This method will work, but your kind of inventing the wheel again, i would recommend using either something as simple as File.Copy ( Works fine over the network ) or if that does not meet your needs perhaps using a FTP client / server solution ( Plenty of C# examples on the net that can be hosted inside your application ).
i tried this
private void Splitinthread()
{
int bufferlength = 2048;
int pointer = 1;
int offset = 0;
int length = 0;
byte[] buff = new byte[2048];
FileStream fstwrite = new FileStream("D:\\TEST.wmv", FileMode.Create);
FileStream fst2 = new FileStream("E:\\karthi.wmv", FileMode.Open);
int Tot_Len = (int)fst2.Length;
int Remain_Buff = 0;
//Stream fst = File.OpenRead("E:\\karth.wmv");
while (pointer != 0)
{
try
{
fst2.Read(buff, 0, bufferlength);
fstwrite.Write(buff, 0, bufferlength);
offset += bufferlength;
Remain_Buff = Tot_Len - offset;
Fileprogress.Value = CalculateProgress(offset, Tot_Len);
if (Remain_Buff < bufferlength)
{
byte[] buff1 = new byte[Remain_Buff];
pointer = fst2.Read(buff1, 0, Remain_Buff);
fstwrite.Write(buff1, 0, pointer);
Fileprogress.Value = CalculateProgress(offset, Tot_Len);
fstwrite.Close();
fst2.Close();
break;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
MessageBox.Show("Completed");
}