tnndesign
asked on
Sending 2D (multi-dimensional) int pointer arrays via Winsock socket (in C)
I have developed a custom Winsock-based library in C for sending data to/from a socket connection between a server and client socket.
My custom library includes functions to transmit and receive single-dimensional (1D) integer-type pointer arrays. It works like this:
Usage:
- Assume I have an array already malloc'd in memory:
testArray[0] = 10;
testArray[1] = 20;
- Array is transmitted like this (dimension count is also transmitted - see above function):
Now I am wanting to transmit a multi-dimensional array ie.
int **myarray;
// Code to malloc both dimensions (not shown)
myArray[0][0] = 1;
myArray[0][1] = 2;
myArray[1][0] = 3;
myArray[1][0] = 4;
How would I then go about transmitting the 2D array over sockets? I just am having trouble figuring it out - I know I could simply loop through all the dimensions and transmit as single ints..but want to transmit it all in one go, like the 1D array.
Suggestions would be greatly appreciated! Many thanks in advance. socketlib.c
(See attached file)
My custom library includes functions to transmit and receive single-dimensional (1D) integer-type pointer arrays. It works like this:
int socketlib_Send_int_Mx1(SOCKET targetSocket, int *Mx1, int m)
{
// Send array size
if(socketlib_Send(targetSocket, (char*)&m, sizeof(m)) < 1)
return 0;
// Send array data
if(socketlib_Send(targetSocket, (char*)Mx1, sizeof(int)*m) < 1)
return 0;
// Successful transmission
return 1;
}
Usage:
- Assume I have an array already malloc'd in memory:
testArray[0] = 10;
testArray[1] = 20;
- Array is transmitted like this (dimension count is also transmitted - see above function):
// Transmit data to server
socketlib_Send_int_Mx1(clientSocket, testArray, arrLen);
Now I am wanting to transmit a multi-dimensional array ie.
int **myarray;
// Code to malloc both dimensions (not shown)
myArray[0][0] = 1;
myArray[0][1] = 2;
myArray[1][0] = 3;
myArray[1][0] = 4;
How would I then go about transmitting the 2D array over sockets? I just am having trouble figuring it out - I know I could simply loop through all the dimensions and transmit as single ints..but want to transmit it all in one go, like the 1D array.
Suggestions would be greatly appreciated! Many thanks in advance. socketlib.c
(See attached file)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I actually just solved this myself about 30 mins ago, but really like your idea of using contingous blocks of memory.
Will incorporate that into my design!