Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to send a 2d char array from server to client,but this doesn't work, it prints me nothing on the client.Im using TCP/IP.Can you help me with some code?

What I have tried:

C++
************SERVER*********

char arr[20][20];

int i,j; 
 for(i=0;i<20;i++){
	for(j=0;j<20;j++){
		send(sock,&arr[i][j],sizeof(char),0);

	}


	}
*******CLIENT***************************

char arr[20][20];


int i,j; 
 for(i=0;i<20;i++){
	for(j=0;j<20;j++){
  		recv(sock,&arr[i][j],sizeof(char),0);
		printf("%s",arr[i][j]);
	}


	}
Posted
Updated 11-May-20 21:52pm
v5
Comments
Afzaal Ahmad Zeeshan 11-May-20 14:35pm    
Sure,

How are you connecting or establishing the connection over TCP/IP? Can you share the entry points for these two programs?
Member 14825085 11-May-20 14:46pm    
Its just a typically tcp/ip connection not something special bro
jeron1 11-May-20 15:19pm    
So you are successful in establishing a TCP/IP connection?
Member 14825085 11-May-20 17:08pm    
Yes i send some simple messages before and works perfectly
jeron1 11-May-20 17:19pm    
The return value from the send() function is what you expect?

Here is an implementation that can be used both for Windows and Linux:
C sockets portable in windows/linux example · GitHub[^]
sizeof(char)
won't work, you need to pass the length of the string.
 
Share this answer
 
v2
Comments
Member 14825085 11-May-20 17:13pm    
Rickzeeland thx for this but what i have to do when i want to send a 2d char array;
There are several different ways to do this. Since you are using C and you have not described the nature of your data at all I will make some assumptions. The primary one is they are going to be variable length strings so I would write the data out with length bytes.

First write the number of rows of data. Use four bytes for this. Then write that number of rows of text data. Just so you don't get too tangled up, I would write an index and a length for each row of text. Use four bytes for the index and two for the length. Then you write the (length) bytes of the string. Continue doing this for every line of data. If you want you could then write an end of array marker. This could be as simple as a few bytes of zeros.

Reading will be just like writing, almost. You have to peel off the bytes of the message and make sure you have more waiting for you. This is one use for the 'peek' option. You could simplify things a bit and first traverse your data and figure out how much you will send and write this as the first four bytes. This would be followed by the row count and then the rows of text. That is a simple operation - number of lines times six plus the length of each line plus four for the initial length and four for the termination.

You could optionally write a terminating null character with each string and if you do then I would have the length bytes include the null. What I mean is if one line has "abc" and you are writing the null too then its length should be four.

As I wrote, this is just one possibility and there are many.
 
Share this answer
 
Your code is somewhat confusing. You do not need to send characters 1 at a time. The main loop should run 20 times, and each time round it can send 20 characters. And the length of your send and receive data should not be sizeof(char) (which is 8) but the count of bytes in the data, which in this case is 1. Also you are trying to print a single character but referring to it as a string. The correct format control for a single character is %c. And unless you know that the 20 characters comprise a null terminate sequence you should never use %s .

Take a look at send(2): send message on socket - Linux man page[^] for more details.
 
Share this answer
 

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