Click here to Skip to main content
16,005,178 members
Home / Discussions / C#
   

C#

 
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 
Sorry guy to put you in problem. And thanks a lot for your help(Really appreciate itSmile | :)
I got the problem on receiving end. Please try to follow me:

lock (this.dataStream) // Lock the NetworkStream
{
byte[] serializedBytes = messageToSend.GetBytes(); // Get bytes from the Message object
byte[] encMessageBytes = encryptorDecryptor.Encrypt(serializedBytes); // Encrypt bytes
serializedBytes = null;
int messageSize = encMessageBytes.Length; // Know the Length of encrypted bytes
byte[] messageSizeBuffer = new byte[this.messageDataLengthBufferSize]; // Make a buffer of 4 bytes to store an integer value
messageSizeBuffer = BitConverter.GetBytes(messageSize); // Convert the messageSize into bytes
this.dataStream.Write(messageSizeBuffer, 0, this.messageDataLengthBufferSize); // Send the size of the message
this.dataStream.Write(encMessageBytes, 0, messageSize); // Send the actual message
this.dataStream.Flush();
messageSizeBuffer = null;
encMessageBytes = null;

what I'm doing in the above code is
1) I lock the NetworkStream
2) Convert the TextMessage's object into bytes (So that I can write them on NetworkStream)
3) Encrypt the Bytes
4) Take the Length of the encrypted bytes and store it into a 4 byte's array
5) Before I send the original message, I write the size of message as a 4 byte array.So that the receiver may know that incoming message includes XXX bytes and i have to read only this much data (not more than that).

Mad | :mad: But when I send messages too fast then the reading end reads data of more than one message (let me show you how)

Receiving End:

int messageSize = BitConverter.ToInt32(messageDataLengthBuffer, 0);
MemoryStream memstrm = new MemoryStream();

readByteCount = 0;
while (messageSize > 0)
{
readByteCount = dataStream.Read(this.messageDataBuffer, 0, messageDataBuffer.Length);
if (readByteCount < 1)
{
Disconnect();
return;
}
memstrm.Write(this.messageDataBuffer, 0, readByteCount);
messageSize -= readByteCount;
}
memstrm.Position = 0;

byte[] decryptedData = encryptorDecryptor.Decrypt(memstrm.ToArray());
receivedMessage = new TextMessage(decryptedData);

memstrm.Close();
memstrm = null;

1) I read 4 bytes from the Stream and Convert these 4 bytes to integer so I may know that incoming message has XXX number for bytes and I have to reaod only XXX bytes.

2) The Buffer I'm using is of 1024 which is filled up with this statement
readByteCount = dataStream.Read(this.messageDataBuffer, 0, messageDataBuffer.Length);

See problem exist at this statement i.e the dataStream inludes bytes of 3 messages on it which have to be read in 3 rounds but
I read all 3 messages with this statement because I'm reading 1024 bytes which is able to store all 3 messages.

Now suggest me a technique That i donot read more than the size of incoming message Confused | :confused: Assuming that a message can be of 5000 bytes for which I'm looping on read.

I hope, the problem is very clear now and you can definitely suggest a god solution. I'm also working on this, If i get the solution I'll let you know.

Best Regards,
Rizwan;)
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 
Generaland System.Drawing.SystemColors Pin
Ennis Ray Lynch, Jr.19-Jul-06 8:35
Ennis Ray Lynch, Jr.19-Jul-06 8:35 
GeneralRe: How to get some special Windows colors? Pin
NPowDev19-Jul-06 12:06
NPowDev19-Jul-06 12:06 
GeneralRe: How to get some special Windows colors? Pin
Judah Gabriel Himango19-Jul-06 17:14
sponsorJudah Gabriel Himango19-Jul-06 17:14 
GeneralRe: How to get some special Windows colors? [modified] Pin
NPowDev20-Jul-06 1:45
NPowDev20-Jul-06 1:45 
GeneralRe: How to get some special Windows colors? Pin
Judah Gabriel Himango20-Jul-06 4:36
sponsorJudah Gabriel Himango20-Jul-06 4:36 
Questionerror Pin
TAREQ F ABUZUHRI19-Jul-06 7:04
TAREQ F ABUZUHRI19-Jul-06 7:04 
AnswerRe: error Pin
Judah Gabriel Himango19-Jul-06 7:12
sponsorJudah Gabriel Himango19-Jul-06 7:12 
QuestionAdd attributes to XML doc [modified] Pin
VK-Cadec19-Jul-06 6:47
VK-Cadec19-Jul-06 6:47 
AnswerRe: Add attributes to XML doc Pin
stancrm19-Jul-06 7:21
stancrm19-Jul-06 7:21 
GeneralRe: Add attributes to XML doc Pin
VK-Cadec19-Jul-06 7:26
VK-Cadec19-Jul-06 7:26 
GeneralRe: Add attributes to XML doc Pin
Rob Graham19-Jul-06 10:45
Rob Graham19-Jul-06 10:45 
QuestionApplication deep logging/traceing Pin
nemopeti19-Jul-06 6:24
nemopeti19-Jul-06 6:24 
AnswerRe: Application deep logging/traceing Pin
Judah Gabriel Himango19-Jul-06 7:16
sponsorJudah Gabriel Himango19-Jul-06 7:16 

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.