Join 137,175 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,375 people online right now. Registration is fast and FREE... Join Now!
regMsg->psswd = "joy"; //trying to read in the psswd array // OR strcpy(regMsg->psswd,"joy"); //a different way of reading in to the array
char outMsg[N]; //Array of the out message( the whole struct)
regMsg= (RegMsg_t *)outMsg; //trying to equate the pointer regMsg and the outMsg array
In the code above Non seems working. those are the two ways m trying to read data in to the struct (THE MAIN QUESION IS HOW TO CHANGE THE STRUCT INTO AN ARRAY
Then the other part is to change this whole struct into an ARRAY for the sento function of the UDP socket
these are the problems holding me up. Can anybody pls help me.. Thnks
This post has been edited by eudos: 1 Jun, 2008 - 01:51 AM
Well if the above code isn't working, it is because in your struct the array is passwd, but in implementation you are calling it as psswd.
Does it have to be only one array? A struct or even a class seems to be the most logical way to represent the data you are currently dealing with.
However, you can make it all one array, the first 16 elements (0-15) can take a variable, the next 14 elements (16-29) takes the user name and the last slots (30-however big the port # is) takes the int for the tcp port.
This post has been edited by KYA: 1 Jun, 2008 - 03:08 AM
Looks like you have a 32 byte structure. If you just point to it with a char pointer, you now have something that appears to be an array of bytes. Unfortunately, C uses the value of 0 to terminate string, to a string method probably wont do what you want.
I believe this code will do what you're looking for:
Looks like you have a 32 byte structure. If you just point to it with a char pointer, you now have something that appears to be an array of bytes. Unfortunately, C uses the value of 0 to terminate string, to a string method probably wont do what you want.
I believe this code will do what you're looking for:
//the problem here is ..it only prints "pass" (password) only //that means m not putting all the other( username and tcp port) the array
return 0; }
in the above code m just testing whether the outMsg got all the stuff needed by the server side. Below is the function m using to send to the server side as a UDP socket
CODE
e = sendto(sock, outMsg, outMsgLen, 0, (struct sockaddr *)&toAddr, sizeof(toAddr));
so Hope i wrote my problem i an understandable way
You'll probably get negative numbers on the second one. If that bugs you, make char outMsg[size]; into unsigned char outMsg[size];
That last unsigned short tcpPort; value will be there, but you might need to do a little mental calculation to figure it out. Just keep in mind, it's all just bytes. Everything else is just a form that makes it easier for us humans to deal with.
You could also write your packet to a file, so that you can look at it with a hex editor (as which I assume you're printing the packet for debugging purposes).
FILE* f = fopen("somefile", "wb"); fwrite(outMsg, 1, sizeof(outMsg), f); fclose(f);
I should probably note that the memcpy from one memory block into another is mostly a formality. You can treat your data as a different type in place; C doesn't really care.