473,398 Members | 2,113 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,398 software developers and data experts.

[QQ] Setting SendTimeout on TCPIP Sockets

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
0 2330

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

Similar topics

3
by: Bob Harrison | last post by:
I will be distributing an application that works with our security panel. The application can be loaded on several workstations in a network but only one workstation actually communicates via...
2
by: Milosz - [playseven.com] | last post by:
Dim EP As System.Net.IPEndPoint = New System.Net.IPEndPoint(mIP, mPort) Dim S As Socket = New Socket(EP.AddressFamily, SocketType.Dgram, ProtocolType.Udp) ...
5
by: vooose | last post by:
Consider a class MyTcpClient that : from TcpClient... MyTcpClient tcpClient = new MyTcpClient(); tcpClient.Client = tcpListener.AcceptSocket(); tcpClient.SendTimeout = 5000; //5 seconds ...
1
by: Mike Sharp | last post by:
I'm fairly new to the C# scene, having been a VB6 programmer for what seems like a age (I used to push VB6 to its absolute limits when handling numerous connections from TCPIP sources). I'm...
0
by: notregister | last post by:
Ji, i tried to use tcpclient to send a series of PCL command to a printer, but after going thru this, it print out a series of junk...the same series work well with another code i written for...
5
by: bizt | last post by:
Hi, I have been asked by my manager to look into the possiblily of setting up an online chat system for the external website. We run an Apache server with PHP (hence the reason Im posting here)...
1
by: DaTurk | last post by:
Hi, Lets see, for arguements sake lets just say that I have a server, which site waiting to receive connections, it then has an array of sockets that are connected to it. It's receiving all of...
6
by: Hemant Shah | last post by:
Folks, I am having hard time figuring out the problem I have with setting up new DB2 instance. This is 22nd instance I am creating on the server and I have never encounterd this problem. My...
1
by: user1357 | last post by:
Hi All, Could you please explain the difference b/w the below codes? when qq is used with eval and without () the o/p is different. $hex1='\xd1';
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
0
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...
0
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,...
0
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...

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.