LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-05-2004, 02:12 PM   #1
Agent007
LQ Newbie
 
Registered: Oct 2003
Posts: 28

Rep: Reputation: 15
Socket Question


I understand all sockets programming until it comes to this level:
int read (int fd, char* buffer, size_t size); /* if i am right */
int write(int fd, char* buffer, size_t size);

My question is that: in my case I need to pass a structure between server and client, how to do that ??? (in other way: how to write binary data)
 
Old 01-05-2004, 02:21 PM   #2
luxitan
Member
 
Registered: Aug 2003
Location: Portugal
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
is there a reason for to using send (tcp) or sendto(udp)?
i never workd with read & write on sockets but what you want must be:

struct your_struct s;
write(fd, (char*)&s, sizeof(s));

[EDIT]

also notice thet is not good policy passing structs between networks, cause not all compiler stores struct fields the same way, you might want to use pragma(0) so the compiler doesn't put blank spaces between fields
and don't forget to use hton functions

Last edited by luxitan; 01-05-2004 at 02:24 PM.
 
Old 01-05-2004, 02:48 PM   #3
bretzeltux
LQ Newbie
 
Registered: Oct 2003
Location: Montreal, Canada
Distribution: Fedora Core 2
Posts: 23

Rep: Reputation: 15
Passing bin data to sockets

Hi Agent007 :-)

All the above things from luxitan message is true and in addition to his advices, there are other problems:

I will try to answer as much as I can right now.

It is indeed possible to send almost every kind of data through sockets... Think of the internet browsers :-)

The point is that the read/write kernel functions expects char* type which is very easy to cast to: below an example how to do it and with what not to do though:
Code:
struct   dataT{    // or even a class
     struct anotherstruct* pdata; // Wrong here: you will send 
pointer to an  address instead of  the data it self...
     char* a_string;    // Wrong here too : Thus pointers value will be sent instead of the string or data ...

     char String[128];  // Ok.
     etc ....
};

// here the trick:

. 
.
.
    struct dataT d; // and then feed this struct node...
    int i_bytes_sent = 0; // init to 0 ( see below why )
    int block = sizeof (struct dataT);
    // if in TCP/IP protocol then something like this must be done
    //  to ensure that all the data in the struct is sent:
    while ( i_bytes_sent != block ){
        i_bytes_sent += write(sockfd, (char*) (&d) + i_bytes_sent, block - i_bytes_sent);
    }
.
.
.
So this is I hope your problem resolved
Regarding the data in the structure or class, I remember that there is a function call that resolves embeded pointers to data in such a structure or class but I can't recall which ones... Like I said above, I can't answer completely your question : I am at work right now and I don't have access to my resources...


Whish you a very happy new year 2004

Bretzel

Last edited by bretzeltux; 01-05-2004 at 02:56 PM.
 
Old 01-06-2004, 04:31 AM   #4
Agent007
LQ Newbie
 
Registered: Oct 2003
Posts: 28

Original Poster
Rep: Reputation: 15
Thank you both for your answers,

BUT my software will be developed under multiplatform (server side on windows and client side on unix/windows)
So bretzeltux, will your method still work if i develop the server program under windows ???

and luxitan, what is pragma ?? and what does it do ????
 
Old 01-06-2004, 09:16 PM   #5
bretzeltux
LQ Newbie
 
Registered: Oct 2003
Location: Montreal, Canada
Distribution: Fedora Core 2
Posts: 23

Rep: Reputation: 15
Quote:
Originally posted by Agent007
Thank you both for your answers,

BUT my software will be developed under multiplatform (server side on windows and client side on unix/windows)
So bretzeltux, will your method still work if i develop the server program under windows ???

and luxitan, what is pragma ?? and what does it do ???? [/B]

I have found this interresting study case about your problem. I think it will answer most of your ( and my ) questions

But one problem like the one I have described in my previous message is that TCP/IP protocol is a reliable transport over internet but the bottom line is that it is a STREAMed protocole. That means that all the data is warranty to be delivered but NOT in one shot.. Several read calls may be required to receive the whole data block.
i.e:
server sends:
send(sockfd, (char*)&data, sizeof(data));
client receive:
somebytes += read(sockfd, (char*)&data, sizeof(data));
somebytes += read(sockfd, (char*)&data, sizeof(data));
somebytes += read(sockfd, (char*)&data, sizeof(data));...etc...
until the sum of somebytes equals the sizeof data struct...

again hoping it helps you better
Bretzel
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket timeout (c question) alaios Programming 13 09-11-2005 11:43 AM
Socket question dcfnef Linux - Networking 3 08-31-2005 08:03 PM
socket Question Whiteghost Linux - Newbie 2 08-13-2005 05:24 PM
apache socket question Syncrm Linux - General 0 10-15-2002 11:02 AM
a question about socket dowin Programming 1 11-12-2001 02:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration