How to send a double value in socket programming

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2005
Posts: 13
Reputation: skprasat is an unknown quantity at this point 
Solved Threads: 0
skprasat skprasat is offline Offline
Newbie Poster

How to send a double value in socket programming

 
0
  #1
May 27th, 2005
Hi all,
im new to this.....
i want to send a doube to client...so im convt that to char and sendg that...it prits d exact value in server, but in client it doesnt print the decimal value... it just display like '123.'
my code looks like....

  1. struct sockaddr_in server;
  2. struct sockaddr_in client;
  3. int sockfd,sockfd2,sockfd3, n_bytes;
  4.  
  5. double a=123.456;
  6. char msg1[3];
  7. sprintf(msg1,"%g",a);
  8.  
  9. printf("\n msg1 = %s ",msg1);
  10. //here it prints msg1=123.456
  11. if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
  12. {
  13. printf("Socket error...");
  14. return 0;
  15. }
  16.  
  17. server.sin_addr.s_addr=INADDR_ANY;
  18. server.sin_port=htons(PORT);
  19. server.sin_family=AF_INET;
  20.  
  21. send(sockfd2,msg1,sizeof(msg1),0);
<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: How to send a double value in socket programming

 
0
  #2
May 27th, 2005
Hello,

Moving to the C++ section for technical support / suggestion....
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,414
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How to send a double value in socket programming

 
0
  #3
May 27th, 2005
  1. double a=123.456;
  2. char msg1[3];
  3. sprintf(msg1,"%g",a);
The string msg1 only has room for 2 characters and a terminating null. And you're trying to write 7 characters plus the null to it.
send(sockfd2,msg1,sizeof(msg1),0);
You might mean strlen(msg1).
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC