473,408 Members | 1,771 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,408 software developers and data experts.

To find number of bytes available in socket

"socket.Available" is not working.

How would you Determine number of bytes present in a TCP/IP socket before reading those bytes without using socket.Available
Expand|Select|Wrap|Line Numbers
  1. void Receive(Socket socket, byte[] buffer, int offset)
  2.         {
  3.             int sockAvail = socket.Available;//the value of socket.Available does not asigned to sockAvail.
  4.             int received = 0;
  5.             do
  6.             {
  7.                 try
  8.                 {
  9.                     received += socket.Receive(buffer, offset + received, sockAvail - received, SocketFlags.None);
  10.                 }
  11.                 catch (Exception e) { Messagebox.Message(e.Message); }
  12.             } while (received < sockAvail);
  13.             MessageBox.Show(socket.Available.ToString());//Here 0 is displayed always
  14.         }
Is there any fault in tool? suggest me without using socket.Available how do I find number of bytes available in TCP/IP socket.
Sep 27 '10 #1
3 5503
Plater
7,872 Expert 4TB
You are probably checking the value of socket.Available before and data is actually received.
If you add a System.Threading.Thread.Sleep(10); right before the that line, does sockAvail become a value?
Sep 27 '10 #2
After adding System.Threading.Thread.Sleep(10);
the value of socket.Available is assigned to sockAvail
and the code works fine.

What is the reason behind it?
Sep 28 '10 #3
Plater
7,872 Expert 4TB
Your code executes too fast. The socket object hasn't had the chance to receive any reply bytes before you request them
Sep 28 '10 #4

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

Similar topics

7
by: groleo | last post by:
Hi. I've searched a lot on google, but no one seems to develop a socket class for c++? I've found a lot of classes that works only on Windows. The thing is that I need a BSD socket library. I...
1
by: | last post by:
one thing to keep in mind is that i've written multiple tcp apps in .net 2002 and 2003, and haven't seen this problem before. I have a simple socket app where a client sends some # of bytes to...
3
by: Dirk Reske | last post by:
Hello, in msdn stands, that the socket.available property can fire a SocketException when the remote machine has closed the connection. why can? I have to check the number of receivable bytes...
8
by: Preben Randhol | last post by:
Hi If I use len() on a string containing unicode letters I get the number of bytes the string uses. This means that len() can report size 6 when the unicode string only contains 3 characters...
6
by: androoo | last post by:
Hello all, I have a string for example : strTest = "a lineof text (60) witha number in it" I need to extract the number from between the brackets, the postion of the number in brackets is...
3
by: sunmat | last post by:
To find number of character without space using java Example: String = prabu sun No. of char =8 plz send me code
2
by: Jason | last post by:
Hello I'm about ready to attempt writting a simple TCP server in C# in which all I need to do is send out some data to a particular IP and port, and the end device will send it back to me. ...
58
by: sh.vipin | last post by:
is there any way to find out number of bytes freed on a particular free() call in C
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.