LinuxQuestions.org
Social Bookmarking all things Linux and Open Source
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Software
User Name
Password
Linux - Software This 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

Tags used in this thread
Popular LQ Tags , , ,

Reply
 
Thread Tools
Old 05-03-2009, 05:46 AM   #1
kbarani
Member
 
Registered: Dec 2008
Posts: 40
Thanked: 0
How to send the dynamically allocated two dimensional character array through C++ UDP


[Log in to get rid of this advertisement]
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];

strcpy(buff[0],"first");
strcpy(buff[1],"second");
strcpy(buff[2],"third");

int cid;
struct sockaddr_in caddr;
if ((cid = socket(AF_INET,SOCK_DGRAM,0))< 0)
perror("socket() failed");

caddr.sin_family = AF_INET;
caddr.sin_port = htons(6666);
caddr.sin_addr.s_addr = inet_addr("10.7.8.9");
if( connect(cid , (struct sockaddr *) &caddr , sizeof(caddr)) < 0)
perror("2");

n = (send(cid,buff, 300, 0));
printf("send %d\n",n);

return 0;
}



Thanks
barani
kbarani is offline     Reply With Quote
Old 05-03-2009, 11:49 PM   #2
thelordmule
LQ Newbie
 
Registered: Jul 2006
Location: Australia
Distribution: Mac OSX 10.5, Ubuntu 7.04, Windows XP SP3
Posts: 18
Thanked: 0
Code:
  int i;
  for (i = 0; i < 3; i++) {
     n = (send(cid, buff[i], 100, 0));
     printf("send %d\n",n);
  }
Does this suffice?
thelordmule is offline  
Tag This Post , , ,
Reply With Quote
Old 05-04-2009, 12:54 AM   #3
kbarani
Member
 
Registered: Dec 2008
Posts: 40
Thanked: 0

Original Poster
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.

Thanks,
Barani
kbarani is offline     Reply With Quote
Old 05-05-2009, 12:41 AM   #4
thelordmule
LQ Newbie
 
Registered: Jul 2006
Location: Australia
Distribution: Mac OSX 10.5, Ubuntu 7.04, Windows XP SP3
Posts: 18
Thanked: 0
Thumbs up

Quote:
Originally Posted by kbarani View Post
Hi

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.
thelordmule is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with RTC minor and major allocated dynamically semi-hor Linux - Kernel 0 06-13-2008 06:45 PM
Two Dimensional Character Problem Mistro116@yahoo.com Programming 8 11-26-2005 09:13 PM
In C, using qsort for dynamically allocated array ntmsz Programming 7 08-23-2005 11:33 AM
Need help: Seg fault, Memcpy, and dynamically allocated arrays benobi Programming 3 06-09-2005 11:58 PM
how to create device node for dynamically allocated major number appas Programming 5 11-01-2004 10:37 AM


All times are GMT -5. The time now is 04:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration