Click here to Skip to main content
15,911,035 members
Home / Discussions / C#
   

C#

 
GeneralRe: Show small TM sign on title bar title text Pin
Dan Neely19-Jul-06 9:39
Dan Neely19-Jul-06 9:39 
GeneralRe: Show small TM sign on title bar title text Pin
RizwanSharp19-Jul-06 9:44
RizwanSharp19-Jul-06 9:44 
Questionhow can I sum ... Pin
mostafa_h19-Jul-06 8:22
mostafa_h19-Jul-06 8:22 
AnswerIf you are using crystal reports Pin
Ennis Ray Lynch, Jr.19-Jul-06 8:28
Ennis Ray Lynch, Jr.19-Jul-06 8:28 
GeneralRe: If you are using crystal reports Pin
mostafa_h19-Jul-06 10:09
mostafa_h19-Jul-06 10:09 
GeneralDon't forget about SQL Injection Pin
Ennis Ray Lynch, Jr.19-Jul-06 10:12
Ennis Ray Lynch, Jr.19-Jul-06 10:12 
GeneralRe: Don't forget about SQL Injection Pin
mostafa_h19-Jul-06 10:36
mostafa_h19-Jul-06 10:36 
QuestionThread Safety??? [modified] Pin
RizwanSharp19-Jul-06 8:12
RizwanSharp19-Jul-06 8:12 
Hello Devs,
I'm developing a Chat application and I'm facing a problem. I have the following Method which is called from multiple threads. And when I start sending too much messages in speed (which invokes this method so fast) then client is disconnected. Let me show you the method and then continue my discussion:

public void SendMessage(TextMessage messageToSend)<br />
        {<br />
            try<br />
            {<br />
                lock (this.dataStream)<br />
                {<br />
                    byte[] serializedBytes = messageToSend.GetBytes();                      // Get bytes from the Message object<br />
                    byte[] encMessageBytes = encryptorDecryptor.Encrypt(serializedBytes);   // Encrypt bytes<br />
                    serializedBytes = null;<br />
                    int messageSize = encMessageBytes.Length;<br />
                    byte[] messageSizeBuffer = new byte[this.messageDataLengthBufferSize];<br />
                    messageSizeBuffer = BitConverter.GetBytes(messageSize);<br />
                    this.dataStream.Write(messageSizeBuffer, 0, this.messageDataLengthBufferSize);                                  // Send the size of the message<br />
                    this.dataStream.Write(encMessageBytes, 0, messageSize);                        // Send the actual message<br />
                    this.dataStream.Flush();<br />
                    messageSizeBuffer = null;<br />
                    encMessageBytes = null;<br />
                }<br />
<br />
            }<br />
            catch (Exception ex)<br />
            {<br />
                string str = ex.Message + "\n" + ex.StackTrace;<br />
                Disconnect();<br />
                if (Disconnected != null)<br />
                {<br />
                    Disconnected(this, EventArgs.Empty);<br />
                }<br />
            }<br />
        }


In my opnion, the cause of this problem is multi threaded environment or it overflows data when i start sending to fast. for example if it is already working to send data on NetworkStream then a new call to this method is made which passes a new object of TextMessage to it which over writes the existing one so it is all messed up.
Note: Actual exception I get on the receiving end when length of data is not one which is supposed to be. See the code for Receiving:

int messageSize = BitConverter.ToInt32(messageDataLengthBuffer, 0);<br />
                MemoryStream memstrm = new MemoryStream();<br />
                <br />
                readByteCount = 0;<br />
                while (messageSize > 0)<br />
                {<br />
                    readByteCount = dataStream.Read(this.messageDataBuffer, 0, messageDataBuffer.Length);<br />
                    if (readByteCount < 1)<br />
                    {<br />
                        Disconnect();<br />
                        return;<br />
                    }<br />
                    memstrm.Write(this.messageDataBuffer, 0, readByteCount);<br />
                    messageSize -= readByteCount;<br />
                }<br />
                memstrm.Position = 0;<br />
                <br />
                byte[] decryptedData = encryptorDecryptor.Decrypt(memstrm.ToArray());<br />
                receivedMessage = new TextMessage(decryptedData);<br />
<br />
                memstrm.Close();<br />
                memstrm = null;


On the receving end I read 4 bytes as a size of incoming message then loop the piece of code to get all data untill complete message is received as of the message size. So when i start messages too fast then at the end of the loop "messageSize" usually gets in -ve which must never be less than 0 and when i try to get data decrypted again, it throughs exception "Length of data is invalid".

Is there any way that i can implement a mechanism that if SendMessage is doing its work then it should not be called again untill its done with its existing work? Or any other way to solve this issue?

Its really urgent. Any help will be really appreciated!!!

Best Regards
AnswerRe: Thread Safety??? Pin
Ennis Ray Lynch, Jr.19-Jul-06 8:34
Ennis Ray Lynch, Jr.19-Jul-06 8:34 
GeneralRe: Thread Safety??? Pin
RizwanSharp19-Jul-06 9:50
RizwanSharp19-Jul-06 9:50 
GeneralPerhaps only one thread? Pin
Ennis Ray Lynch, Jr.19-Jul-06 10:32
Ennis Ray Lynch, Jr.19-Jul-06 10:32 
QuestionRe: Perhaps only one thread? Pin
RizwanSharp19-Jul-06 11:16
RizwanSharp19-Jul-06 11:16 
GeneralRe: Thread Safety??? Pin
Super Lloyd19-Jul-06 17:22
Super Lloyd19-Jul-06 17:22 
QuestionRe: Thread Safety??? Pin
Dustin Metzgar19-Jul-06 9:13
Dustin Metzgar19-Jul-06 9:13 
AnswerRe: Thread Safety??? Pin
RizwanSharp19-Jul-06 9:52
RizwanSharp19-Jul-06 9:52 
GeneralRe: Thread Safety??? Pin
Dustin Metzgar19-Jul-06 10:16
Dustin Metzgar19-Jul-06 10:16 
GeneralRe: Thread Safety??? Pin
RizwanSharp19-Jul-06 11:15
RizwanSharp19-Jul-06 11:15 
GeneralRe: Thread Safety??? Pin
Dustin Metzgar19-Jul-06 11:25
Dustin Metzgar19-Jul-06 11:25 
GeneralRe: Thread Safety??? Pin
RizwanSharp19-Jul-06 11:35
RizwanSharp19-Jul-06 11:35 
GeneralRe: Thread Safety??? Pin
Dustin Metzgar19-Jul-06 13:45
Dustin Metzgar19-Jul-06 13:45 
AnswerGot the actual problem!!! Pin
RizwanSharp19-Jul-06 21:43
RizwanSharp19-Jul-06 21:43 
GeneralRe: Got the actual problem!!! Pin
Dustin Metzgar20-Jul-06 5:23
Dustin Metzgar20-Jul-06 5:23 
GeneralSolved ;) Pin
RizwanSharp20-Jul-06 5:33
RizwanSharp20-Jul-06 5:33 
QuestionHow to get some special Windows colors? Pin
NPowDev19-Jul-06 7:25
NPowDev19-Jul-06 7:25 
AnswerRe: How to get some special Windows colors? Pin
Judah Gabriel Himango19-Jul-06 7:29
sponsorJudah Gabriel Himango19-Jul-06 7:29 

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.