LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-04-2005, 09:29 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
socket programmin advice plz


Hi I want to make 2 processes communicate using sockets... I want to transfer a structure with its data.. I am a little if this is possible because everything example i read send single characters and not integers and longs that i want to send...
 
Old 09-04-2005, 11:05 AM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
It works the same, you call send() on one side and pass in the address of the structure along with its length, and you receive the same number of bytes on the other end.
In general you need to be careful that both sides agree on the size of an integer/long etc., and also agree on packing as well as byte endian. This is not a problem if you compile and run both processes on the same computer and never on 2 different computers.
 
Old 09-04-2005, 11:14 AM   #3
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Should i use send or should i use sendmsg function???? I am trying it now but i cant do it
 
Old 09-04-2005, 11:26 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You use the API that's most appropropriate.

If this is your SNMP project, you probably want "sendto()" (because SNMP sends UDP datagrams).

Please consider buying this book - you will absolutely find it indispensible:

Unix Network Programming, Vol 1
W. Richard Stevens
<= AN ABSOLUTE MUST-HAVE!!!!!

And if this is your SNMP project: please consider finding an SNMP library to help you. Please do NOT try to write all of your SNMP code from scratch.
 
Old 09-04-2005, 04:55 PM   #5
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Thx i have searched a little and i have also found this one
Unix Network Programming: The Sockets Networking Api
Stevens, W.Richard, Fenner, Bill, Rudoff, Andrew M.
Do u know which one is better?
 
Old 09-04-2005, 08:54 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
The two are just different editions of the same book. The Stevens, Fenner, Rudoff version you mention (the 3rd Edition of UNP) is the later edition - it's the one I'd recommend. The last version Stevens himself was able to complete (before his untimely death) was the 2nd edition - it's the one I own.

Satisfaction guaranteed! It's really in indispensible book!
 
Old 09-05-2005, 02:22 AM   #7
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
I have one more question.. I am trying to pass a socket...
I think i have succesfully make it work
if (sendto( sock, &ds_stats, bufsiz, 0, (struct sockaddr *) &server, server_len) <0){

The struct is declared as struct mystruct ds_stats[10]
I have noticed that when i was using the sendto system call with if (sendto( sock, &ds_stats[10], bufsiz, 0, (struct sockaddr *) &server, server_len) <0){

I couldnt receive the correct values.. So can u explain me the difference betweern &ds_stats[10] and &ds_stats
Thx a lot
 
Old 09-05-2005, 06:46 AM   #8
mehuljv
Member
 
Registered: Nov 2004
Posts: 72

Rep: Reputation: 15
hi,
seems to me you are using some junk memory location. you have declared ds_stats[10], which means you can access ds_stats[0] to ds_stats[9] in C/C++. But you are using ds_stats[10] which has some junk contains.

Mehul.
 
Old 09-05-2005, 09:03 AM   #9
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
So the declaration ds_stats[10] dont means 10 structures...?
 
Old 09-05-2005, 09:42 AM   #10
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
It turns out if you declare a structure ds_stats[10], that the three will all have the same value
ds_stats
&ds_stats[0]
&ds_stats
the last one however is improper (but works) because ds_stats is already a pointer and does not have an address.
 
Old 09-05-2005, 01:22 PM   #11
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Alois:
Quote:
I am trying to pass a socket...
<= uh... what do you mean "trying to pass a socket???""
I hope you mean "I'm trying to send some data".
I hope you *don't* mean that you're somehow trying to pass the socket itself to another process!!!!

Quote:
So the declaration ds_stats[10] dont means 10 structures...?
<= Yes, that's *exactly* what it means.
"mystruct ds_stats[10]" declares an array of exactly 10 items of "mystruct".

Quote:
&ds_stats[10]
<= Unfortunately, this takes the address of the *11th* element of your 10 element array.
It's almost certainly *not* what you want.
This is the error mehuljv was trying to point out.

Quote:
&ds_statis[0]
<= This takes the address of the *first* element.
It is probably what you meant to do.

Please buy the Stevens book and work through some of the examples before you go too much further in this project. I assure you, you'll be much happier and much more productive!

And please, if you haven't already done so, look for an SNMP library that you can use. Please do *not* try to "roll your own" SNMP library (if that's indeed what you're doing...) As sundialsvcs says below, you do *NOT* want to re-invent the wheel!

IMHO .. PSM

Last edited by paulsm4; 09-05-2005 at 01:48 PM.
 
Old 09-05-2005, 01:24 PM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
Be careful that you are not inventing the wheel here...
 
  


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
Triple boot advice needed plz. mortal Slackware 2 11-26-2005 04:27 PM
in need of some productive advice,help plz max_rsr Programming 12 05-02-2005 01:24 PM
Sugestions for a ghood network advice site plz? milescook Linux - Networking 0 01-01-2005 12:43 PM
UK Laptop purchase advice Plz (Where to buy) The Bad Penny General 5 08-21-2004 06:06 AM
Advice Plz. Looking for a full featured router dist. o0shadow0o Linux - Networking 0 07-08-2003 02:39 AM

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

All times are GMT -5. The time now is 07:34 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