943,864 Members | Top Members by Rank

Ad:
Feb 10th, 2009
0

Winsock Sending problem

Expand Post »
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.
Attached Files
File Type: txt client.txt (5.2 KB, 30 views)
File Type: txt server.txt (6.5 KB, 21 views)
Similar Threads
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
bushman_222 is offline Offline
65 posts
since Jan 2008
Feb 10th, 2009
0

Re: Winsock Sending problem

Quote ...
Any suggestions on how to improve my code and make it more robust is extremly welcome
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 49
Solved Threads: 44
Posting Pro in Training
hkdani is offline Offline
426 posts
since Nov 2007
Feb 11th, 2009
0

Re: Winsock Sending problem

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. arrayNumber = Left(strData, InStr(1, strData, "\") - 1)

Quote ...
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

Quote ...
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
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
bushman_222 is offline Offline
65 posts
since Jan 2008
Feb 11th, 2009
0

Re: Winsock Sending problem

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. 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.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub SortNewData(ByVal StrSortcode As String, ByVal strData As String)
  2. ' Change that to the following
  3.  
  4. 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.
Reputation Points: 49
Solved Threads: 44
Posting Pro in Training
hkdani is offline Offline
426 posts
since Nov 2007
Feb 17th, 2009
0

Re: Winsock Sending problem

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?
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
bushman_222 is offline Offline
65 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: problem with INSERT INTO command
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Visual Basic 6 Sub programs?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC