1

I am coming here because I have problem with the recv() function. I am trying to code a TC/IP client which will receive data from a server ( I have not acces to the server code, it is a .exe). I am able to connect and receive the data but then I can not use them. Normaly I should receive a string but in bytes code.

int main()
{
    WSADATA WSAData;
    WSAStartup(MAKEWORD(2,0), &WSAData);//Initialisation du DLL,MAKEWORD(2,0) pour dire que c'est la V2,adresse de la variable qui lance le DLL

    string convert;

    long succes;
    SOCKADDR_IN sin;//info du socket

    int sock, bytes_recieved, bytes_send;
    char send_data[1024], recv_data[2048];

    struct hostent *host;
    struct sockaddr_in server_addr;

    host = gethostbyname("127.0.0.1");

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("SocketError");
        exit(1);
    }
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(50500);
    server_addr.sin_addr = *((struct in_addr *) host->h_addr);
    bzero(&(server_addr.sin_zero), 8);


    if (connect(sock, (struct sockaddr *) &server_addr, sizeof(struct sockaddr))
        == -1) {
        perror("ConnectToError");
        exit(1);
    }


    //bytes_send = send(sock, a, strlen(a), 0);
    //bytes_send = shutdown(sock, 1);


    bytes_recieved = recv(sock, recv_data, 2048, 0);    recv_data[bytes_recieved] = '\0';
    printf("\nRecieved data = %s ", recv_data);
    cout << endl << endl;

    shutdown(sock, 2);
    system("PAUSE");
        WSACleanup();


    return 0;
    }

I have value into my array : Array value But I do not know how to translate them into a string, it should follow the following order :

[0:3] Size of the string

[4:n-2] String, each letter on 2 bytes

[n-1:n] End symbole

Thank you for you help.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Quentin M
  • 171
  • 2
  • 11
  • 3
    You will have to parse the bytes you received.. get the first 4 bytes and convert to a numeric value `n` ([example of little endian to uint](http://stackoverflow.com/questions/17916394/converting-4-bytes-in-little-endian-order-into-an-unsigned-integer)). Afterwards, you should use this `n` to get the string. Since you mention 2 bytes per letter... is it a wchar? You can get the following `n` bytes and construct a `std::wstring` for example. Do you know the end symbol used? is it always the same? Will you receive more than one text at the time? – wendelbsilva Sep 16 '15 at 18:05
  • First thank for the quick answer. I do not know waht kind of data it is the just said bytes. The end symbol is two zeores, like the exemple : Example: to send or receive the “xyz” Unicode string, it must be wrapped into the following packet: i. Bytes [0:3] – integer 8 as the number of data bytes ii. Bytes [4:5] – ‘x’ in Unicode iii. Bytes [6:7] – ‘y’ in Unicode iv. Bytes [8:9] – ‘z’ in Unicode v. Bytes [10:11] – zeroes terminating the string vi. Total amount: 12 bytes I will try to make a wstring and use wchar. – Quentin M Sep 17 '15 at 07:39

2 Answers2

0

I modified a bit my code after reading some things on internet and to make it more clear :

void reception()
{

    wchar_t input[2048];
    wstring message;
    unsigned int data_receive;
    if(data_receive = recv(sock,(char*) input, 2048, 0) != -1)
    {
        message = input;
        std::wofstream fs("testout.txt");
        fs << message << std::flush;

        //cout << input[0]<<endl;

        system("PAUSE");
    }
}

Now I have a array of bytes I suppose but I do not know how to parse it. I think my input array is with UTF16. See the array value : Array view

Quentin M
  • 171
  • 2
  • 11
0

wendelbsilva already posted the answer, but it was in a comment.

Do two recv calls. The first call for 4 bytes. Like so:

unsigned int len;
assert(sizeof(len) == 4);
data_receive = recv(sock, &len, 4);

Then you can read the real string.

std::vector<wchar_t> input(len+1);
if(data_receive = recv(sock, input.data(), len) != -1)
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131