|
TCP is like a phone call, UDP is like putting messages in a bottle and tossing them into the sea. If you need persistent connections and reliable communications you need to use TCP. If you do use UDP you have to appreciate that you just get data as it comes in, there is no connection or reliability in the data you receive. UDP is only suitable for a limited number of applications, or maybe if you're on a local LAN with an old-school token-ring network.
|
|
|
|
|
thank sir for this information and thank you for my support
now Iam understand about UDP
please Sir can be modification for my code by use TCP
or can be put small example about connected IP and Recived data and send data by used TCP
Best regards
|
|
|
|
|
Hi,
This code from my friend about how can check the connection
if connected or not also continuous check
public bool TestConnection(string ip)
{
Ping p = new Ping();
try
{
PingReply reply = p.Send(ip, 3000);
if (reply.Status == IPStatus.Success)
return true;
}
catch (Exception e)
{
return false;
}
return false;
for example
private void button2_Click(object sender, EventArgs e)
{
busy = true;
if (TestConnection(textBox2.Text))
{
Byte[] sendBytes = Encoding.ASCII.GetBytes("B");
udpClient.Send(sendBytes, sendBytes.Length);
var receivedData = udpClient.Receive(ref ep);
textBox1.Text = Encoding.ASCII.GetString(receivedData);
label1.Text = Encoding.ASCII.GetString(receivedData);
}
busy = false;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (!busy)
{
Byte[] sendBytes = Encoding.ASCII.GetBytes("C");
udpClient.Send(sendBytes, sendBytes.Length);
var receivedData = udpClient.Receive(ref ep);
textBox1.Text = Encoding.ASCII.GetString(receivedData);
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Thread.Sleep(500);
if (TestConnection(textBox2.Text))
{
backgroundWorker1.RunWorkerAsync();
}
else
{
MessageBox.Show("No Device Connected");
}
}
|
|
|
|
|
Hello all, I have a quick question. I have a service running that continually checks the database for a record, if a record exist then it process the record and deletes it. The service starts a thread that runs a while look until it tells it to stop. Is it better to create the connection inside or outside of the loop? Keep in mind that this loop could run for days. See below for the two options:
while (!stopEvent.WaitOne(0, false))
{
using (SqlConnection dbConnection = new SqlConnection(connectionString))
{
dbConnection.Open();
using (SqlCommand dbCommand = new SqlCommand("SELECT * FROM [TableName]", dbConnection))
{
using (SqlDataReader dbReader = dbCommand.ExecuteReader())
{
if (dbReader.Read())
{
}
}
}
}
System.Threading.Thread.Sleep(100);
}
using (SqlConnection dbConnection = new SqlConnection(connectionString))
{
dbConnection.Open();
while (!stopEvent.WaitOne(0, false))
{
using (SqlCommand dbCommand = new SqlCommand("SELECT * FROM [TableName]", dbConnection))
{
using (SqlDataReader dbReader = dbCommand.ExecuteReader())
{
if (dbReader.Read())
{
}
}
}
}
System.Threading.Thread.Sleep(100);
}
|
|
|
|
|
I'd go for option 1 - create and dispose of the connection within the loop. You don't want to hold a connection open when you're not using it, and the Thread.Sleep call isn't using it.
I'd also suggest adding a TOP 1 to your query, since you're only expecting to read and process a single record. Either that, or change the if (dbReader.Read()) { ... } to while (dbReader.Read()) { ... } to read and process all of the returned rows.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Two reasons why you should be creating the connection when you need it and not before the loop.
1) If the connection is broken by something, like a drop in network connectivity, you can recover by creating a new connection inside the loop every time you need it without having to break out of the loop to create a new one.
2) A SQL Server connection seat license ain't cheap. If you had one license to share between multiple applications and users, you'd want your connection to hang on to it for as little time as possible.
Connect as late as you can, run your query, and disconnect as early as you can.
|
|
|
|
|
As already mentioned, creating the connection when you need it is best. ado.net implements connection pooling for you under the covers so although you are opening and closing the connection, in normal operation you're not actually doing anything...when you close in your code ado.net keeps the connection open and adds it to the pool, when you open it is just retrieved from the pool so you're not consuming any resources or creating overhead by repeatedly opening and closing. However it means that when your server is very busy then it allows other processes to use the connection if needed and you might get a different connection, or again as mentioned if the connection breaks for some reason you'll just get a new one.
So, basically, don't worry about rapidly opening and closing connections in terms of resources or overhead, ado.net's connection pooling is helping you out.
|
|
|
|
|
Hello. I'm trying to add image slices (about 6 pixels across) to a pre-created bitmap. The idea is to stitch a large bitmap as images come in from a camera. For the purposes of this example, i've changed to code from a newframe event to a for loop. See below. It works well, except i'm occasionally getting a parameter not valid error when creating the blank bitmap, especially if I have created it one or twice before. I've looked around a bit on the google and the best I can figure is that i'm running out of contiguous memory, which is required to create bitmaps. If that's the problem, then I don't have a solutions. Any help would be greatly appreciated!!!!
<pre>private List<Bitmap> BMPList = new List<Bitmap>();
private Bitmap stitchImage = null;
private Graphics stitchGraphics = null;
private void InitializeStitch()
{
stitchImage = new Bitmap(100000, BMPList[0].Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
stitchGraphics = Graphics.FromImage(stitchImage);
}
private void UninitializeStitch()
{
if (stitchGraphics != null)
stitchGraphics.Dispose();
if (stitchImage != null)
stitchImage.Dispose();
}
private void CreateStitch()
{
if (BMPList.Count > 0)
{
InitializeStitch();
for (int i = 0; i < BMPList.Count; i++)
stitchGraphics.DrawImage(BMPList[i], i * BMPList[i].Width, 0);
using (Bitmap tempBMP = (Bitmap)StitchImage.stitchBMP.Clone(new Rectangle(0, 0, BMPList[0].Width, BMPList[0].Width.Height), StitchImage.stitchBMP.PixelFormat))
{
tempBMP.Save(@"C:\MyTestImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
Uninitialize();
}
}
|
|
|
|
|
How many items are in this BMPList array? How big are the images in it? How big does this "large image"?
Bitmap objects cannot be infinitely large. Any managed object can only be a maximum of 2GB in size. I get the feeling that you're either filling up memory holding all of these images at once and/or you're making a Bitmap object that's just too large.
|
|
|
|
|
Hello Dave,
Thanks for the reply.
In the example I listed, the bitmap is 100000x640. This will create fine, once, but it's only after I dispose it and then recreate (sometimes it even works several times) that I get the problem. So, I feel like the issue is with continuous memory.
I have seen applications work with much larger bitmaps then this, I just don't know how they do it.
|
|
|
|
|
You say you can make it once; save it to disc, just to see how large one such bitmap is.
It will be very large.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
JBHowl wrote: the bitmap is 100000x640.
You do realize that's 256MB of RAM, right? Do 4 of those combined with a bunch of other images (I'm assuming 640 of them) and it's no wonder why memory is so badly fragmented. In a 32-bit app, that's a LOT of memory. You might want to come up with another scheme to do this that doesn't involve loading 640 images into memory all at once.
|
|
|
|
|
Which line is throwing the exception?
|
|
|
|
|
stitchImage = new Bitmap(100000, BMPList[0].Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
|
|
|
|
|
Does it have to be done in WinForms? If you were to use WPF, the bitmap support in there doesn't use GDI+ and it's a lot more responsive.
|
|
|
|
|
It may be that it's the .NET version that is causing the problem: prior to V4.5 the Large Object Heap was not compacted, so even disposing of your objects may not free up the memory correctly.
If you can, it's worth a try at 4.5 or 4.5.1 / 4.5.2 and see if the problem goes away.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Interesting, i'll give that a try.
|
|
|
|
|
Hi,
I'm going to implement WebRTC using WebBrowser Control in C# Windows form application.
I attempted with EO.WebBrowser and CefSharp library, but I can't connect my webcam in their WebBrowser control.
Have you any suggestion?
|
|
|
|
|
if you have a tenvis camera then it comes with a program (Active-X control) to work in IE
VS Browser control is IE so it will work
Tenvis lets you opt in to viewing your camera from across the internet but be warned that even if you turn the option off it still kicks out across the internet and by-passes your DNS server so best to just block it in the routers firewall.
|
|
|
|
|
I need to copy an audio file from resources to the HD:
private void EnsureDefaultFileExists()
{
var outputFile = Path.Combine(FileSystemLocations.GetTempFilesDirectory(), "fc6sound.wav");
if (!File.Exists(outputFile))
{
var targetAssembly = Assembly.GetExecutingAssembly();
var resourceName = "fc6sound.wav";
using (Stream stream = targetAssembly.GetManifestResourceStream(targetAssembly.GetName().Name + "." + resourceName))
{
if (stream == null)
{
throw new Exception("Cannot find embedded resource '" + resourceName + "'");
}
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
using (BinaryWriter sw = new BinaryWriter(File.Open(outputFile, FileMode.Create)))
{
sw.Write(buffer);
}
}
}
}
This isn't working. The exception is thrown. I've tried it with and without the ".wav". Anyone see what's wrong?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
The resource name is case-sensitive and has to be fully qualified, that is, if you've put the resource into a sub-directory of the project, you have to include the name of the directory as quasi-namespace. E.g.: "MyAssembly.MyResourcesDir.fc6sound.wav"
Or you can enumerate all embedded resources with Assembly.GetManifestResourceNames() and simply copy the resource name from there.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I tried using Assembly.GetManifestResourceNames() and I got back an array of 33 resources:
var resources = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
using (var sw = new StreamWriter(@"C:\someplace\resources.txt", true))
{
foreach (var resource in resources)
{
sw.WriteLine(resource.ToString());
}
}
I get this:
FALCON6.g.resources
FALCON6.DownloadDialog.resources
FALCON6.ucAdvancedSettingsConfig.resources
FALCON6.ucConfigureBluetoothSensor.resources
FALCON6.ucBluetoothPairing.resources
FALCON6.ucBluetoothPairingItem.resources
FALCON6.ucCOPSoundSelector.resources
FALCON6.ucConfigureFalcon6Sensor.resources
FALCON6.ucConfigureWiredSensor.resources
FALCON6.ucCOPIconSelector.resources
FALCON6.ucCOPSettings.resources
FALCON6.ucFC6ActivityListContainer.resources
FALCON6.ucFC6Sensor.resources
FALCON6.FALCON6MainForm.resources
FALCON6.Properties.Resources.resources
FALCON6.ucCameras.resources
FALCON6.ucConnectMain.resources
FALCON6.ucConnect_Prof.resources
FALCON6.ucConnect_Profs.resources
FALCON6.ucFC6Activity.resources
FALCON6.ucFC6SensorPictureBox.resources
FALCON6.ucFC6Timeline.resources
FALCON6.ucGSMConfig.resources
FALCON6.ucImageResizer.resources
FALCON6.ucImageViewer.resources
FALCON6.ucIPAddressConfig.resources
FALCON6.ucLiveView.resources
FALCON6.ucRow.resources
FALCON6.ucTable.resources
FALCON6.ucMapCompositeExtended.resources
FALCON6.ucUploadSettings.resources
FALCON6.ucVideo.resources
FALCON6.ucVideoPlayer.resources
I have no clue which of these is right, and I have no idea how to use it.
Sascha Lefévre wrote: and simply copy the resource name from there.
.. I don't understand.
If it's not broken, fix it until it is
|
|
|
|
|
Hm.. did you actually select "Embedded Resource" as the Build Action for your wav-file?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
1 - I double click the "resources.resx" node in the Solution Explorer. The resources view opens.
2 - I select "Audio" from the type dropdown at the top
3 - I right click the audio file. There is no option to embed.
4 - I bring up properties and drop down the "Persistence" option. I selected "Embedded in .resx" option.
I don't see any "Build Action"
If it's not broken, fix it until it is
|
|
|
|
|
OK, I got it... I added the file to the SOLUTION, not the RESOURCES node, then set it to embedded, and it's resource name is "FALCON6.fc6sound.wav"..
With that, my code above now works.
If it's not broken, fix it until it is
|
|
|
|