954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to send a double value in socket programming

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

struct sockaddr_in server; 
	struct sockaddr_in client; 
	int sockfd,sockfd2,sockfd3, n_bytes; 

	double a=123.456;
             char msg1[3];
	sprintf(msg1,"%g",a);    	
	    
	printf("\n msg1 = %s ",msg1);
//here it prints msg1=123.456 	  
	if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{ 
		printf("Socket error..."); 
		return 0; 
	} 

	server.sin_addr.s_addr=INADDR_ANY;  
	server.sin_port=htons(PORT);  
	server.sin_family=AF_INET; 
 
	send(sockfd2,msg1,sizeof(msg1),0);


<< moderator edit: added [code][/code] tags >>

skprasat
Newbie Poster
13 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

Hello,

Moving to the C++ section for technical support / suggestion....

kc0arf
Posting Virtuoso
Team Colleague
1,937 posts since Mar 2004
Reputation Points: 121
Solved Threads: 57
 
double a=123.456;
             char msg1[3];
	sprintf(msg1,"%g",a);


The stringmsg1 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 meanstrlen(msg1).

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You