user2594182
user2594182

Reputation: 13

Char to Char[1024] error - UDP Internet Protocol

Code:

int SendDG(char TOSEND, int IRES, SOCKET SSOCK, sockaddr_in RADD)
    {
        char Data[1024]=TOSEND;
        int BufLen = 1024;
        int iResult = IRES;
        SOCKET SendSocket = SSOCK;
        sockaddr_in RecvAddr = RADD;
           //---------------------------------------------
        // Send a datagram to the receiver
        wprintf(L"Sending info to the receiver...\n");
        iResult = sendto(SendSocket,
                         Data, BufLen, 0, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr));
        if (iResult == SOCKET_ERROR) {
            wprintf(L"sendto failed with error: %d\n", WSAGetLastError());
            closesocket(SendSocket);
            WSACleanup();
            return 1;
        }
    }

Errors:

IntelliSense: initialization with '{...}' expected for aggregate object error C2440: 'initializing' : cannot convert from 'char' to 'char [1024]'

Upvotes: 0

Views: 139

Answers (3)

np_complete
np_complete

Reputation: 163

use strcpy(Data,TOSEND); instead of Data[1024]=TOSEND hope it solves the problem.

Upvotes: 0

barak manos
barak manos

Reputation: 30136

If you just want to send the contents of TOSEND (as it appears to be the case in your code), then:

  1. Get rid of this:

    char Data[1024]=TOSEND;

    int BufLen = 1024;

  2. Change this:

    iResult = sendto(SendSocket, Data, BufLen, ...);

    to iResult = sendto(SendSocket, &TOSEND, sizeof(TOSEND), ...);

Upvotes: 0

sleepy1771
sleepy1771

Reputation: 319

A question would have been nice.

But I guess: in the third line you write:

char Data[1024]=TOSEND;

where Data is an char-array and TOSEND is a plain char. And so you cannot assign a plain char to a char array. Might be that you want something like

char Data[1024];
Data[0] = TOSEND;

But I don't think so. I assume that the TOSEND should some kind of pointer that points to the data that should be sent.

Upvotes: 1

Related Questions