944,166 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4668
  • C RSS
May 27th, 2005
0

How to send a double value in socket programming

Expand Post »
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 >>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skprasat is offline Offline
13 posts
since May 2005
May 27th, 2005
0

Re: How to send a double value in socket programming

Hello,

Moving to the C++ section for technical support / suggestion....
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
May 27th, 2005
0

Re: How to send a double value in socket programming

  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).
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: how to find all the drives on a computer?
Next Thread in C Forum Timeline: Implement "Whats This" using Win32





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC