Socket Question


 
Thread Tools Search this Thread
Top Forums Programming Socket Question
# 1  
Old 01-05-2004
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)
# 2  
Old 01-05-2004
At one level could just you take the address of the structure, cast it to (char *) and you have the second parameter. And the third parameter can be obtained via sizeof.

But this whole approach is very wrong. You are assuming identical architectures for both systems. To explain how to do this right would take a book. Several have been written. Buy one and read it. Search this forum for book suggestions.
# 3  
Old 01-05-2004
In case you really knows the structure passed is portable across systems, some people may choose RPC instead of socket, which has provisions for passing arbitrary structures. It is also well-known that RPC is not secure though.
# 4  
Old 01-06-2004
Thank you both for your replies,

But Perderabo, what do you mean by identical architectures for both systems???

In my case the software I'm developing will be deployed under two operating systems (server side on windows, client side on tru64 UNIX). Currently I'm testing both sides on unix, and your method seems to work, however does that mean it won't when I convert the exact same equivalent server source code to windows ?????
# 5  
Old 01-06-2004
We don't need to introduce other OS's to illustrate the problem. It will almost certainly fail when used between a sparc running solaris and an intel running solaris!

Now understand that you can get lucky and find two different systems that can successfully run your code. I don't have a prediction for the two systems you mention.

Here is an example of a program that fails because it assumes a particular structure for an integer. And no networking is involved, simply an abuse of read()/write() much like you plan to do.
# 6  
Old 01-06-2004
ok .. Thanks Perderabo for your reply .. I get what you mean now

So Is there any other alternatives ??? like send(), recv() ???
(I never understood what they do, I just know they do much like read()/write() .. except read()/write() are 1000 times easier)
# 7  
Old 02-26-2004
Underlying any socket communication is the read/write suite of functions (actually I think the vector routines are the most raw without going down to character by character level) however its a translation that you are looking for. A single 8 bit character, in essence, is the only information that will, bitwise, translate across a socket and be reliable on both ends.

I.E. if you send a character, with binary value 01100001 via write it will wind up on the receive end as 01100001 as expected. However, integers (on many systems) are 16 bits, or 2 bytes, or 2 characters. Some systems will represent them MSB, some LSB (or big/little endian). Therefore an integer with "in memory" value [00000001][10000000] on one machine may read [10000000][00000001] on another. Therefore sending the two characters (as an integer) 00000001 followed by 10000000 will wind up, potentially, having different integer values on each end.

Now this problem becomes more complicated in that some systems represent integers as 16 bits, some as 32 bits, some as 8 bits (if you go back long enough). Then you throw in things like byte alignment and other issues and you can get very confusing results.

For example, the structure:

Code:
struct
{
     int a;
     char b;
     int c;
}

On a machine with 32 bit (4 byte) integers (and normal 8 bit (1 byte) characters) could be 72 bits (or 9 bytes) on a machine whos naturally aligned on character boundaries (I doubt there are many). However many machines align on 4 byte boundaries, meaning that the character needs to take up a full 32 bits to insure that the next integer 'c' is correctly aligned, making most machines create a 12 byte structure.

Therefore even sizeof is not reliable.

Now, on to the solution. Basically, what generally happens is that at some level a precompiler generally takes a description of the structures that you want to communicate (such as RPCGEN for the RPC/XDR suite) and generates a set of routines that can traverse the structure on a given machine and convert it into a string of bytes that is in a "intermediate" agreed upon format. Then sends it across the socket (with write) and receives it on the other end and traverses this agreed upon format to reconstruct a similar structure on the other end. These routines are typically not too complicated, but need to be standardized and written for every machine that will be communiated to translate (bi-directionally) to and from that machine's native format.

There is also built-in macros/functions that can convert some primary types to a standard (called network) format. For integers and shorts (or more accurately 32 and 16 bit numbers) there is the htonl and ntohl and htons and ntohs routines, which stand for (host to network long) (network to host long) (host to network short) and (network to host short). These typically should be passed the (uint32_t and uint16_t) types respectivelly to guarentee that 32 and 16 bit integers are used for your machine, because as mentioned before some machines use different "native" sizes for int, long, and short.

So there's a primer...hope it helped. I've currently been smack in the middle of writing a huge communication API for my company which had to address a lot of these concerns. I ended up using the RPC XDR suite to do the translation and built a much better, in my opinion, communication suite are pre-existing structure translation rather than use the RPC paradigmn directly since it really isn't designed to do bi-directional client/server asynchronous type communication like we needed.

Well, if I can clarify anything else...I'd be glad to if I have the time.

Brian Gregg
Computer Sciences Corp.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

2. Programming

Error with socket operation on non-socket

Dear Experts, i am compiling my code in suse 4.1 which is compiling fine, but at runtime it is showing me for socket programming error no 88 as i searched in errno.h it is telling me socket operation on non socket, what is the meaning of this , how to deal with this error , please... (1 Reply)
Discussion started by: vin_pll
1 Replies

3. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

4. Shell Programming and Scripting

Perl socket question

Hi there, Really quick question that I have been unable to find an answer for on the web is simply, can use the newer IO::Socket module on my sever process and use the older perl built-in "socket" module to connect to the IO:socket? or do i need to have the same module (be it socket or... (1 Reply)
Discussion started by: rethink
1 Replies

5. Programming

which socket should socket option on be set

Hi all, On the server side, one socket is used for listening, the others are used for communicating with the client. My question is: if i want to set option for socket, which socket should be set on? If either can be set, what's the different? Again, what's the different if set option... (1 Reply)
Discussion started by: blademan100
1 Replies

6. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

7. IP Networking

IP Socket

Please need to setup a IP Socket on SCO Open Server 5.06 / 5.07 to a Linux machine. Al that I want to do is via a simple shell command open a tcpip address, port number, and send a call to a progam with a name of a file on the linux box. My programming language does not support socket... (1 Reply)
Discussion started by: comcaps
1 Replies

8. UNIX for Dummies Questions & Answers

C socket

I have been serching for a guide to unix C network programming everywhere but I have found only some tuorials, very useful but a little bit poors. My question is if exist a network domain all over the world where C socket is treated with examples. Programs i have looked into is hard to... (4 Replies)
Discussion started by: Davide71
4 Replies

9. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies

10. Programming

How to use the socket

I must write some codes about client/server architecture. The server program send the data to the client program,And the data is huge count and I want to implement that If some error happen and traslation of the data is interrupted,The program can send the data to the client program from the pos... (1 Reply)
Discussion started by: niuzefeng
1 Replies
Login or Register to Ask a Question