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 non-string data thru BSD Sockets

Hi,
I'm a BSD Socket beginner and I would like to make a small networking program written in C.

I've read many instructional articles and I haven't found a example how to send/receive a non-char - string sending everywhere. I mean, I thought about sending some various structures as "frames". Which would contain: username, filename, data, timestamp, etc. for instance.

The problem is how should I receive the various structures /"frames"/ by recv() func?

Can anybody give me an advice with this? Am I thinking right? I would really appreciate any help with sending a non-string-only data.

yuri1969
Light Poster
29 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

The easiest way is the functionality in the XDR library(eXternal Data Represention) or if your not familiar then you could pass non c-string data by passing the start address and the length...Like this.

unsigned int mydata = 1234;

write(clientfd, &mydata, sizeof(mydata));


Please note, you cannot pass pointers and expect the receiver to use them without failure....You must pass the data that the pointer points to..

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

Ok, so I'm leaving my struct-based date idea. I'll pass the data directly in a sequence of write().

Thank you for the reply.

yuri1969
Light Poster
29 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

Ok, so I'm leaving my struct-based date idea. I'll pass the data directly in a sequence of write().

Thank you for the reply.

You should really check the XDR library, it'll make passing data through a network a snap. http://docs.sun.com/app/docs/doc/816-1435/6m7rrfn9b?a=view

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

Too difficult for a beginner as me.

yuri1969
Light Poster
29 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: