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

Socket Programming - Receive Buffer Length

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 OnSocketConnectCompleted 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[]>();
  6.  
  7.             DnsEndPoint endPoint = new DnsEndPoint("111.222.333.444", port);
  8.             Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  9.  
  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.         }
  16.  
  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.         }
  25.  
  26.         private void OnSocketReceive(object sender, SocketAsyncEventArgs e)
  27.         {
  28.             Q.Enqueue(e.Buffer);
  29.  
  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 11217
Plater
7,872 Expert 4TB
OnSocketConnectCompleted is fired when a connection is established. Data transfer has nothing to do with it
Nov 24 '09 #2
@Plater
Right but I did not ask anything about what causes the OnSocketConnectCompleted event to fire. Please reread the question, I asked "what causes the completed' event in the OnSocketConnectCompleted 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
Plater
7,872 Expert 4TB
@danomano
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
Frinavale
9,735 Expert Mod 8TB
@danomano
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 OnSocketConnectCompleted 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

-Frinny
Nov 25 '09 #5

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

Similar topics

4
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...
5
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...
13
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...
9
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...
3
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...
0
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...
2
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...
0
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...
2
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...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.