473,386 Members | 1,706 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.

Reusing a local address/port when creating sockets that send data

Hello,

I want to periodically send a TCP packet to a peer, always from the same
source port. That is, each packet will come from my local ip address, port
8801, and go to the peer's ip address, to HIS port 8801.

This means that I need to bind my sender socket to (myaddress,8801), use it
for the send, then shut it down and close it. Then I want to wait, say 30
seconds, and do it all over again. Naturally, the socket's ReuseAddress
property must be set for this to work.

However, in practice, the second time I bind a socket to my local endpoint,
I get a SocketException: "Only one usage of each socket address
(protocol/network address/port) is normally permitted."

Can anyone help me? The following code attempts to send a string containing
the date/time to a server on 192.168.2.10:8810. If I set the "bindIt"
variable to false, the code works fine. If I try to bind (and re-bind) the
socket to the same local endpoint (e.g, I've set bindIt to true), the code
fails with the above exception. Help!

Thanks

Greg Hassett

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

class TcpClientTest
{
static public void Main()
{
bool bindIt = true;

IPHostEntry localHostEntry = Dns.GetHostByName(Dns.GetHostName());
IPEndPoint localIPEndPoint = new IPEndPoint
(localHostEntry.AddressList[0], 8801);

while (true)
{
try
{
Console.WriteLine("Press return to send data");
Console.ReadLine();

Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

socket.SetSocketOption (SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
socket.SetSocketOption (SocketOptionLevel.Socket,
SocketOptionName.Linger, new LingerOption(false, 0));
socket.SetSocketOption (SocketOptionLevel.Socket,
SocketOptionName.DontLinger, 1);

if (bindIt)
{
socket.Bind (new IPEndPoint (IPAddress.Any, 8801));
}

socket.Connect (new
IPEndPoint(IPAddress.Parse("192.168.2.10"), 8801));

byte[] buf = Encoding.ASCII.GetBytes
(DateTime.Now.ToString());
socket.Send (buf);

socket.Shutdown (SocketShutdown.Both);
socket.Close ();

Console.Out.WriteLine ("Sent");
}
catch (Exception ex)
{
Console.Out.WriteLine (ex.ToString());
}
}
}
}

Nov 17 '05 #1
0 2985

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

Similar topics

2
by: Jo Voordeckers | last post by:
Hello all, I'm pretty new to the Java newsgroups so I apologize for dropping this into several maybe offtopic groups. I'm sorry! So on to my problem... I've come to a point in our RMI...
4
by: John Morgan | last post by:
I have Enterprise Manager on my local machine. For the last twelve months it has been connecting without problem to my online SQL Server database provided by my ISP. Three weeks ago the ISP...
7
by: Rex Winn | last post by:
I've Googled until my eyes hurt looking for a way to issue Telnet commands from C# and cannot find anything but $300 libraries that encapsulate it for you. I don't want to be able to create a...
3
by: D. André Dhondt | last post by:
In VB.NET 2003, is there a way to create a System.Net.Sockets.UDPClient to listen to any address AND any port? I can get it to listen to any address, but only if I specify a port (for example,...
2
by: Marty | last post by:
Hi, When we create a new socket and make it connect to a remote endpoint, how can we tell to the socket to use a specific IP address on the local computer, when multiples IPs are enabled. ...
7
by: Sharon | last post by:
Hi all, I've implemented a TCP server using the Socket async methods. When connecting to the server from 3 instances of hyper terminal, i've noticed that each of the newly created server sockets,...
2
by: jasonsgeiger | last post by:
From: "Factor" <jasonsgeiger@gmail.com> Newsgroups: microsoft.public.in.csharp Subject: Multiple Clients, One port Date: Wed, 19 Apr 2006 09:36:02 -0700 I'm been working with sockets for a...
1
by: jcprince | last post by:
Hi Not sure I can do what I'm trying to do without using a 3rd party component like Dart. I need to build a windows service to create a socket connection on an IBM mainframe using an IP and port...
0
by: Xionbox | last post by:
Hello everybody, The error I have seems very easy to solve, but for some odd reason I can't seem to solve it. Anyways, here's my "setup". I created a server running on localhost:1200 (telnet...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.