Click here to Skip to main content
15,887,683 members
Home / Discussions / C#
   

C#

 
GeneralRe: windows 8 hybrid shutdown notification Pin
morglorf3-Feb-15 5:54
morglorf3-Feb-15 5:54 
QuestionHelp with Array search. Pin
Member 114193361-Feb-15 18:18
Member 114193361-Feb-15 18:18 
AnswerRe: Help with Array search. Pin
Richard Andrew x641-Feb-15 18:34
professionalRichard Andrew x641-Feb-15 18:34 
GeneralRe: Help with Array search. Pin
Member 114193361-Feb-15 19:28
Member 114193361-Feb-15 19:28 
AnswerRe: Help with Array search. Pin
BillWoodruff1-Feb-15 21:16
professionalBillWoodruff1-Feb-15 21:16 
AnswerRe: Help with Array search. Pin
Richard Deeming2-Feb-15 2:53
mveRichard Deeming2-Feb-15 2:53 
AnswerRe: Help with Array search. Pin
ramazan aktolu3-Feb-15 7:36
ramazan aktolu3-Feb-15 7:36 
QuestionTrouble binding with TCP to same local endpoint Pin
Mc_Topaz1-Feb-15 4:07
Mc_Topaz1-Feb-15 4:07 
I need to bind a TCP-client to a specific IP-address and port. The Socket.Bind() method seems to be the way to do this. But I stumble over a problem when I try to bind and connect to the same local and remote end points.

This is what I do:
1) The TCP-client binds to the local endpoint: 172.168.0.10:20001.
2) The TCP-client connects to the remote endpoint: 172.168.0.18:10001.
3) The TCP-client send a request to the TCP-server.
4) Wait some arbitrary time so the TCP-server can respond.
5) The TCP-client receives the respond from the TCP-server.
6) The TCP-client calls the Shutdown and Close methods.

This works, one time.
If I immediately after step 6 start all over from step 1; my applications throws an SocketException at step 2.

The SocketException says:
ErrorCode: 10048
SocketErrorCode: AddressAlreadyInUse
Message: Only one usage of each socket address (protocol/network address/port) is normally permitted 172.168.0.18:10002
MSDN about ErrorCode 10048[^]

Some other information
* I cannot keep the connection with the TCP-server open, this is why I always close after I have received something.
* What I make of it; is that I don't seem to close/shutdown/terminate the socket properly at step 6 for the particular local endpoint.
* If I change the local endpoint's port to 20002 I can restart the sequence without any problems. This works only once as well.
* I have also tried the SetSocketOption with the ResuseAddress enum, but that don't seem to solve the issue.

Any suggestions?


My code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IPEndPoint local1 = new IPEndPoint(IPAddress.Parse("172.168.0.10"), 20001);
            IPEndPoint local2 = new IPEndPoint(IPAddress.Parse("172.168.0.10"), 20002);
            IPEndPoint remote = new IPEndPoint(IPAddress.Parse("172.168.0.18"), 10002);
            byte[] request = new byte[] { 0x10, 0x7B, 0xFE, 0x79, 0x16 };

            Test(local1, remote, request); // Works
            Test(local1, remote, request); // Failes
            Test(local2, remote, request); // Works
        }

        static void Test(IPEndPoint local, IPEndPoint remote, byte[] request)
        {
            byte[] response = new byte[0];
            TcpClient tcp = new TcpClient();
            NetworkStream stream = null;

            try
            {
                tcp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                tcp.Client.Bind(local);
                tcp.Connect(remote);

                stream = tcp.GetStream();
                stream.Write(request, 0, request.Length);

                Thread.Sleep(500);

                response = new byte[tcp.Available];
                stream.Read(response, 0, response.Length);
            }
            catch (SocketException soe)
            {
                Console.WriteLine(soe.ErrorCode);
                Console.WriteLine(soe.SocketErrorCode);
                Console.WriteLine(soe.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (tcp.Connected)
                {
                    tcp.Client.Shutdown(SocketShutdown.Both);
                }

                tcp.Close();

                if (stream != null)
                {
                    stream.Close();
                }
            }

            Console.WriteLine("Received: {0} byte(s)", response.Length);
        }
    }
}

AnswerRe: Trouble binding with TCP to same local endpoint Pin
Richard MacCutchan1-Feb-15 5:22
mveRichard MacCutchan1-Feb-15 5:22 
GeneralRe: Trouble binding with TCP to same local endpoint Pin
Mc_Topaz1-Feb-15 5:27
Mc_Topaz1-Feb-15 5:27 
GeneralRe: Trouble binding with TCP to same local endpoint Pin
Richard MacCutchan1-Feb-15 6:05
mveRichard MacCutchan1-Feb-15 6:05 
GeneralRe: Trouble binding with TCP to same local endpoint Pin
Mc_Topaz1-Feb-15 6:43
Mc_Topaz1-Feb-15 6:43 
GeneralRe: Trouble binding with TCP to same local endpoint Pin
OriginalGriff1-Feb-15 7:43
mveOriginalGriff1-Feb-15 7:43 
GeneralRe: Trouble binding with TCP to same local endpoint Pin
OriginalGriff1-Feb-15 5:38
mveOriginalGriff1-Feb-15 5:38 
GeneralRe: Trouble binding with TCP to same local endpoint Pin
Richard MacCutchan1-Feb-15 6:05
mveRichard MacCutchan1-Feb-15 6:05 
NewsRe: Trouble binding with TCP to same local endpoint Pin
Mc_Topaz1-Feb-15 6:39
Mc_Topaz1-Feb-15 6:39 
AnswerRe: Trouble binding with TCP to same local endpoint Pin
Pete O'Hanlon2-Feb-15 6:31
mvePete O'Hanlon2-Feb-15 6:31 
GeneralRe: Trouble binding with TCP to same local endpoint Pin
Mc_Topaz2-Feb-15 19:57
Mc_Topaz2-Feb-15 19:57 
GeneralRe: Trouble binding with TCP to same local endpoint Pin
Pete O'Hanlon2-Feb-15 23:18
mvePete O'Hanlon2-Feb-15 23:18 
GeneralRe: Trouble binding with TCP to same local endpoint Pin
Mc_Topaz2-Feb-15 23:43
Mc_Topaz2-Feb-15 23:43 
AnswerRe: Trouble binding with TCP to same local endpoint Pin
DelphiCoder5-Feb-15 20:12
DelphiCoder5-Feb-15 20:12 
GeneralRe: Trouble binding with TCP to same local endpoint Pin
Mc_Topaz8-Feb-15 8:00
Mc_Topaz8-Feb-15 8:00 
GeneralRe: Trouble binding with TCP to same local endpoint Pin
DelphiCoder11-Feb-15 5:22
DelphiCoder11-Feb-15 5:22 
QuestionConversion of Text Box Hindi Value to Unicode to store in database in hindi Pin
Member 1080107431-Jan-15 21:04
Member 1080107431-Jan-15 21:04 
AnswerRe: Conversion of Text Box Hindi Value to Unicode to store in database in hindi Pin
Afzaal Ahmad Zeeshan31-Jan-15 21:44
professionalAfzaal Ahmad Zeeshan31-Jan-15 21:44 

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.