Hi all,
I am trying to write a sample socket program in UEFI (using UDK2014 with
StdLib Pkg), But i am getting some error wile connecting to a socket. my
code sample is given below



*******************************************************************************************************

#include "KmsSocketClient.h"

//EFI_STATUS
//EFIAPI
//KmsSocketClientMain(
//IN EFI_HANDLE        ImageHandle,
//IN EFI_SYSTEM_TABLE  *SystemTable

#define SD_SEND 1
int
main()
{
    EFI_STATUS Status;
    int Socket = -1;
    int iResult = 0;
    //int BytesRead = 0;
    struct sockaddr_in RemoteHostAddress;
    CHAR16 recvBuffer[512];
    char *sendbuf = "this is a test";
    int ConnectStatus =0;
    UINT32 RemoteAddress;
    UINT32 Value1 = 10;
    UINT32 Value2 = 201;
    UINT32 Value3 = 42;
    UINT32 Value4 = 116;
    struct sockaddr_in * pRemoteAddress4;
    ZeroMem ( &RemoteHostAddress, sizeof ( RemoteHostAddress ));
    ZeroMem (recvBuffer, 512*sizeof(CHAR16));
    RemoteHostAddress.sin_port  = 27015;
    pRemoteAddress4 = (struct sockaddr_in *)&RemoteHostAddress;

    pRemoteAddress4->sin_len = sizeof ( *pRemoteAddress4 );
    pRemoteAddress4->sin_family = AF_UNSPEC;
    RemoteAddress = Value1
        | ( Value2 << 8 )
        | ( Value3 << 16 )
        | ( Value4 << 24 );
    pRemoteAddress4->sin_addr.s_addr = RemoteAddress;
    Status = EFI_SUCCESS;
    Print(L"Before Creating Socket\n");
    //open a new socket
    if((Socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP )) < 0)
    {
        Print(L"\n Error : Could not create socket \n");
        return EFI_UNSUPPORTED;
    }
    Print(L"After Creating Socket\n");


    //connect to the server.
    ConnectStatus = connect ( Socket,
                              (struct sockaddr *)pRemoteAddress4,
                              pRemoteAddress4->sin_len );
    if(-1 != ConnectStatus){
        Print(L"Connected to the server %d:%d:%d:%d",Value1, Value2,
Value3, Value4);
    }
    else{
        Print(L"Failed to Connected to the server %d:%d:%d:%d",Value1,
Value2, Value3, Value4);

        return EFI_UNSUPPORTED;
    }


    // Send an initial buffer
    iResult = send (Socket, sendbuf, (int)strlen(sendbuf), 0 );
    if (iResult <0) {
          Print(L"Error in sending to the server\n");
        return EFI_UNSUPPORTED;
    }

    printf("Bytes Sent: %ld\n", iResult);

    // shutdown the connection since no more data will be sent
    iResult = shutdown(Socket, SD_SEND);
    if (iResult <0) {
         Print(L"Error in shutting down\n");
        return EFI_UNSUPPORTED;
    }

    // Receive until the peer closes the connection
    do {

        iResult = recv( Socket, recvBuffer, 512*sizeof(CHAR16), 0 );
        if ( iResult > 0 )
            Print(L"Bytes received: %d\n", iResult);
        else if ( iResult == 0 )
            Print(L"Connection closed\n");
        else
            Print(L"recv failed with error: %d\n", 1);

    } while( iResult > 0 );


    //Read from the server.
    /*BytesRead = read(Socket, recvBuffer, 512*sizeof(CHAR16));
    Print(L"Recieved string from the server is %s", recvBuffer);*/
    return EFI_SUCCESS;
}


**********************************************************************************************************

In the above program while connecting (Connect() method) i am getting
 error like SOCKET_ERROR (-1) . Am I not able to resolve the IP correctly
or some where wrong in structuring the sockaddr_in  structure.

Could anyone help me in this issue. suggestions are welcome.
*Thanks,*

*~ParthaS*
*Seagate HDD India *
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
edk2-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/edk2-devel

Reply via email to