473,850 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket Programming - Receive Buffer Length

2 New Member
The code below works fine. As of now I just set the buffer length real big and all is good. However, I hate the impreciseness of that.

So my question in general is how do I know what length to set the buffer? In specific, my question is what causes the 'completed' event in the OnSocketConnect Completed method to fire? Does it fire at end of a packet or does happen at some end of data marker?
Expand|Select|Wrap|Line Numbers
  1.         public SocketClient(Page pg, Int32 port, Int32 length)
  2.         {
  3.             this.PAGE = pg;
  4.             this.LENGTH = length;
  5.             this.Q = new Queue<byte[]>();
  7.             DnsEndPoint endPoint = new DnsEndPoint("111.222.333.444", port);
  8.             Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  10.             SocketAsyncEventArgs argz = new SocketAsyncEventArgs();
  11.             argz.UserToken = socket;
  12.             argz.RemoteEndPoint = endPoint;
  13.             argz.Completed += new EventHandler<SocketAsyncEventArgs>(OnSocketConnectCompleted);
  14.             socket.ConnectAsync(argz);
  15.         }
  17.         private void OnSocketConnectCompleted(object sender, SocketAsyncEventArgs e)
  18.         {
  19.             byte[] response = new byte[this.LENGTH];
  20.             e.SetBuffer(response, 0, response.Length);
  21.             e.Completed += new EventHandler<SocketAsyncEventArgs>(OnSocketReceive);
  22.             Socket socket = (Socket)e.UserToken;
  23.             socket.ReceiveAsync(e);
  24.         }
  26.         private void OnSocketReceive(object sender, SocketAsyncEventArgs e)
  27.         {
  28.             Q.Enqueue(e.Buffer);
  30.             //Prepare to receive more data
  31.             Socket socket = (Socket)e.UserToken;
  32.             byte[] response = new byte[this.LENGTH];
  33.             e.SetBuffer(response, 0, response.Length);
  34.             socket.ReceiveAsync(e);
  35.         }
Nov 24 '09 #1
4 11253
7,872 Recognized Expert Expert
OnSocketConnect Completed is fired when a connection is established. Data transfer has nothing to do with it
Nov 24 '09 #2
2 New Member
Right but I did not ask anything about what causes the OnSocketConnect Completed event to fire. Please reread the question, I asked "what causes the completed' event in the OnSocketConnect Completed method to fire"

Obviously I am trying to understand how to adjust the incoming buffer to match the incoming data. Please explain the issue if you can. Could you possibly consider answering again?

I do appreciate the help.
Nov 25 '09 #3
7,872 Recognized Expert Expert
Edit: ok I see the subtle difference. We got so many people here where english is not their best language, that I just understood it to be a translation issue.
Nov 25 '09 #4
9,735 Recognized Expert Moderator Expert
The best way to do this would be to append the length of the data being sent to the beginning of the data being sent.

Add an Integer (or Int16 if you don't want to use the full 32) to the beginning of the data. Read this integer when you first receive a request and then adjust your buffer.

One thing that you should know is that the data being sent could contain data for more than one "thing" that you are sending over the socket.

In other words the data that you retrieve in your OnSocketConnect Completed method could be for more than one "thing". You have to know how big the thing is that you're sending or else you won't know when the first thing has finished sending and the next thing begins (and of course you need to adjust your buffer size)

You can see the problems that I faced with this same issue in this thread: Determining the size of data being set from socket server

Nov 25 '09 #5

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

Similar topics

by: Brian Rice | last post by:
I have a socket application that is sending and receiving packets asynchronously. It works great except, when I receive packets that are larger than my receive buffer which then generate several EndReceive calls... these calls seem to be sometimes coming out of order. For example, the following is from my log file: Connection at 5/1/2004 11:39:14 AM SENT> ZZZ:1:FMZZZ:ID1234:PWAAAA:VR100:\0 RCVD>...
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of 1024 with all values set to Null arrive. Other than examine a block of 1024 to see if the entire block is null, is there any other way to determine if , say a chat message "Hi Charlie" has been received completely?
by: coloradowebdev | last post by:
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via TCP. the remoting site works like a champ. my problem is executing the transactions against the remote server and returning the response to the remoting client. i can open the socket fine and, if i am executing one transaction at a time, everything works great. it's when my proxy server...
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so that its buffer is bigger than this. I did this expecting the data to be read in one pass.
by: Eric Marthinsen | last post by:
Hello- I'm trying to create a console app that will accept an incoming HTTP request and output the information of the request to the console. The code is pretty simple - here's what it looks like: TcpListener listenter = new TcpListener(IPAddress.Any, 8000); listenter.Start();
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a number of different types of data coming in. I have a databuffer for each type of data coming in.
by: Ben | last post by:
I need to send data from a client to a server. In the server code I have: s = ServerSocket.Accept() If (s.Connected = False) Then MsgBox("Unable to connect", , "Server Error") Exit Sub End If While (s.Connected = True)
by: george585 | last post by:
Hello! I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is supposed to do the following: monitor ALL INCOMING TCP traffic on the local computer, and save certain parts of it as files - not log files though, but actual files that are sent to the computer as part of http or ftp. Basically if a user browse a page...
by: manasap | last post by:
Hi all! I've written a server and a client application using asynchronous sockets.The client sends data packets for every 7 seconds.The server receives the packets. This process proceeds smoothly without any errors but,sometimes I get the following error. "An existing connection was forcibly closed by the remote host." Why is this happening when i am not closing the client program? Could someone guide me in this issue
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.