473,788 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sockets: How to know when your data is sent

Hello,

I'm having a problem with sending data with sockets over a dial-up
connection. When I use the send function, it will happily send a
buffer of a megabyte and more in one shot. But of course, the data is
still in the network buffer... Meaning you can't disconnect for awhile
(but for how long...). The problem is, how can I know when it's done?
Is there a way to be notified when the data has truly been sent?

I tried using setsockopt and SO_SNDBUF to reduce the send buffer size,
thinking that the data would actually send in smaller increments,
instead of putting the whole thing in a buffer and taking it out of my
hands... No luck. It all works, mind you, but I'm left clueless as to
when the data is really off my computer!

I'm just following the example in the Python sockets How-To. Here's
the send function:

def mysend(msg):
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send( msg[totalsent:])
if sent == 0:
raise RuntimeError, \\
"socket connection broken"
totalsent = totalsent + sent

Is there something I'm missing, or is there a better way to do this?

Thanks,
-Marc
Jul 18 '05 #1
12 2306
On 2004-11-09, Marc Ederis <me*****@hotmai l.com> wrote:
I'm having a problem with sending data with sockets over a
dial-up connection. When I use the send function, it will
happily send a buffer of a megabyte and more in one shot. But
of course, the data is still in the network buffer... Meaning
you can't disconnect for awhile (but for how long...). The
problem is, how can I know when it's done? Is there a way to
be notified when the data has truly been sent?
What OS are you using?

Under Linux, there is no way to tell. IIRC, there's no way to
tell under BSD either. I actually wrote some kernel mode code
once upon a time to provide an ioctl() that I could call from
user space to find out when the data I had written to a socket
was actually sent.
Is there something I'm missing, or is there a better way to do
this?


The way to tell that data has been sent is to use an
application-level protocol that acknowleges the data transferr.

--
Grant Edwards grante Yow! Remember, in 2039,
at MOUSSE & PASTA will
visi.com be available ONLY by
prescription!!
Jul 18 '05 #2
Grant Edwards <gr****@visi.co m> wrote:
On 2004-11-09, Marc Ederis <me*****@hotmai l.com> wrote:
When I use the send function, it will
happily send a buffer of a megabyte and more in one shot. But
of course, the data is still in the network buffer...
[ ... ] The
problem is, how can I know when it's done?Under Linux, there is no way to tell. IIRC, there's no way to
tell under BSD either. I actually wrote some kernel mode code
once upon a time to provide an ioctl() that I could call from
user space to find out when the data I had written to a socket
was actually sent.


I may well be missing the point of the problem here, but wouldn't
just turning TCP_NODELAY on guarantee that send() won't return
until the last ACK has returned? And if you're not using TCP:
The way to tell that data has been sent is to use an
application-level protocol that acknowleges the data transferr.


applies in spades if you care about reliability.

--
\S -- si***@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Jul 18 '05 #3
On 2004-11-09, Sion Arrowsmith <si***@chiark.g reenend.org.uk> wrote:
When I use the send function, it will happily send a buffer of
a megabyte and more in one shot. But of course, the data is
still in the network buffer... [ ... ] The problem is, how
can I know when it's done?
Under Linux, there is no way to tell. IIRC, there's no way to
tell under BSD either. I actually wrote some kernel mode code
once upon a time to provide an ioctl() that I could call from
user space to find out when the data I had written to a socket
was actually sent.


I may well be missing the point of the problem here, but wouldn't
just turning TCP_NODELAY on guarantee that send() won't return
until the last ACK has returned?


Nope. Send returns as soon as the the data is copied to a a
kernel-space buffer. TCP_NODELAY has no effect on that. It
only changes the stack's behavior as it's taking data from that
buffer and putting it on the wire. Eventually, the TCP window
will fill up, and after than the tx buffer will fill up, and
after that, send will block. There's no practical way to
predict the size of either the TCP window or the tx buffer.
There's also no practical way to determine how much data is
still waiting to be acked (unless you want to do kernel
hacking).
And if you're not using TCP:
The way to tell that data has been sent is to use an
application-level protocol that acknowleges the data transferr.


applies in spades if you care about reliability.


Unfortunately, many people who write network applications (and
protocols) make all sorts of bizarre assumptions, and somebody
ends up jumping through hoops later to try to fix things when
those assumptions turn out to be wrong. :/

--
Grant Edwards grante Yow! I wonder if I should
at put myself in ESCROW!!
visi.com
Jul 18 '05 #4
Grant Edwards <gr****@visi.co m> wrote in message news:<41******* *************** *@visi.com>...
On 2004-11-09, Sion Arrowsmith <si***@chiark.g reenend.org.uk> wrote:
When I use the send function, it will happily send a buffer of
a megabyte and more in one shot. But of course, the data is
still in the network buffer... [ ... ] The problem is, how
can I know when it's done?

Under Linux, there is no way to tell. IIRC, there's no way to
tell under BSD either. I actually wrote some kernel mode code
once upon a time to provide an ioctl() that I could call from
user space to find out when the data I had written to a socket
was actually sent.


I may well be missing the point of the problem here, but wouldn't
just turning TCP_NODELAY on guarantee that send() won't return
until the last ACK has returned?


Nope. Send returns as soon as the the data is copied to a a
kernel-space buffer. TCP_NODELAY has no effect on that. It
only changes the stack's behavior as it's taking data from that
buffer and putting it on the wire. Eventually, the TCP window
will fill up, and after than the tx buffer will fill up, and
after that, send will block. There's no practical way to
predict the size of either the TCP window or the tx buffer.
There's also no practical way to determine how much data is
still waiting to be acked (unless you want to do kernel
hacking).
And if you're not using TCP:
The way to tell that data has been sent is to use an
application-level protocol that acknowleges the data transferr.


applies in spades if you care about reliability.


Unfortunately, many people who write network applications (and
protocols) make all sorts of bizarre assumptions, and somebody
ends up jumping through hoops later to try to fix things when
those assumptions turn out to be wrong. :/

I see, so the right way to do it is to have the server respond on a
successful receipt of the data. Thanks Grant. And thanks for
mentioning TCP_NODELAY, Sion, because I was about to try that too, but
this saves me the trouble.

-Marc
Jul 18 '05 #5
On 09 Nov 2004 17:26:03 GMT, Grant Edwards <gr****@visi.co m> wrote:
[ snip! ]
Unfortunately, many people who write network applications (and
protocols) make all sorts of bizarre assumptions, and somebody
ends up jumping through hoops later to try to fix things when
those assumptions turn out to be wrong. :/


Woah! This is all enlightening news to me. Of course, what little
bit of socket level stuff I've done, I've always had a
"send/acknowledge" type protocol wrapped around. I guess I've just
been lucky so far :-).

jw
Jul 18 '05 #6
On 2004-11-10, Jaime Wyant <pr***********@ gmail.com> wrote:
On 09 Nov 2004 17:26:03 GMT, Grant Edwards <gr****@visi.co m> wrote:
[ snip! ]
Unfortunately, many people who write network applications (and
protocols) make all sorts of bizarre assumptions, and somebody
ends up jumping through hoops later to try to fix things when
those assumptions turn out to be wrong. :/


Woah! This is all enlightening news to me. Of course, what little
bit of socket level stuff I've done, I've always had a
"send/acknowledge" type protocol wrapped around. I guess I've just
been lucky so far :-).


While we're on the topic, another common problem occurs when
people assume that there is a 1:1 correspondance between
write() and read() operations when using TCP. IOW, they assume
that if they write() (or send()) a block of data of size N,
that it will arrive at the other end as a block and be read as
a block of size N. Under some conditions, this will often be
true. I ran into one app that worked under this assumption for
years, but broken when a satellite link was placed in the path.

--
Grant Edwards grante Yow! With YOU, I can be
at MYSELF... We don't NEED
visi.com Dan Rather...
Jul 18 '05 #7

[Marc]
When I use the send function, it will
happily send a buffer of a megabyte and more in one shot. But
of course, the data is still in the network buffer... Meaning
you can't disconnect for awhile (but for how long...). The
problem is, how can I know when it's done? Is there a way to
be notified when the data has truly been sent?
[Grant] Under Linux, there is no way to tell. IIRC, there's no way to
tell under BSD either. [...]
The way to tell that data has been sent is to use an
application-level protocol that acknowleges the data transferr.


If you want to ensure that all the data has been sent before *closing* the
socket, which sounds like Marc's requirement, you can use
setsockopt(SO_L INGER):

SO_LINGER
Sets or gets the SO_LINGER option. The argument is
a linger structure.

struct linger {
int l_onoff; /* linger active */
int l_linger; /* how many seconds to linger for */
};

When enabled, a close(2) or shutdown(2) will not
return until all queued messages for the socket
have been successfully sent or the linger timeout
has been reached. Otherwise, the call returns imme*
diately and the closing is done in the background.
When the socket is closed as part of exit(2), it
always lingers in the background.

(from the GNU manpages). I don't believe that's true for shutdown() on
all platforms, but I do believe it's true for close() / closesocket().
Calling it from Python is a bit of a chore involving the struct module,
and is left as an exercise for the reader. 8-)

--
Richie Hindle
ri****@entrian. com

Jul 18 '05 #8
Woah! This is all enlightening news to me. Of course, what little
bit of socket level stuff I've done, I've always had a
"send/acknowledge" type protocol wrapped around. I guess I've just
been lucky so far :-).

Oh yeah. I did the same and I realized that the response time can be
very long.
I had to reconsider my protocol, and use as few send/ack pairs as possible.
No matter if you have a broadband connection or not - it can be VERY slow
if your protocol uses many send/ack pairs.
Jul 18 '05 #9
On 2004-11-10, Gandalf <ga*****@geoche msource.com> wrote:
Woah! This is all enlightening news to me. Of course, what
little bit of socket level stuff I've done, I've always had a
"send/acknowledge" type protocol wrapped around. I guess I've
just been lucky so far :-).
Oh yeah. I did the same and I realized that the response time
can be very long.


For a badly designed protocol, it can.
I had to reconsider my protocol, and use as few send/ack pairs
as possible. No matter if you have a broadband connection or
not - it can be VERY slow if your protocol uses many send/ack
pairs.


The answer is to use a sliding window protocol.

--
Grant Edwards grante Yow! I'm having a
at RELIGIOUS EXPERIENCE... and
visi.com I don't take any DRUGS
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3710
by: Robert A. van Ginkel | last post by:
In news:OZ0W9RsdDHA.2432@TK2MSFTNGP10.phx.gbl... I ask the question how I can see if all the data is on the other side of the connection. I got as answer that I should use the blocking property. I tried this I don't see any diffents, I am sending 10Mb and the Send/BeginSend command doesn't wait till the data is on the remotepoint. Can somebody pls. explain this. Regards Robert.
6
7002
by: Laxmikant Rashinkar | last post by:
Is there any way to use a C# socket in promiscuous mode? Any sample code that shows how this is done? any assistance is much appreciated! thanks LK
0
2352
by: Stuart Norris | last post by:
Dear Group, I am having a problem setting SocketOptionName.SendTimeout on a client TCPIP application using the sockets in .NET. From the on-line help it is possible to set a SocketOptionName.SendTimeout for sends on TCPIP sockets. In all the tests that I have done with both Async and Sync sends the send returns immediately with the number of bytes sent (< 100 in my messages).
3
4364
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly found out that .net uses system.net.sockets.socket or .tcpclient/.tcpserver. and these confuse me... :o| and help would be really great! links, code, wrappers, tips, whateveryougot... one of my main problems, i guess, is: why don't sockets...
2
6672
by: a_agaga | last post by:
Do you know are there some reasons why many do not make processes to communicate through memory? Why network connections (sockets) are used so commonly in IPC (inter process communication) instead of memory? (Is IPC harder to maintain / handle if it is made through memory, when compared to communication through sockets?) Some background things:
14
11927
by: eliss.carmine | last post by:
I'm using TCP/IP to send a Bitmap object over Sockets. This is my first time using C# at all so I don't know if this is the "right" way to do it. I've already found out several times the way I was doing something was really inefficient and could reduce 10 lines of code with 2, etc. For reading, I am using a TcpClient and I call NetworkStream ns = client.GetStream(); to get a stream stream.Read(buffer, 0, buffer.Length);
5
2780
by: Cichy | last post by:
Hello, I'm writing a Client-Server application using sockets (asynchronous). There is a Server (Master) which accepts incoming connections, and Client (Slave). Afetr establishing connections with all Slaves I wanna hit a button "Automatic", then everything must be reorganised, there is an ellection for a new Master. Everything is all right when I'm connecting manually (when I hit a button connect), but after hitting this button...
0
9498
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,...
0
10177
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10118
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,...
0
9969
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
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, 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...
0
6750
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();...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4074
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
3
2897
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 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.