Click here to Skip to main content
15,887,338 members
Home / Discussions / C#
   

C#

 
AnswerRe: Gridview Pin
HimanshuJoshi19-Aug-09 21:04
HimanshuJoshi19-Aug-09 21:04 
AnswerRe: Gridview Pin
Vimalsoft(Pty) Ltd19-Aug-09 22:20
professionalVimalsoft(Pty) Ltd19-Aug-09 22:20 
AnswerRe: Gridview Pin
padmanabhan N19-Aug-09 22:43
padmanabhan N19-Aug-09 22:43 
AnswerRe: Gridview Pin
wyj12020-Aug-09 0:40
wyj12020-Aug-09 0:40 
QuestionNetworkStream problem... Pin
Trapper-Hell19-Aug-09 20:43
Trapper-Hell19-Aug-09 20:43 
AnswerRe: NetworkStream problem... Pin
Luc Pattyn19-Aug-09 22:51
sitebuilderLuc Pattyn19-Aug-09 22:51 
GeneralRe: NetworkStream problem... Pin
Trapper-Hell23-Aug-09 21:07
Trapper-Hell23-Aug-09 21:07 
GeneralRe: NetworkStream problem... Pin
Luc Pattyn24-Aug-09 0:38
sitebuilderLuc Pattyn24-Aug-09 0:38 
Hi,

if your code works sometimes, that would be a miracle.

here are some detailed comments:

1. SendString()
your try-catch converts an exception into a bool result, something I don't like since now you MUST check the return value every time you use SendString; as it normally will succeed, why not drop the try-catch and let it throw when there is a problem?

2. SendBytes()
2a. Stream.CanWrite is a constant, either it is always false and your app will fail, or it is always true. Testing it inside a loop doesn't make much sense.
2b. while (nsStream.DataAvailable) {...} is terrible code and the result depends on the speed of your PC, so it is completely unreliable.
this loop is wasting CPU cycles; why should a send method be interested in incoming data? and if there is a need, try to get it event-driven or use a separate thread and a Sleep inside the loop.

3. Send()
3a. Calling SendString and NOT checking the result???
3b. A try-catch that will never catch anything as the methods themselves have their own try-catch?
3c. you send two things (length and data), both in ASCII, without separator. How will the receiver now where the length ends and the data starts? Here is a major problem. What if the first databyte happens to be in the range [0x30,0x39] i.e. an ASCII digit?
3d. NEVER use catch{...} without parameter as it throws away all information about an exception that may be happening; the correct way to do this would be:
try {
    ....
} catch(AVerySpecificExceptionWeWantToReallyIgnore) {
    // empty by design, as this exception is really harmless and does not need logging
} catch(Exception exc) {
    log(exc.ToString());
    ...
}

where log(string) is some method to signal a mishap to the user and./or the log file.

4. Serialize()
4a. oSerialize = null; does not make sense, as oSerialize is a local variable in a method that is reaching its end.
4b. bf = null; does not make sense, as bf is a local variable in a method that is reaching its end.
4c. msMem.Flush(); doesn't do a thing.
4d. MemoryStream has a Dispose method, so you should call that when done to clean up things. Hence return msMem.ToArray() should be split in 3 lines.

5. ReceiveBytes()
5a. here you claim the length takes 8 characters; it does not. If you want it that way (good idea!) then make it so, e.g. SendString(Serialize(oSend).Length.ToString("D8")); in Send().

etc etc a lot of comments on sending also apply to your receiving code.

and some overall suggestions:

1. seems like you haven't really grasped the concept of Exceptions and try-catch, as you are trying to get rid of exceptions as soon as possible. I suggest you (re)read that chapter in your C# book.
2. I always suggest to improve observability by adding logging, with a log(string) method and possibly also a log(Exception) method, which log 1 or many lines to an appropriate output, such as a console window, a ListBox, or a text file (and possibly two of those); absolutely not to a MessageBox as you can only see 1 of them at a time.
public void log(string s) {
    listbox1.Items.Add(s);
    listbox1.TopIndex=listbox1.Items.Count-1; // scroll
    File.AppendText(logfileName, s+Environment.NewLine);
}

3. read the MSDN documentation on the classes and methods you are using, including the remarks section. I know the examples are often absent or too simple, but the text that is present usually is relevant.
4. I wouldn't turn a memorystream into a byte array into a networkstream; the byte array is a detour, and maybe the serializer could write straight to the networkstream.
5. the communication code you have is extensive and unreliable; apply my suggestions, you will make it both shorter and better.

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.

GeneralRe: NetworkStream problem... Pin
Trapper-Hell24-Aug-09 2:30
Trapper-Hell24-Aug-09 2:30 
GeneralRe: NetworkStream problem... Pin
Luc Pattyn24-Aug-09 2:36
sitebuilderLuc Pattyn24-Aug-09 2:36 
GeneralRe: NetworkStream problem... Pin
Trapper-Hell24-Aug-09 20:06
Trapper-Hell24-Aug-09 20:06 
GeneralRe: NetworkStream problem... Pin
Luc Pattyn24-Aug-09 23:49
sitebuilderLuc Pattyn24-Aug-09 23:49 
QuestionWin Form - resize form problem Pin
Gindi Bar Yahav19-Aug-09 19:41
Gindi Bar Yahav19-Aug-09 19:41 
AnswerRe: Win Form - resize form problem Pin
dan!sh 19-Aug-09 20:14
professional dan!sh 19-Aug-09 20:14 
AnswerRe: Win Form - resize form problem Pin
darkelv19-Aug-09 20:15
darkelv19-Aug-09 20:15 
GeneralRe: Win Form - resize form problem Pin
Gindi Bar Yahav19-Aug-09 20:30
Gindi Bar Yahav19-Aug-09 20:30 
GeneralRe: Win Form - resize form problem Pin
dan!sh 19-Aug-09 20:42
professional dan!sh 19-Aug-09 20:42 
GeneralRe: Win Form - resize form problem Pin
darkelv19-Aug-09 21:37
darkelv19-Aug-09 21:37 
AnswerRe: Win Form - resize form problem Pin
Trapper-Hell19-Aug-09 20:49
Trapper-Hell19-Aug-09 20:49 
QuestionResource File Help Pin
Dushan12319-Aug-09 18:12
Dushan12319-Aug-09 18:12 
AnswerRe: Resource File Help Pin
Christian Graus19-Aug-09 18:23
protectorChristian Graus19-Aug-09 18:23 
Questionusing DOM how to identify the link that is clicked? Pin
Jacobb Michael19-Aug-09 17:54
Jacobb Michael19-Aug-09 17:54 
QuestionHealth monitoring server up or down and logging to database Pin
ramindya19-Aug-09 13:37
ramindya19-Aug-09 13:37 
AnswerRe: Health monitoring server up or down and logging to database Pin
Christian Graus19-Aug-09 15:36
protectorChristian Graus19-Aug-09 15:36 
QuestionBackground Worker Pin
spankyleo12319-Aug-09 12:14
spankyleo12319-Aug-09 12:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.