LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > 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


Reply
  Search this Thread
Old 05-03-2009, 04:46 AM   #1
kbarani
Member
 
Registered: Dec 2008
Posts: 41

Rep: Reputation: 15
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];

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
 
Old 05-03-2009, 10:49 PM   #2
thelordmule
LQ Newbie
 
Registered: Jul 2006
Location: Australia
Distribution: Mac OSX 10.6, Ubuntu 10.10
Posts: 23

Rep: Reputation: 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?
 
Old 05-03-2009, 11:54 PM   #3
kbarani
Member
 
Registered: Dec 2008
Posts: 41

Original Poster
Rep: Reputation: 15
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
 
Old 05-04-2009, 11:41 PM   #4
thelordmule
LQ Newbie
 
Registered: Jul 2006
Location: Australia
Distribution: Mac OSX 10.6, Ubuntu 10.10
Posts: 23

Rep: Reputation: 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.
 
  


Reply

Tags
array, network, pointer, socket


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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-12-2008 08:48 AM
Two Dimensional Character Problem Mistro116@yahoo.com Programming 8 11-26-2005 08:13 PM
In C, using qsort for dynamically allocated array ntmsz Programming 7 08-23-2005 10:33 AM
Need help: Seg fault, Memcpy, and dynamically allocated arrays benobi Programming 3 06-09-2005 10:58 PM
how to create device node for dynamically allocated major number appas Programming 5 11-01-2004 09:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:48 PM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration