Thursday, 15 March 2012

Sending a structure over socket in c -



Sending a structure over socket in c -

i writing programme uses socket programming in c.

in programme client , server maintain transmitting , receiving data.

when send , receive integers on socket using kind of code

//in programme sending info int data; len= send(s,(char *)&(data),sizeof(data),0); //in programme receiving info int num; len = recv(s,&num, sizeof(num) , 0);

the above code working fine , able transmit , receive numbers.

in 1 case trying send construction of form

struct sample{ int num; char chain[10*hops+10]; }; //in programme sending info struct sample s1; len= send(s,(char *)&(s1),sizeof(s1),0); //in programme receiving info struct sample s2; len = recv(s,&s2, sizeof(s2) , 0);

in illustration trying send , receive construction not working. how send , receive struct ?

note: 1) hops pre defined variable.the value of hops same in both programs. 2) not working meant : when receive struct , print value in the num. printing not same value zero!

if trying send struct must utilize serialization , deserialization, read that, can start :

serialization issues while sending struct on socket

c sockets tcp send recv

No comments:

Post a Comment