How to send the dynamically allocated two dimensional character array through C++ UDP
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to send the dynamically allocated two dimensional character array through C++ UDP
Hi all,
How to send the dynamically allocated two dimensional character array through C++ UDP socket. my udp client program given below , here i am not able to send data to server , due to the dynamic memory allocation, how to send this dynamically allocated 2 dimensional array through socket.
int main()
{
char **buff;
buff = new char*[3];
buff[0] = new char[100];
buff[1] = new char[100];
buff[2] = new char[100];
Hi
int i;
for (i = 0; i < 3; i++) {
n = (send(cid, buff[i], 100, 0));
printf("send %d\n",n);
}
If i follow above method i have to send three UDP packets , but i want use only one UDP send packet to achieve above purpose , please tell me how to go about this.
If i follow above method i have to send three UDP packets , but i want use only one UDP send packet to achieve above purpose , please tell me how to go about this.
Thanks,
Barani
you never mentioned needing to send only one packet. So my next question is how big do you expect your 2D array to be? since the UDP packet has a maximum size of 65535 bytes of payload (data) per packet.
if your 2d array is always going to be smaller than that number, then using the send method, all you can do is copy all the data into a single array.
Code:
int main()
{
char **buff;
buff = new char*[3];
buff[0] = new char[100];
buff[1] = new char[100];
buff[2] = new char[100];
...
char *tmpbuf = (char*)malloc( 3 * 100 );
int i;
for (i = 0; i < 3; i++)
memcpy(tmpbuf + i*100, buff[i], 100);
n = (send(cid, tmpbuf, 3 * 100, 0));
printf("send %d\n",n);
free(tmpbuf);
return 0;
}
Dont forget to error check all your socket operations. they are bigger problems than you think. trust me.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.