|
-
April 28th, 2011, 03:39 PM
#1
C# Small program in daemon mode?
Hi,
My situation:
I want to send some information to a webserver.
I have a program that generating the information and I have a webserver wich can collect this information and put it into a database.
At the moment I use a small (self-created) C# program where the information will we added as params. The command that I currently use is for example;
C:\post_to_web.exe "information string 1" "information string 2"
The problem I currently have is that sometimes the information goes very quick after each other. That cause problems on the database, where the messages must be connected to each other.
I was thinking about creating a program that runs in the background where another program is connecting to. At this way every message will be send after each other, what's solving my problem.
How can I do this? Or do you have a better idea?
The code of my currently program is;
Code:
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;
namespace Post
{
class Program
{
static void Main(string[] args)
{
string post_url = "http://www.mywebsite.nl/input/";
if (args.Count() == 2)
{
try
{
string data1 = args[0];
string data2 = args[1];
Console.WriteLine("DATA1: " + data1);
Console.WriteLine("DATA2: " + data2);
Console.WriteLine("Start request to: " + post_url);
byte[] buffer = Encoding.ASCII.GetBytes("data1="+data1+"&data2="+data2);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(post_url);
WebReq.Method = "POST";
WebReq.UserAgent = "Poster";
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine("Status: " + WebResp.StatusCode);
Console.WriteLine("Connected with: " + WebResp.Server);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}
}
else
{
Console.WriteLine("Fout, ongeldig aantal paramaters opgegeven ("+args.Count()+"/2)");
}
}
}
}
-
May 1st, 2011, 02:04 PM
#2
Re: C# Small program in daemon mode?
I see you are writing out the status code but you are not giving valid arguments lets say...
Lets just say for that the value of 9000 is good status code
GET STATUS CODE:
if status code == 9000 goto finish: ;
else go to Bad: ;
BAD:
Code here handling bad response attempt to repost possibly;
finish:
Check for next string of data
if there are no more strings to process go to done
else process next string
Essentially to handle your errors you need to create a handler there may be an easier way to do it but I am no coding genius.
In laymens terms what you need to do is:
Set the number of strings to process.
Let first string in
process it with a counter
verify that it was processed
if good process next string:
else it was not processed reprocess it however many times you specify
if not able to be reprocessed
give an error status message and exit
process next string:
check the number of strings left to process
if the number is zero goto finished:
if the number is one go to process string
I dunno if this makes any since to you as I said I am no guru so take this all with a grain of salt I am rather sure there are easier ways to do things then I know how to do them.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|