none
SAEA tcp not working fine in .net 45 with vs2012 RP RRS feed

  • Question

  • Hi guys

    my old tcp proxy code works in 4.0 but not in 4.5, from deveoper preview to this RP version.

    so i made a simple console app to exam, and found that SAEA pattern nor working well in .net 45.

    here is the  code

    using System;
    using System.Collections.Generic;
    using System.Net.Sockets;
    using System.Text;
    namespace SocketEventArgsTest
    {
        class Program
        {
            static Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            static void Main(string[] args)
            {
                _socket.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, 5555));
                _socket.Listen(1000);
                var ea = new SocketAsyncEventArgs();
                ea.Completed += ea_Completed;
                _socket.AcceptAsync(ea);
                Console.Read();
            }
            static void ea_Completed(object sender, SocketAsyncEventArgs e)
            {
                Console.WriteLine("ok");
            }
        }
    }

    if we visit (telnet) localhost:5555    in .net 2.0 /4.0 without .net 45 installed  will show "ok", and in .net 45 machine  this will not work, the tcp is connected but no event  or handle callback

    is this a known issue?

    Wayne


    入了ipad,最近用ipad上论坛

    Saturday, June 2, 2012 9:51 PM

Answers

All replies

  • First I assuming you are correct and you do have a connection.  Yo ucan verify that the connection occured by using the DOS command Netstat -a and verifying the port number 5555 is connected between your computer and the server.  If you aren't getting a connection then I suspect the setting System.Net.IPAddress.Any needs to be changed.  You are probably attempting to use the loopback address 127.0.0.1 and TCP will refuse a connection using the loopback.  I have code that will fix this problem if you need it.

    There is nothing in the code that you posted that will use a proxy address.  I supect a bug was fixed in 4.5 that changed a default setting.  In 4.0 the default setting must of been to use a proxy and in 4.5 it was changed to not using a proxy.

    You need to set the clientcredential property (se webpage below).  the credential can either be set to a specific proxy or use the appropriate proxy in the webbrowser.

    http://msdn.microsoft.com/en-us/library/ms732391.aspx

    The code below is from the webpage below.   It sets the credentials to prxoy in the webbrowser.

    WebProxy proxy =new WebProxy(proxyServerAddress, proxyServerPort); 
    proxy
    .Credentials = System.Net.CredentialCache.DefaultCredentials(); 

    http://stackoverflow.com/questions/3311746/how-to-pass-credentials-in-defaultproxy-config-setting


    jdweng

    Sunday, June 3, 2012 9:44 AM
  • i think i did not make it clear.

    i am not using a proxy  in my tcp application.my tcp application is kind of proxy and a tcp bridge it self.

    it failed in 4.5. there is nothing about proxy setting at all.

    just the  saea.Completed event cannot be fired.

    TELNET CLIENT IS CONNECTED  AND CAN SEND DATA, BUT EVENT IS NOT FIRED.

    Could  u please just run my code in  4.0 only env  and  4.5 env  and compare them ?


    入了ipad,最近用ipad上论坛

    Sunday, June 3, 2012 5:17 PM
  • I made two changes in the code

    1) The event handler needs to be before the listen otherwise you may miss the event.  This is probably why the code isn't working.  In Net 4.0 there probably was a race condition and you were lucky that the code worked.

    2) You will get an error with tCP if your end point is the Loopback address 127.0.0.1.  I made changes to make sure you will not get the loopback address as an endpoint.  On some computers without this change TCP won't complete a connection.

    using System;
    using System.Collections.Generic;
    using System.Net.Sockets;
    using System.Net;
    using System.Text;
    namespace SocketEventArgsTest
    {
        class Program
        {
            static Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            static void Main(string[] args)
            {
                string LocalHostName = Dns.GetHostName();
                IPHostEntry LocalHostIPEntry = Dns.GetHostEntry(LocalHostName);
                IPAddress LocalHostIP = LocalHostIPEntry.AddressList[0];
                IPEndPoint ep = new System.Net.IPEndPoint(LocalHostIP, 5555);
                _socket.Bind(ep);
                var ea = new SocketAsyncEventArgs();
                ea.Completed += ea_Completed;
                _socket.AcceptAsync(ea);
                //listen has to be after the event is added
                _socket.Listen(1000);
                Console.Read();
            }
            static void ea_Completed(object sender, SocketAsyncEventArgs e)
            {
                
                Console.WriteLine("ok");
            }
        }
    }


    jdweng

    Sunday, June 3, 2012 8:15 PM
  • Hi Wayne,

    Does Jdweng's suggestion helpful?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 5, 2012 7:58 AM
    Moderator
  • Hi Mike

     

    Not woking at all.

    the code failed event before “ new SocketAsyncEventArgs();” i am not sure he did run the code .

    And I am afraid  what he thought about how SAEA works is wrong.

    1 There is a build in Queue in listening socket, if a connection is create, the new instance will stay in queue, SAEA will never know because it know nothing about listing socket,listing socket know nothing about SAEA ,ether.

    2 After "Accept" the  listing socket got  SAEA reference, and dequeue one new connection, put it into SAEA, then fire "Complete"

    My sample code did "+=" before "Accept" , there is no lucky.

    ---------------------------------

    My sample code works in win7/2.0/4.0   but does not work in Win8 RP/4.5.     if 4.0 & 4.5 got different result, i don't think it is by design.

    Please look into it, it is a bug.


    入了ipad,最近用ipad上论坛

    Wednesday, June 6, 2012 4:28 AM
  • the only thjing that was wrong was moving the Listen() method.  the rest of the changes were fine.  I got any error intiallial on the _socket.AcceptAsync(ea).  try the new changes and see if it works.

    using System;
    using System.Collections.Generic;
    using System.Net.Sockets;
    using System.Net;
    using System.Text;
    namespace SocketEventArgsTest
    {
        class Program
        {
            static Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            static void Main(string[] args)
            {
                string LocalHostName = Dns.GetHostName();
                IPHostEntry LocalHostIPEntry = Dns.GetHostEntry(LocalHostName);
                IPAddress LocalHostIP = LocalHostIPEntry.AddressList[0];
                IPEndPoint ep = new System.Net.IPEndPoint(LocalHostIP, 5555);
                _socket.Bind(ep);
                SocketAsyncEventArgs ea = new SocketAsyncEventArgs();
                ea.Completed += ea_Completed;
                _socket.Listen(1000);
                _socket.AcceptAsync(ea);
                //listen has to be after the event is added
                
                Console.Read();
            }
            static void ea_Completed(object sender, SocketAsyncEventArgs e)
            {
                
                Console.WriteLine("ok");
            }
        }
    }



    jdweng

    Wednesday, June 6, 2012 8:55 AM
  • Joel

    your code is not working in win8 because ip v6 address is in  LocalHostIPEntry.AddressList[0]

    btw, still not work.

    the event is not fired in win8


    入了ipad,最近用ipad上论坛

    Thursday, June 7, 2012 5:41 AM
  • This forum is not meant for bugs in Preview versions

    For that is 

    Http://Connect.Microsoft.Com

     However a little bit senseless as the Preview was not meant for actual code. 

    Meanwhile there was a Beta which is succeeded already by the Release Candidate 

    http://www.microsoft.com/visualstudio/11/en-us/downloads


    Success
    Cor

    Thursday, June 7, 2012 5:52 AM
  •  this bug can show in any version of 4.5  from dev preview  to RC

    and i've follow your advice , log  it to connect.

    Thanks Cor


    入了ipad,最近用ipad上论坛

    Wednesday, June 13, 2012 6:53 AM