|
There is indeed a lot of data now in the downloaded file but its still all in numbers (It dosent look like binary too me but more like decimal without the hyphens) what am I missing here I'm assuming that the browser would know how to handle this data and reproduce a valid file from it (perhaps I'm wrong)?
also to answer the type of variable for My404HtmlPage is
Dim My404HtmlPage As New System.Text.StringBuilder()
which then is passed on to a variable named MyHtmlServerResponse as follows
MyHtmlServerResponse = (My404HtmlPage.ToString)
MyHtmlServerResponse is a string variable as can be seen below
Dim MyHtmlServerResponse As String
EDIT:
Just as extra Info the actual sending is done through TCP/ IP as follows
For Each c As TcpClient In MyClientList
If c Is MyClient Then
Dim nns As NetworkStream = c.GetStream()
nns.Write(Encoding.ASCII.GetBytes(MyHtmlServerResponse), 0, MyHtmlServerResponse.Length)
MyClient.Dispose()
End If
modified 3-Apr-23 12:01pm.
|
|
|
|
|
It does not matter what the content of the file looks like. If you do not understand how to handle binary data without corrupting it then you will never pass go or collect your $200. So stop trying to convdert the content into printable/displayable strings, that just destroys it (as I said in my previous message). You must read and write the content as pure binary bytes, nothing else.
|
|
|
|
|
Bart van Tuijl wrote: also to answer the type of variable for My404HtmlPage is
Dim My404HtmlPage As New System.Text.StringBuilder()
Well, there's your problem. You cannot treat binary data as a string!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
This My404HtmlPage being a string I had suspected, though Im at a dilemma because my http header information is sent as string and my file as bytes yet they belong to eachother.
Is there a way I can convert my HTTP header to bytes and add my file to it as bytes and then send it or will that not work?
(Sorry guys Im just a hobbyist so my knowledge is limited)
|
|
|
|
|
Yes as I suggested in my other post.
You need to learn what hexadecimal is ('hex').
You need to get a hexadecimal editor (it will be called a hex editor.)
You need to compile a SMALL project and then look at the exe (which is a binary file) using the hex editor. Not notepad.
You need to then look at an image using a hex editor.
You need to then find the specification ('spec') for that type of image and use the hex editor (not notepad) to figure out the parts of the image.
I would also suggest that you write a program parse an image file. NOT display it. Just read it and print out the parts.
1. Read the file with a stream
2. Use the spec to figure out how to read the file.
3. Print out the various pieces. For example there is likely a size (x and y) so print that out as integers.
|
|
|
|
|
Bart van Tuijl wrote: I'm in the process of building a HTML server using vb.ne
Reading through this and your other posts.
You need to come to a full stop on your code and first learn how http and the browser works.
And additionally what a 'binary' file is.
------------------------------------------
Binary file - if is an executable and you open it in notepad will always look like you found. Does not matter where it came from.
Notepad takes data and then transforms it in to characters.
That can only work if the data is in fact characters.
A binary, especially an executable will never consist solely of characters. So of course notepad will never display it because it cannot. Notepad displays a '?' for data that is not actually characters.
Some (most) executables will have data in them that you will be able to read. Doesn't mean the rest is wrong just that an executable has text in it.
And all of that depends on what character set notepad expects to see (I suspect you might want to research that also.)
You can experiment with this by using notepad to open various files on your computer. I suggest
1. Executable
2. Image file
3. Write your own program, put text in it (a string), compile it, then open that.
Do NOT save anything in Notepad to those above files. Unless you want to destroy them (try it if you wish but only if you expect to lose it.)
If you really want to modify a binary file manually then you need to find a 'hex editor'.
If you want to do that then you will need to learn what 'hex' means.
------------------------------------------
A browser sends a request to a web server and the web server then responds to that request.
You should learn about the HTTP protocol.
Your browser will have a 'developer tool' which allows you to inspect both requests and responses.
The HTTP protocol itself does not support file downloads in anyway.
A specific request might result in some content which originates from files being downloaded.
1. A html response (from a request) might have tags in it which the browser which will then use to make another request to the web server, and the response body with have data which is an image. The request defines this not the HTTP protocol.
Browsers used to support a 'ftp' protocol that allowed files to be downloaded. Rather than 'http' it started with 'ftp'
However apparently newer versions (Chrome, Firefox) no longer support that at all. So you would need to get an older version if you wanted to implement that protocol.
|
|
|
|
|
So, I've done mainly VBc and C/C++ for embedded device, and a tiny bit of VC++. When .Net came along, after a while, it was often repeated that .Net allowed people to write bad code, but will still work. I was a stickler for following good programming form.
On one occasion, someone had ask a question about what the .Net compiler will do with [whatever code it was], and someone else chimed in saying something else like 'the compiler is smart and will optimize...'.
But, you know, I'm still a stickler for proper form, allocating vars and objects, properly destroying them, stopping any timers before program exit, etc. I guess, proper "cleanup" you could say.
So I'm working with some code the company hired a supposedly experienced programmer to write a program for the company that monitors devices it makes. Some of this is so appalling, that it looks like an absolute beginner would write. So the below chunk of code is one function he had written. He has this running in it's own thread, and it's a UPD listening socket. This is used to prevent multiple instances of it running, and to return from the system tray after it's been minimized to the tray. So this function is always running in it's own thread...
Private Sub udpThreadsub()
While True
Try
udpcl = New UdpClient(UDPPORT)
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
While True
Try
Dim rec = udpcl.Receive(RemoteIpEndPoint)
If rec.Length = 11 Then
Dim match = True
For i = 0 To 10
If rec(i) <> UDP_MSG(i) Then
match = False
Exit For
End If
Next
If match Then
Outoftray()
End If
End If
Catch e As Exception
End Try
End While
Catch e As Exception
End Try
End While
End Sub
To me, this is some of the ugliest code I've ever seen. I mean, the compiler isn't THAT smart is it?
To me, this looks like the dim'd object will be created and sent to GC every iteration through the loop! But then again, I don't have an intimate knowledge of what the compiler will do with this. Will it just realize that it should just reusing these objects and not instantiate them and send GC as it appears they would?
I would have done the DIM's above the loops, and then reuse them in the code.
So is this some of the worse code ever seen (by me anyway) or is the compiler just 'that smart'?
Thanks in advance.
|
|
|
|
|
Ignoring the fact that this is VB.NET ...
The outer loop only runs if an exception is thrown (and swallowed) outside of the inner loop's exception-swallowing Try..Catch block. Realistically, the only things that could trigger that would be the creation of the UdpClient , or the creation of the IPEndPoint . But that would mean that either the port was inaccessible, or the application had run out of memory. In which case, the next iteration would almost certainly throw the same exception again, leading to a thread constantly executing a tight try-throw-catch-try-again loop.
The receive call will allocate a new byte array. But there's no option to avoid that, and no way to make it reuse the same array. The only way that could be optimized would be if the BCL code did it for you. But that's unlikely, since it has no way of knowing what the caller will do with the returned array. So you'll have some GC churn on the inner loop.
If this is intended to implement a single instance application, there are far better ways to do that. For a start, in a VB.NET WinForms project, there's a specific option on the application properties to do this.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Without a (stopwatch) timing, the "GC collecting" theory is meaningless.
I'm amazed how fast C# can run "before" I've bothered with my optimizations; where I routinely create objects in called methods that run at many frames per second. I had the same concerns and found I had cycles to spare.
The GC "graph" just chugs along.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
I've seen uglier code than that, but there is still only so much the compiler can do with what it sees. Detection of "code intent" is what gives a compiler the ability to optimize, but it can only go so far. The compiler can optimize inside methods, but over large sections of an app or even a class? Not so much.
The compiler cannot optimize you out of bad design.
|
|
|
|
|
Member 10097972 wrote: VBc and C/C++ for embedded device, and a tiny bit of VC++.
Perhaps the rest of the comments are based on that background.
Certainly for me (decades ago) working on computers with much smaller memories one had to be much, much more careful not only with how memory was used but how and when it was allocated.
Computers have substantially more memory than almost all embedded devices. That was even true long ago. Both have gotten bigger since then.
Additionally for PC computers the OS and compilers/interpreters have been designed for decades to optimize the usage of that memory.
One need only read up on how memory heaps used to work (perhaps even how they are still taught) versus the complexity that goes into moderns heaps to see the changes. And even reading up on how the OS and even the CPUs are optimized to assist in that. Even physical memory and other hardware in computers has changed to facilitate that.
Member 10097972 wrote: So this function is always running in it's own thread...
Excluding the VB part of that...
Yep that is how you write it - in its own thread. Unclear to me why you seem to think that is a problem?
Member 10097972 wrote: This is used to prevent multiple instances of it running,
Not sure what that is supposed to mean. But any IP port should never have more than one reader. Not in any circumstance. So mentioning seems odd since that is exactly what must happen.
Member 10097972 wrote: the compiler isn't THAT smart is it?...I would have done the DIM's above the loops
I think this is your major concern.
And yes the compiler (and interpreter in this case) is that smart.
The code I see is getting a message, in a buffer, but still a message. So the API creates the buffer and not the code that you posted. You definitely could not have moved that outside the loop. (As mentioned compiler/interpreter will easily deal with this.)
So I don't see any other allocations in the loop?
|
|
|
|
|
I know this is a few months old, but I got a chuckle out of this! =)
Back in the day I moved from C++ to VB6, then to .NET and I've been there since.
Honestly I wasn't the most excited about the changes, and I probably went through all the states of acceptance getting to know VB. It's funny how things turned out, because even though I don't use it much at all any more, I actually have fond memories of it.
This code though, it's funny to look at. But I'm not sure I would call it horrible for the ways that most might in knee-jerk reaction.
In plain English, it's intended to do the same thing over and over again. Listen for a a message, check if it meets a criteria "good enough to try getting what it wants", if it met the first challenge, but ultimately didn't work, try again.
What I dislike about it is more the fact that it's not as explicit about exactly what it's looking for, and the fact that it's not even logging errors.
The fact that it's destroying and re-creating the client in this case might be wasteful at first glance, but it's clear that whatever it's looking for is just meant to wake something else up, and it probably works as intended...
For all we know if it received a malformed packet and they ignored it and tried again with the same instance of the client, the old "UNIX server in the back that nobody knows how to use any more" would catch fire and have to be hit with a hammer. =)
|
|
|
|
|
I haven't done a c# project in some time, and trying to remember all the things I've forgotten.
I have an application with various application & user settings. I want the user to be able to set those preferences. I figured out how to use a property grid to show settings on the appropriate form:
Properties.Settings mySettings = new Properties.Settings();
private void SettingsForm_Load(object sender, EventArgs e)
{
this.propertyGrid1.SelectedObject = mySettings;
}
It seems I can't bind the Settings directly to the property grid, I had to create an object from the settings and bind to that. I put the object assignment at form level in case I needed it to update later, but it could also be assigned in the form load event if I don't need to refer to it later.
This shows application settings as disabled and user settings enabled in the property grid, as expected.
Now I want to change a setting and save that back to the actual settings file. I can't seem to find any recent article on how to do that. I found an article from 2009 but it was quite complex, and I feel like that should be simpler in .Net 5.
Did I properly load the property grid?
Is there a way to update all changed user settings in 1 go, or do I need to iterate each property grid item, verify if it was changed, then update the actual setting?
How do I iterate the grid items, skipping categories & disabled items, and put them back into the settings?
Thanks so much for any help....
|
|
|
|
|
|
Thanks Gerry.
I had already read that article, but I'm still not understanding how to get the (maybe) changed settings out of the Property Grid and back into the actual Settings object before saving the settings to the proper settings file.
I'm thinking I need to iterate each setting in the Property Grid, compare it in code to the Settings object, and for each one that doesn't agree, update that setting in code, THEN execute the save command.
Remember, I instantiated the Settings object, and used that to bind to the Property Grid. Still not sure I did that part correctly either, but it does cause the settings to appear in the Property Grid.
|
|
|
|
|
Never used it but the docs say the Property Grid is for "browsing" ... How are you actually changing the settings?
I have my own custom "settings" code (serialize; deserialize). I verify the settings as a whole; I don't need to go back and see what "changed".
Any object can be iterated using reflection; so if you had to compare, I'd use reflection to compare like named members if it came down to it.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Thanks Gerry.
Not sure what is meant by "browsing". If the property grid is bound to the Properties.Settings collection, they (the settings) just appear in the property grid at run-time. Depending on how the grid is bound, it either updates the settings immediately if you make a change, or you have to iterate the grid items to see if any changes were made. For each of the changes, you need to update the setting itself. This is decided when instantiating an object to the settings collection before binding the property grid to it. If you use the New keyword, settings are not auto-updated, you need to update each grid item value to settings. If not using New, the changed values are updated immediately. Either way, you need to remember to save the settings.
I chose the first method (New) so if a user "accidentally" changes a setting in the grid, they just click the Cancel button and nothing is changed. Otherwise, they need to click the Save button.
|
|
|
|
|
I got this working now. It loads the settings into the PropertyGrid perfectly. Since I'm not limiting it to User settings, it shows them all, both User & Application. Since Application settings are read-only, the PropertyGrid helpfully makes those grey'd out.
Interesting quirk: If I bind the PropertyGrid to an object instantiated from the settings themselves, changing a setting in the PropertyGrid directly updates the User settings. Just need to remember to save the settings before closing the form.
Since I wanted this to not auto-change the settings, I created a Save button, and bound the form to a settings-type object using the New keyword. If I change a setting in the PropertyGrid, I iterate its items and compare each value to the current matching setting value. If different, I update the settings right then, and set a bool flag that something has changed. After completing iteration I save the settings and we're done. Works great.
Now I want to be able to add a new setting.
Does anybody know what the enumeration name is that contains the different setting types? You can see them in the project properties, Settings tab, when you go to add a setting. The Type drop-down shows many types (string, char, bool, int,...). I'd love to be able to choose which type in code when trying to add a new setting.
Thanks...
|
|
|
|
|
This one's on how to create new application settings.
How to: Create Application Settings - Windows Forms .NET Framework | Microsoft Learn
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
With the help of a Udemy course I am learning ASP.NET Core MVC. The course is this one:
https://www.udemy.com/course/aspnet-core-mvc-configuracion-e-implementacion-net-6/
I haven't finished it yet, but I already have a question: Is it possible to host the application on a Linux based webspace, where .NET Core is not installed and to make it worse, I don't have root permission to install it? On this webspace I can host Wordpress, Joomla, HTML+CSS etc. Is it possible to publish the ASP.NET Core MVC application in such a way, that everything that is needed is included?
Thanks in advance for your help.
René
|
|
|
|
|
temuco wrote: Linux based webspace, where .NET Core is not installed
The binaries require OS specific calls so you would need libraries that support the linux version. Been years since I looked but in the past those existed for .Net.
temuco wrote: Is it possible to publish the ASP.NET Core MVC
With minimal information provided but with you saying that you "host" wordpress then I would say yes. Figuring out all of the libraries is going to take a bit but should be possible.
|
|
|
|
|
temuco wrote: With the help of a Udemy course I am learning ASP.NET Core MVC. Does the course offer any support ?
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
Thank you, I will ask. You're right, I hadn't thought about that.
|
|
|
|
|
Hello dear experts,
I have been having sleepless nights trying to figure why my insert statements keep inserting duplicate records and at this point, I am so stumped that I could really, really use your superior skills to help me out.
First some background. I have been with this company for a little over two years and I was handed over an application that keeps breaking, mostly due to poor DB design.
I am asked to not discard the current DB design to "work with what I have."
The application accepts applications for request for some financial assistance but the applicant must meet certain conditions in order to be considered. If the applicant does not meet conditions, the application is rejected and returned to the applicant with reasons why it is rejected. The applicant is asked for adddress (install address and mailing address are requested.) there two key items to look for.
(1), Is Install address (residential address, same as mailing address?
(2), Is the application returned (rejected) or not.
So, here is what the code I am working with is supposed to do.
It searches the db for records and if record exists, then the code checks whether the application is rejected or not and is does install address same as mailing address or not.
The logic goes like this:
If applicant's address is same as mailing address (if SameAsMailing.Checked)
If record exists, then if SameAsMailing.Checked and returnyesorno.SelectedValue=0 Then install address is same as mailing address and it is not a return then update three tables (Addresses, Owner and Application tables).
If SameAsChecking is not checked and returnyesorno.SelectedValue=1 then install address is not same as mailing address, therefore, insert the both the install address and mailing address and also process for return by inserting records into Addresses, Owner, Application and return tables.
If install address is same as mailing address but not for return, then insert into Addresses, Owner and Application tables.
Finally, if install address is not the same as and also not a return then insert into Addresses, Owner and Application tables.
No matter the variation that I use, I keep getting about 4 duplicate records inserted into Addresses and Applications table.
It is a complicated mess and I need whatever help I can get from you superior talents, please.
Here is what makes it so complicated.
On the Addresses table, there are three key fields, AutoID (integer) Mailing and Installation are BIT datatypes.
If both Install address and mailing addresses are same, then both Mailing and Installation get value of 1 (true) and therefore AutoIID gets assigned same value.
If install address nd mailing address are different, Installation gets 1 and Mailing gets 0
Then on Applications table, there are two key fields, MailAddress and InstallAddress (both integer datatypes)
If Mailing and Installation (from Addresses table) both have a value of 1, then both MailAddress and InstallationAddress get the value of AutoID, say 123
If the Install Address is different from Mailing address, AutoID should get 123 for InstallAddress and 1234 for MailAddress.
I have tried various combinations of the insert statements but to no avail.
I have attached a copy of my code and it is very long.
I ask for your patience and assistance. Please help. I have delayed giving management demo and they have insisted that I show something by wednesday March 15th. Any assistance you could give would really, really be very helpful.
Thank you in advance for your patience and assistance.<a href="https://www.kenig-dev.tech/wp-content/uploads/2023/03/code.txt"></a>
Richard Deeming, could please help me? Sorry to call your name. I will just this time only because of my desparation.
|
|
|
|
|
Out of interest, I took a look at your code. Honestly, that's going to be almost impossible for someone else to solve in its current form. Some of the things you should look at, to make it more readable.
- Actually use SQL Parameters properly. Right now, you're creating sql parameters, but you are injecting the values directly into your SQL statements - this means you are open for SQL Injection attacks
- Break the logic up into smaller chunks - that code is far too long to understand
- Close your connections when you're finished with them. You are going to hit issues very quickly if you don't.
- You're issuing insert statements that are using a value of -1 inserted records to determine whether or not a record is a duplicate. Rather than doing this, you should actually use real duplicate checks. You should use something like
IF NOT EXISTS (SELECT ....) INSERT INTO .... , but be aware that it is technically possible to still get duplicates if you encounter race conditions.
modified 13-Mar-23 7:27am.
|
|
|
|
|