locked
why does datagramsocket->ConnectAsync() fail when i give ip address?

    Question

  • Hi,

    I am trying to send data using a datagramsocket object but it is returning error when i try to call the storeasync() method of datawriter object(created using the ouputstream property of the datagramsocket).
    When i used "localhost"  to create the hostname parameter it worked without any issue, but when I try to use "127.0.0.1" to create the hostname the storeasync() method is giving exception.

    Please help

    Thanks

    AKEV


    • Edited by AKEV Friday, December 14, 2012 1:47 PM
    Friday, December 14, 2012 1:43 PM

All replies

  • As far I know, HostName class can take both dotted-decimal and domain name, so your above claim should not true. Would you share actually what kind of exception  is thrown(i.e HResult/Message). If possible you can share the minimal code segment where you got exception so that problem can be reproduced.

    Whatever, you may try outputStream->WriteAsync(buffer). Check whether you have followed the proper steps or not.


    Friday, December 14, 2012 3:12 PM
  • Hi,

    Thanks for the reply, I included the code below

    String^ip=ref new String(L"localhost");// This is working But if i give 127.0.0.1 ip instead of localhost it is not working

    socket->ConnectAsync(ref new HostName(ip),port)->Completed=ref new AsyncActionCompletedHandler(AsyncActionCompletedHandlerForSockUdp);
     while(!acfUdp);

     }catch(Exception^ex)
     {
      String^message=ex->Message;
      return E_FAILURE;
     }

    Datawriter^ writer=ref new DataWriter(socket->outputstream);

    writer->writeBytes(data);//data i need to send

    (writer->StoreAsync())->Completed=ref new AsyncOperationCompletedHandler<unsigned int>(AsyncOperationCompletedHandlerSock);

    As i mentioned in the comment section of the first line. If i give localhost as ip, it is working without any problem, but if i give ip it is not working.

    Monday, December 17, 2012 5:57 AM
  • Hi,

    I cannot reproduce this on my side. I used DatagramSocket sample
    http://code.msdn.microsoft.com/windowsapps/DatagramSocket-sample-76a7d82b

    To test with 127.0.0.1 and it works fine. Maybe you can check if there is another issues on your codes. Also, you can try to update your system.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 17, 2012 6:43 AM
  • Hi,

    Even I have downloaded and tried sample code from msdn. It is working for both localhost and for ip(127.0.0.1). The only difference that I can see from my code is that , it is a static library. But if the code works fine for the string localhost , the very same code should work fine for the ip address also. That is my concern, is there any other factors that I need to take care?

    Thanks

    AKEV

    Monday, December 17, 2012 7:18 AM
  • Hi,

    DatagramSocket^ dgSocket=ref new DatagramSocket();
    dgSocket->MessageReceived+=ref new TypedEventHandler<DatagramSocket^,DatagramSocketMessageReceivedEventArgs^>(sContex,&LoopSocketContext::OnMessage);
    (dgSocket->BindEndpointAsync(ref new HostName(ref new String(L"127.0.0.1")),ref new String(L"22112")))->Completed=ref new AsyncActionCompletedHandler(AsyncActionCompletedHandlerForSock);
    dgSocket->ConnectAsync(ref new HostName(ref new String(L"localhost")),ref new String(L"22112"))->Completed=ref new AsyncActionCompletedHandler(AsyncActionCompletedHandlerForSock);

    When I try to send data using the above DatagramSocket it is working without any problem.

    But if I change the Ip 127.0.0.1 to localhost(in BindEndpointAsync) and localhost to 127.0.0.1 (in ConnectAsync) , then it is giving exception.

    what can be the reason for this ?

    Monday, December 17, 2012 9:11 AM