Hey all,

I have this problem that I can't work out why it is doing it.

I've got a client - server program. The client sends an request to the Server, which in returns it queries an access database and save the output as a file. Which it will then send to the client.

It looks as if it saves the file correctly with out any corruption.

My problem is sometimes when I run it, some information gets corrupted (I think that it leaves some of the data off the end of the sending string)

Any suggestions on how to improve my code and make it more robust is extremly welcome

I've stripped out the code that I beleive is causing the problem. The code has some comments in it so you know why and what it is doing.

Any suggestions on how to improve my code and make it more robust is extremly welcome

arrayNumber = Left(strData, InStr(1, strData, "\") - 1)

You're pulling an array number based on the position of the "\?"
I don't understand that. Explain, if you would.

And you declared arrayNumber as long and assign a string value to it? Why are you doing this? If you're using the Left() function (which returns a variant string value), you should be assigning its return value to a string variable?

I don't see how you dependably come up with a meaningful value number to use for your string array using this code.

arrayNumber = Left(strData, InStr(1, strData, "\") - 1)

You're pulling an array number based on the position of the "\?"
I don't understand that. Explain, if you would.

Before I send the files I add it to the array so that I know how many sections I needs to send, and if i'm missing a section then it can be sent again accuractly

And you declared arrayNumber as long and assign a string value to it? Why are you doing this? If you're using the Left() function (which returns a variant string value), you should be assigning its return value to a string variable?

I don't see how you dependably come up with a meaningful value number to use for your string array using this code.

On the sending string it is prefixes with the number, This is so that if there is a backlog of messages waiting to be processed and they get mixed up. The client adds that data into the array at that number. not very clear?. I've tried to explain it better below

(This is only an example)

A simple way of looking at that is that the server has 4 words to send "I have four words" I add it to an array

myArray(1) = I
myArray(2) = have
myArray(3) = four
myArray(4) = words

I then send each element of the array to the client

When the client gets the array element it saves it to an array, I do it this way in case I get the words mixed up when received, ie in this order "I four have words"

and that line makes sure that it is in the correct part of the array by taking the prefixed number and then saves the rest in to that element

Hope that makes sense

arrayNumber = Left(strData, InStr(1, strData, "\") - 1)

Hope that makes sense

Well, you know, if you have to tell another programmer, I hope that it makes sense, you've got problems. Of course, that may depend on the programmer. But there's a simple formula for programming that should be followed: KISS.

I still can't make sense of that code. I still remember my old programming teacher telling me when I provided a very complex solution to a simple problem, "Keep it Simple Stupid!"

But this is what I'm guessing. You send a string: e.g. "1\some file data" You pull the number one with your code. That happens to be an integer. And I assume because there are only numbers before the "\" that you always extract integers in string form. And because the string can be converted by the compiler to a long, it doesn't throw an error.

In keeping with the paradigm of KISS--you did ask for tips for more robust code--I would simplify that code.

Private Sub SortNewData(ByVal StrSortcode As String, ByVal strData As String)
' Change that to the following

Private Sub SortNewData(ByVal StrSortcode As String, ByVal strData As String, MyArrayNumber as Long)

If you already know what the ArrayNumber is, why not just send it?
Add another parameter to your function. And then you wouldn't have to write extra code to extract the file data either. Just send the data as a string without any extra identifying characters. You could eliminate about 2 or 3 lines of code in that one Select Case statement.

That would be simpler. And if you had to try to figure out 2 years from now what you were doing, it wouldn't be a problem.

A common problem with programmers is that they write complex solutions for simple problems. The code may seem impressive at the time, but all that complexity can lead to unintelligible and unmanageable code down the road. And that means spending extra unneeded hours to sort through solutions that should have been written simply to begin with. Extra hours translates into extra money. And that's not a good thing.

Sometimes our minds get trapped in 'complex' mode. We have to continually remind ourselves to keep it simple. The simpler the better.

I haven't had time to analyze the rest of the code. I was stuck there. But I assume your complex solution works, but why is the data corrupt? Put a breakpoint at that point and step through it with the F8 checking your variables as you go to see if you're program is processing the data correctly.

If you consistently come up with corrupt data in a certain situation, that provides a perfect condition to use a breakpoint and the F8 function key to step through that section of the code you believe to be at fault to check the data to see where the problem lies.

Sorry for the long delay


Your right about getting stuck in 'complex' mode, that was where my mind was on writing that blurb,

I only asked that as to make sure that I explained my self clearly, I know sometimes i don't ;S. The code it self is actually simple design,

Thanks for the tips, I'll merge them in.

The weird thing is that it works fine on my computer (hosting both server and client) but when I put the server on a different computer, it corrupts,

Any ideas?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.