By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

[QQ] Setting SendTimeout on TCPIP Sockets

P: n/a
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).

However the send may not have actually completed sending the data at
this time.

In the example below I connect to a server and then remove the
ethernet cable from the server and press the send button on my
application. I expect since the send has not delivered the packet to
the server that the send would time-out and an exception would occur.

However what actually happens is the send returns immediately
reporting that the data has been sent. I can see from a protocol
analyser that Windows tries to resend the data several times.

In my application I need to know that the data has not been sent as
after the timeout period as it is not valid any more. I do not want
windows to try to continue sending it after this timeout period.l

How can I detect that the data has not actually been sent within the
timeout period to the remote server?

How does Windows tell my TCPIP application that the data was not sent?
Is there an event that I need to look for?

Does Windows store the data in an internal buffer then forward the
messages from my application? Is it possible to inquire into this
buffer that the data has been delivered or timed-out to the remote
server.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;

namespace simplesend
{
public class Form1 : System.Windows.Forms.Form
{
private Socket tcpsocket;
private System.Windows.Forms.Button Connect;
private System.Windows.Forms.Button Send;
private System.Windows.Forms.Label ConnectionStatus;
private System.Windows.Forms.Label SendStatus;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.Connect = new System.Windows.Forms.Button();
this.Send = new System.Windows.Forms.Button();
this.ConnectionStatus = new System.Windows.Forms.Label();
this.SendStatus = new System.Windows.Forms.Label();
this.SuspendLayout();

this.Connect.Location = new System.Drawing.Point(80, 16);
this.Connect.Name = "Connect";
this.Connect.Size = new System.Drawing.Size(128, 48);
this.Connect.TabIndex = 2;
this.Connect.Text = "Connect";
this.Connect.Click += new System.EventHandler(this.Connect_Click);

this.Send.Location = new System.Drawing.Point(72, 128);
this.Send.Name = "Send";
this.Send.Size = new System.Drawing.Size(136, 56);
this.Send.TabIndex = 3;
this.Send.Text = "Send";
this.Send.Click += new System.EventHandler(this.Send_Click);

this.ConnectionStatus.Location = new System.Drawing.Point(88, 72);
this.ConnectionStatus.Name = "ConnectionStatus";
this.ConnectionStatus.Size = new System.Drawing.Size(112, 24);
this.ConnectionStatus.TabIndex = 4;
this.ConnectionStatus.Text = "Not Connected";

this.SendStatus.Location = new System.Drawing.Point(88, 200);
this.SendStatus.Name = "SendStatus";
this.SendStatus.Size = new System.Drawing.Size(120, 16);
this.SendStatus.TabIndex = 5;
this.SendStatus.Text = "Nothing Sent";

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.SendStatus);
this.Controls.Add(this.ConnectionStatus);
this.Controls.Add(this.Send);
this.Controls.Add(this.Connect);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Connect_Click(object sender, System.EventArgs e)
{
tcpsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
tcpsocket.SetSocketOption(SocketOptionLevel.Socket ,
SocketOptionName.SendTimeout, 5000);
IPEndPoint remoteServerEndPoint = new IPEndPoint(
IPAddress.Parse("10.1.1.1"), 5000 );
tcpsocket.Blocking = true;
try
{
tcpsocket.Connect(remoteServerEndPoint);
}
catch (SocketException socketException)
{
socketException = socketException;
return;
}
if (tcpsocket.Connected) ConnectionStatus.Text = "Connected";
}

private void Send_Click(object sender, System.EventArgs e)
{
Byte[] buffer = new byte[5];
buffer[0] = System.Convert.ToByte('H');
buffer[1] = System.Convert.ToByte('e');
buffer[2] = System.Convert.ToByte('l');
buffer[3] = System.Convert.ToByte('l');
buffer[4] = System.Convert.ToByte('o');
int numBytes = 0;
try
{
numBytes = tcpsocket.Send (buffer, SocketFlags.None);
if (tcpsocket.Connected) SendStatus.Text = numBytes.ToString();
}
catch (SocketException socketException)
{
socketException = socketException;
}
}
}
}
Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.