Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does anyone know how can i send a 2d array from server to client.i have tried everything but server never prints my array.(tcp/ip). thanks a lot

What I have tried:

I am trying to do this:send(sock,arr, sizeof(arr), 0)
And in the client:recv(sock, arr, 1024,0) and then a classic double for loop in order to print it
Posted
Updated 9-May-20 4:54am
Comments
Richard MacCutchan 9-May-20 7:18am    
How is the array declared in your code? Also what data gets received by the server?
Member 14825085 9-May-20 7:26am    
it is declared like this char array[30][30]
Richard MacCutchan 9-May-20 12:03pm    
Just create two loops that go round each dimension passing the inner set of characters to the server each time.
Member 14825085 10-May-20 15:42pm    
Can you hell with some code cause im trying 1 day and just cant
Richard MacCutchan 11-May-20 3:19am    
Not really as I don't know exactly what you are trying to do. Show us the code that you have written and explain what is not working, and we can try to help. But just saying "send a 2d array via a socket" does not really make it clear.

You could transform it to a single-dimensioned array then send it as a char* array[900] instead, and transform it back to a bidimenional array on the client.
C++
dim1[i] = dim2[x][y];
i = 30 * x + y;
x = i / 30;
y = i % 30;
 
Share this answer
 
Quote:
Does anyone know how can i send a 2d array from server to client.i have tried everything but server never prints my array.(tcp/ip)

Without seeing your actual code (on both sides), difficult to see what is wrong.
What you describe is a chain of tasks, 1 error and it din't work. You got reached the point where you need to debug your code (make sure every subtask work as expected).

First thing, you need a network sniffer to make sure the server emit something, and what is sent, and where.
Then you need to use the debugger to check that code works as expected on each step.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
-----
By the way, do you know that C do not know the size of arrays ?
C++
send(sock,arr, sizeof(arr), 0);
 
Share this answer
 
v2
You copy your array into the byte buffer and then you write that byte buffer into your open socket
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900