I am Receiving 3144 bytes through TCP/IP socket During debug mode, but during excecution 1024 byte were received
Why it is happen
IPEndPoint ipEnd = new IPEndPoint(ipAdd, ipPort);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipEnd);
byte[] sendData = Encoding.ASCII.GetBytes("msg");
socket.Send(sendData);
int byteLen = 4096;
byte[] RecData = new byte[byteLen];
DataLen = socket.Receive(RecData, 0, RecData.Length, SocketFlags.None);
12 2550
Raj,
You need to build a loop around your call to Receive.
Since you have no idea of how many bytes make it through in a network packet, you'll have to wait for the whole message to come through.
This is basically pseudo-code/java combined. Hopefully it'll help. -
boolean msgComplete = false;
-
int start = 0;
-
while (! msgComplete)
-
{
-
int input = socket.Receive(RecData, start, (RecData.Length-start), SocketFlags.None);
-
if (input = -1)
-
throw new Exception("socket disconnect");
-
else if (<message is not complete>)
-
start += input;
-
else
-
msgComplete = true;
-
}
-
yes, it works good. Without using socket.Available how would we find available message(data) in the socket.
boolean msgComplete = false;
int start = 0;
while (! msgComplete)
{
int input = socket.Receive(RecData, start, (RecData.Length-start), SocketFlags.None);
if (input = -1)
throw new Exception("socket disconnect");
else if (socket.Available != 0)
start += input;
else
msgComplete = true;
}
How would the available bytes in the socket could be found without using "socket.Available".
What messaging structure are you using?
Are you using a message structure, where the first four bytes are the message length?
Or, are you using a message structure where each record is terminated by a sentinel character, like '\n'?
You sent the message, so you should know its structure. The whole point of the test I penciled in is that you have to provide that test. - else if (<message is not complete>)
It's possible that the test code becomes - else if (isMessageComplete(RecData, (start+input)))
I don't know your messaging protocol, so I can't make any stronger suggestions.
By default the buffer is 1024. (Think of it as a "maximum block size") So the most you could read in a single read is 1024. (This is not always true, just common in .NET)
You should always be checking how many bytes you get from read/write operation and ensuring they are all read/written
@Plater,
I think Paraveen Raj V's issue is determining when a complete message is recieved. Without knowing what his communication protocol is, we can talk until we're blue in the face about blocking and such.
From my perspective, the current issues are:
1) Message Structure
and
2) Synchronous versus Asynchronous Messaging
Without answering those questions, just grabbing bytes from the incoming data stream until "no more are available" is pointless.
I am using Synchronous Messaging. On using System.Threading.Thread.Sleep(100);
before the function call, it works properly and good.
Why it happens? Why should we use System.Threading.Thread.Sleep(100); before function call
Iam sending a byte of messaage to server and then receiving 3144 bytes from server.
How would you find that the message is completely read or not.
Ok, that's the basic protocol.
Does the inquiry byte carry any meaning, other than its existance?
Is the response length always known to be 3144 bytes, or can it vary?
If really all you care about is 3144 bytes, because the record if fixed length, then use something like the following: - int in;
-
for(int siz=0; siz < 3144; siz+=in) {
-
in = socket.Receive(RecData, siz, (RecData.Length-siz), SocketFlags.None);
-
if (in = -1) throw new Exception("socket disconnect");
-
}
Yes, the response length won't be same, it varies depending upon the data that I send. Hence we need to code it dynamic. The fixed length can't be used.
I don't know why it requires Thread.Sleep() method.
So, assume that cmd is the command byte you sent -
int expct = -1;
-
if (cmd = 0x01) expct = 3000;
-
if (cmd = 0x02) expct = 4001;
-
if (cmd = 0x03) expct = 3144;
-
if (expct = -1) throw new Error("unknowed cmd");
-
-
int in;
-
for(int siz=0; siz < expct; siz+=in) {
-
in = socket.Receive(RecData, siz, (RecData.Length-siz), SocketFlags.None);
-
if (in = -1) throw new Exception("socket disconnect");
-
}
OK, How would you expect exact amount of bytes that your going to receive. Here I am receiving XML data and I am doing parsing over the XML data. Incase the received bytes goes more than one you expected, XML parsing would not happen, it through an exception.(ie I am telling about the XML tag)
The data inside the XML tags may varies. If that happen then your expectation would go wrong. As in your code you read only 3144 bytes for cmd = 0x03.If you getting more than 3144 what happens. For me the application freezes because it have been blocked to get remaining bytes.
so, I think that expecting a certain amount of bytes would not be dynamic.
Ouch!
You don't have a well defined protocol at all.
You have several options at this point - object streams, use a record terminator, or a send a record-length field of some sort.
Which do you prefer to do?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: nospam |
last post by:
All the documentation says that leaving an ASP.NET application in debug
mode has a big performance hit. I can't detect any difference between
debug and non-debug modes. Am I missing something or is...
|
by: Bob Day |
last post by:
Consider the two code snippets below.
a) During development, if you hold your cursor over AKA.SomeDateTime in
Snippet 2, it will indicate a value of #1/1/1950# as expected.
b) If you...
|
by: pauldepstein |
last post by:
To help me debug, I am writing a lot of information into a stream which
I call debug.
However, because of the large amount of time taken to print this
information, I only want this printed while...
|
by: Max |
last post by:
Can anyone point me towards reasons why Response.Redirect would work ok when
running without debugging (via "Start without debugging" menu choice) but be
really really slow when running in debug...
|
by: Ryan |
last post by:
Is there any way to view what data is populating my DataSet tables during
debugging?
Thanks,
Ryan
|
by: semedao |
last post by:
Hi , I am using asyc sockets p2p connection between 2 clients.
when I debug step by step the both sides , i'ts work ok.
when I run it , in somepoint (same location in the code)
when I want to...
|
by: B. |
last post by:
We just recently move to code from VC++6 to VC++.NET 2005 and upgrade
the warning level from 3 to 4. In debug mode, we compile the
application with no warning no error, but when I build it in...
|
by: MJKulangara |
last post by:
I have an ap written in vb.net using 1.2 framework that makes a call to
a c++ dll. The executable I create runs just fine, but when I run the
ap in debug mode I get a PInvokeStackImbalance error....
|
by: corey.hutchinson |
last post by:
I have an application that when I run it in VS 2005 debug mode it
operates faster (10 fold) than when I execute the exe directly from
the bin directory. The one operation in particular in which...
|
by: Jothishankar |
last post by:
Hi,
I am new to c#. I am trying to build an application that does backup
of files to an external hard disk.
My application behaves strangely. When i run the application under
debug mode (F5),...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| | |