Click here to Skip to main content
15,910,121 members
Home / Discussions / C#
   

C#

 
GeneralRe: Simple XML problem Pin
leppie18-May-05 9:40
leppie18-May-05 9:40 
GeneralRe: Simple XML problem Pin
DavidNohejl18-May-05 10:28
DavidNohejl18-May-05 10:28 
GeneralRe: Simple XML problem Pin
Snowjim18-May-05 11:26
Snowjim18-May-05 11:26 
GeneralRe: Simple XML problem Pin
DavidNohejl18-May-05 12:34
DavidNohejl18-May-05 12:34 
GeneralRe: Simple XML problem Pin
Snowjim18-May-05 12:44
Snowjim18-May-05 12:44 
GeneralRe: Simple XML problem Pin
DavidNohejl18-May-05 13:26
DavidNohejl18-May-05 13:26 
GeneralRe: Simple XML problem Pin
Snowjim18-May-05 13:31
Snowjim18-May-05 13:31 
GeneralReusing a local port when sending from a bound socket Pin
ghassett2218-May-05 7:28
ghassett2218-May-05 7:28 
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;<br />
using System.IO;<br />
using System.Net;<br />
using System.Net.Sockets;<br />
using System.Text;<br />
using System.Threading;<br />
<br />
class TcpClientTest <br />
{<br />
    static public void Main() <br />
    {<br />
        bool bindIt = true;<br />
<br />
        IPHostEntry localHostEntry = Dns.GetHostByName(Dns.GetHostName());<br />
        IPEndPoint  localIPEndPoint = new IPEndPoint (localHostEntry.AddressList[0], 8801);<br />
<br />
        while (true)<br />
        {<br />
            try<br />
            {<br />
                Console.WriteLine("Press return to send data");<br />
                Console.ReadLine();<br />
<br />
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);<br />
<br />
                socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);<br />
                socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(false, 0));<br />
                socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 1);<br />
<br />
                if (bindIt)<br />
                {<br />
                    socket.Bind (new IPEndPoint (IPAddress.Any, 8801));<br />
                }<br />
<br />
                socket.Connect (new IPEndPoint(IPAddress.Parse("192.168.2.10"), 8801));<br />
<br />
                byte[] buf = Encoding.ASCII.GetBytes (DateTime.Now.ToString());<br />
                socket.Send (buf);<br />
<br />
                socket.Shutdown (SocketShutdown.Both);<br />
                socket.Close ();<br />
<br />
                Console.Out.WriteLine ("Sent");<br />
            }<br />
            catch (Exception ex)<br />
            {<br />
                Console.Out.WriteLine (ex.ToString());<br />
            }<br />
        }<br />
    }<br />
}<br />

QuestionC# 'static' variables on the web? Pin
LizardWiz18-May-05 7:05
LizardWiz18-May-05 7:05 
AnswerRe: C# 'static' variables on the web? Pin
S. Senthil Kumar18-May-05 7:24
S. Senthil Kumar18-May-05 7:24 
AnswerRe: C# 'static' variables on the web? Pin
S Sansanwal18-May-05 12:09
S Sansanwal18-May-05 12:09 
AnswerRe: C# 'static' variables on the web? Pin
Joshua Nussbaum18-May-05 14:21
Joshua Nussbaum18-May-05 14:21 
GeneralWebService DataSet Help Pin
Rougy18-May-05 5:29
Rougy18-May-05 5:29 
GeneralRe: WebService DataSet Help Pin
Robert Rohde18-May-05 8:40
Robert Rohde18-May-05 8:40 
GeneralRe: WebService DataSet Help Pin
Rougy19-May-05 4:10
Rougy19-May-05 4:10 
GeneralRe: WebService DataSet Help Pin
Rougy19-May-05 4:15
Rougy19-May-05 4:15 
GeneralRe: WebService DataSet Help Pin
Robert Rohde19-May-05 6:16
Robert Rohde19-May-05 6:16 
GeneralrichTextBox keyPress event problem Pin
pessen18-May-05 5:27
pessen18-May-05 5:27 
GeneralRe: richTextBox keyPress event problem Pin
S. Senthil Kumar18-May-05 5:42
S. Senthil Kumar18-May-05 5:42 
GeneralRe: richTextBox keyPress event problem Pin
pessen18-May-05 5:53
pessen18-May-05 5:53 
GeneralProblem in Opening connection in SQL Server Pin
Murtuza Husain Miyan Patel18-May-05 5:10
professionalMurtuza Husain Miyan Patel18-May-05 5:10 
GeneralRe: Problem in Opening connection in SQL Server Pin
Marc Clifton18-May-05 5:51
mvaMarc Clifton18-May-05 5:51 
GeneralRe: Problem in Opening connection in SQL Server Pin
Tom Larsen18-May-05 10:49
Tom Larsen18-May-05 10:49 
GeneralC# equivelant of Eval() Pin
JMichael246818-May-05 4:55
JMichael246818-May-05 4:55 
GeneralRe: C# equivelant of Eval() Pin
Marc Clifton18-May-05 5:47
mvaMarc Clifton18-May-05 5:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.