473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Receiving 3144 bytes TCP/IP socket During debug mode, but during excecution 1024 byte

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);
Sep 23 '10 #1
12 2550
Oralloy
988 Expert 512MB
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.
Expand|Select|Wrap|Line Numbers
  1. boolean msgComplete = false;
  2. int start = 0;
  3. while (! msgComplete)
  4. {
  5.   int input = socket.Receive(RecData, start, (RecData.Length-start), SocketFlags.None);
  6.   if (input = -1)
  7.     throw new Exception("socket disconnect");
  8.   else if (<message is not complete>)
  9.     start += input;
  10.   else
  11.     msgComplete = true;
  12. }
  13.  
Sep 23 '10 #2
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;
}
Sep 24 '10 #3
How would the available bytes in the socket could be found without using "socket.Available".
Sep 24 '10 #4
Oralloy
988 Expert 512MB
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.
Expand|Select|Wrap|Line Numbers
  1.   else if (<message is not complete>) 
It's possible that the test code becomes
Expand|Select|Wrap|Line Numbers
  1.   else if (isMessageComplete(RecData, (start+input))) 
I don't know your messaging protocol, so I can't make any stronger suggestions.
Sep 27 '10 #5
Plater
7,872 Expert 4TB
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
Sep 27 '10 #6
Oralloy
988 Expert 512MB
@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.
Sep 27 '10 #7
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.
Sep 28 '10 #8
Oralloy
988 Expert 512MB
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:

Expand|Select|Wrap|Line Numbers
  1. int in;
  2. for(int siz=0; siz < 3144; siz+=in) { 
  3.   in = socket.Receive(RecData, siz, (RecData.Length-siz), SocketFlags.None); 
  4.   if (in = -1) throw new Exception("socket disconnect"); 
Sep 28 '10 #9
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.
Sep 29 '10 #10
Oralloy
988 Expert 512MB
So, assume that cmd is the command byte you sent

Expand|Select|Wrap|Line Numbers
  1. int expct = -1;
  2. if (cmd = 0x01) expct = 3000;
  3. if (cmd = 0x02) expct = 4001;
  4. if (cmd = 0x03) expct = 3144;
  5. if (expct = -1) throw new Error("unknowed cmd");
  6.  
  7. int in; 
  8. for(int siz=0; siz < expct; siz+=in) {  
  9.   in = socket.Receive(RecData, siz, (RecData.Length-siz), SocketFlags.None);  
  10.   if (in = -1) throw new Exception("socket disconnect");  
  11. }  
Sep 29 '10 #11
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.
Sep 30 '10 #12
Oralloy
988 Expert 512MB
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?
Sep 30 '10 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

12
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...
1
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...
6
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...
0
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...
1
by: Ryan | last post by:
Is there any way to view what data is populating my DataSet tables during debugging? Thanks, Ryan
22
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...
5
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...
0
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....
3
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...
8
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),...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
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
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.