0

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

3
Contributors
2
Replies
4
Views
12 Years
Discussion Span
Last Post by Dave Sinkula
0
double a=123.456;
             char msg1[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).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.