473,547 Members | 2,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determining the size of data being sent from Socket Server

Frinavale
9,735 Recognized Expert Moderator Expert
I'm implementing a Silverlight application that uses Sockets to receive data that is pushed to it from a Socket Server. (Silverlight only supports the TCP protocol)

The Socket Server pushes a series of images to the Silverlight application which displays them. Every 2 seconds the next image in the series is sent until all of the images have been sent, at which time the Socket Server just starts over again.

Everything's working fine right now but that's only because I've made the receive buffer size for the SocketAsyncEven tArgs large enough to accept the whole image file.

When the receive buffer size is smaller than the file more than one packet is sent from the server to the client until the whole file's been sent.

My question is: how do I know how large the file is that's being sent?


Thanks for your time,

-Frinny
Jul 13 '09 #1
38 8933
Plater
7,872 Recognized Expert Expert
Do you control the file sending? Send a datasize first?
Jul 13 '09 #2
Frinavale
9,735 Recognized Expert Moderator Expert
Thanks Plater,

Yes I control both sides.

I have thought of that solution too but it seems that there should be another way to do this....

The BeginSend method even takes a parameter that indicates the size of the data being sent.

Why can't I see this value client side?

I was actually thinking about serializing each packet so that the size would be included at the beginning....I was reading an article on this last week but when I tried access it this morning it wasn't there any more.....

Maybe this serialization thing isn't the best idea?

-Frinny
Jul 13 '09 #3
Plater
7,872 Recognized Expert Expert
Well msdn.com and my local msdn library both seem to be broken when it comes to looking up that SocketAsyncEven tArgs object.
If you have access to the socket, you can see how much data is currently on the stream though, that might help a bit?
I generally avoid using ASync stuff for these very "grouping" reasons.
Jul 13 '09 #4
Frinavale
9,735 Recognized Expert Moderator Expert
@Plater
I have no choice but to use the async stuff because Silverlight requires it.

I have access to the socket, and I can see how much data is currently on the stream but I have no idea how much data's supposed to be sent....how big the end result is supposed to be...so I have no idea how large to grow my "data collection" buffer or when the file actually ends and new data begins. I'm quite a newbie when it comes to Sockets so I might even be over looking a property/method that tells me this...but I did go through every single property/method intellisense displays looking for anything that could help me out.

-Frinny
Jul 13 '09 #5
Plater
7,872 Recognized Expert Expert
Well sending a header with the datasize is still my recomended action. This backwards client/server business boggles my mind (I prefer the polling process)
If you send the datasize first, your async thing can go "Ok I'm getting this many bytes" and you can keep appending the bytes to some sort of object until you have them all and create you image object from it.

How do you currently know what seperates image A from image B?
Jul 13 '09 #6
Frinavale
9,735 Recognized Expert Moderator Expert
@Plater
The whole image fits into the receive buffer right now because I've made the buffer large enough to accommodate the whole file... so it's pretty simple, when I receive the data (in the OnReceive event) I know that it's the whole file.

Right now, for practice, the Socket Server sending static image files (images that exist in a folder) to the client. This is how I was able to determine how big to make the buffer (just used the largest image size); however, in my real world application the socket server is going to be receiving raw byte data from a physical device and pushing that to the Silverlight application. So you can understand why it's important that I some how be able to tell how big the data is.

@Plater
I have no idea how to do this...what should I be keywords would you recommend that I research with to find the answer?

@Plater
Would you do this in a header or in a separate "init" push to the client?
Jul 13 '09 #7
Plater
7,872 Recognized Expert Expert
However you want really. You could steal the idea from how http works with a robust header system (you can include things like the name of the image, the size, the type, date modified, any meta-data, etc) or just something simple like the first 8 bytes represent a size and anything after it are bytes for the image.

What happens when a 2nd image is sent before the first one is done being received? Wouldn't the OnReceive events inter-mix with each other? There mose be some way to tell them apart? Perhaps the remote port from the Socket object could tell you which "image" the receive event belongs to?
Jul 13 '09 #8
Frinavale
9,735 Recognized Expert Moderator Expert
@Plater
I think I'll try sending the file information first and then send the file because at least I understand how that works...I don't know how to get at the header information using the SocketAsyncEven tArgs right now and google's not helping me today.

@Plater
I've thought about this problem too but I'm not ready to solve it yet. That's why I have the timer for 2 seconds...and sometimes I extend this

When I'm debugging and have the client paused for more than 2 seconds the server throws an exception saying something or other...I'm not sure what happens when more than one file is sent within milliseconds of each other yet...I'm sure that the packets are going to be all mixed up. Asynchronous Socekts are pretty hard to work with.

I'll look at that later. First I just want to figure out how big the expected data is. Then the bigger can of worms will be opened.

One step at a time :)

-Frinny
Jul 13 '09 #9
Frinavale
9,735 Recognized Expert Moderator Expert
@Plater
On second thought I think I know how I'll be able to tell them apart......I Think.

When the application calls BeginSend, it will use a separate thread to execute the specified callback method, and will block on EndSend until the Socket sends the number of bytes requested.

I would assume that if a second file were to be sent before the first file it would be queued. I don't know for sure though.

This would kind of defeat the purpose of sending via asyc though.
I'll have to look into this later.

Err, I'm off track now...back to the original problem: determining the size of the file.
Jul 13 '09 #10

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

Similar topics

0
2332
by: anuradha.k.r | last post by:
hi, I 'v e written both the server and client side socket programs in perl( a very small one).My socket is created and shows that the connection is established with the client.however I am not able to receive data in server side.I think the client sends the data properly.Somehow receive does not happen. Can someone tell me what could be...
0
1685
by: Johann Blake | last post by:
I am using the TcpClient to connect to a web site. I then open a NetworkStream and read the contents that are being sent back. The problem is that I have no idea when the remote host is finished sending data. It is sending a web page but it doesn't use the Content-Length header to indicate the size. While I can use the DataAvailable...
4
8193
by: yaron | last post by:
Hi, I have a problem when sending data over TCP socket from c# client to java server. the connection established ok, but i can't send data from c# client to java server. it's work ok with TcpClient, NetworkStream and StreamWriter classes. but with low level socket it doesn't work (When using the Socket class Send method).
7
3928
by: Crirus | last post by:
Hi all! I use a webClient for requesting data from a server of mine. Should I worry about long ammount of data sent by server in the client side? Or, another way, should I send some kind of a terminator sign in the data in order to realise in the client that all data arrived? -- Ceers, Crirus
2
4652
by: Macca | last post by:
Hi, My application uses an asynchronous socket server. The question I have is what i should set my socket server buffer size to. I will know the size of each data packet sent across the network and was considering setting the buffer size to this. In the examples I have seen on the net the buffer size is usually set to 1024 bytes and...
16
8692
by: Matthew Geyer | last post by:
been using asynchronous sockets with AsyncCallback and System.Net.Sockets as per the excellent MSDN example. everything has been working great, except when i want to receive more total bytes than my buffer can hold. i open a connection with an 8k buffer on the socket. if the server sends me 5k and closes connection it works fine. if the...
10
34468
by: John Salerno | last post by:
I wrote some pretty basic socket programming again, but I'm still confused about what's happening with the buffer_size variable. Here are the server and client programs: -------------- from socket import * host = '' port = 51567 address = (host, port) buffer_size = 1024
4
7052
by: Zytan | last post by:
This may be the dumbest question of all time, but... When I set the packet size, does it mean ALL packets are that size, no matter what? Let's say the packet size is 8KB, and I send a 5 byte "hello", will it cause 8KB of bandwidth, or 5 bytes (plus TCP/IP packet header, as well, of course). (Btw, I 'set' the packet size via...
1
9701
by: starter08 | last post by:
Hi, I have a C++ routine(client-side) which uploads an xml file to a web server by making a socket connection and sending all the post request through that socket. On the server side I have a cgi script which receives all the data and creates a file in the specified directory. If I am uploading only the file all works well, however I want to...
0
7437
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...
0
7703
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, 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. ...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7797
tracyyun
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...
1
5362
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3473
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1923
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
748
bsmnconsultancy
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...

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.