Re: C Sockets Newbie: Easy question
From: Lew Pitcher (Lew.Pitcher_at_td.com)
Date: 04/16/04
- Next message: Fletcher Glenn: "Re: Problems with shared libraries on Solaris"
- Previous message: joe_at_invalid.address: "Re: Problems with shared libraries on Solaris"
- In reply to: Francesco Moi: "C Sockets Newbie: Easy question"
- Next in thread: Francesco Moi: "Re: C Sockets Newbie: Easy question"
- Reply: Francesco Moi: "Re: C Sockets Newbie: Easy question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 16 Apr 2004 15:08:51 -0400
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Francesco Moi wrote:
| I was trying to learn to program Sockets in C and found this
| exam question:
If this is an exam question for a sockets course, then the examiner is
examining the wrong subject. The code fragment below has nothing to do
with sockets, and everything to do with unix file handling.
| ------------------//----------------------
| Explain what is happening in the following code segment
|
| int fd;
| fd = open("~/myfile",O_RDONLY);
| if(fd==-1)
| {
| /* Some code */
| exit(1);
| }
| --------------//------------------------
fd is a 'file descriptor'
open() attempts to the named file, with the named mode, and returns
either the file descriptor of the open file (which is a small number
greater than -1) or a -1 to indicate an error.
the if statement checks the file descriptor returned by the open()
function, and if it is -1 (that is to say, open() failed to open the
file), the if statement executes the 'true' branch of the test.
The true branch does some things ("/* some code */"), then invokes the
exit() function to hard-terminate the process. The exit() function takes
as its argument, a number (in this case, 1) which will be given to the
OS as the process' termination code. exit() does not return, but
instead, causes the process to terminate.
So, this is prototypical code for an "abort the program if the file
can't be opened for read access" logic segment, and has nothing directly
to do with sockets programming.
Aside: open() will attempt to open the named file. That file is, in this
case, the file "myfile" in the "~" subdirectory of the process' current
working directory.
| I found in some codetexts 'fd' and 'outb', what are they?
It's impossible to tell, given the amount of detail you've supplied.
'fd' probably is the file descriptor returned by open(), but it /may/ be
used as a socket descriptor.
'outb' probably is an output buffer, but may be a reference to a
function outb() which /may/ output a single 8-bit byte to a specified
output port.
| Where can find I resources to learn C Sockets?
Like Barry said, "Unix Network Programming" by Stevens.
I'd also suggest that you pick up a couple of books on Unix programming.
"Advanced Programming in the Unix Environment" may be too advanced for
you at the moment, but it's essential for anyone developing Unix code at
the level of sophistication that sockets implies.
- --
Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
iD8DBQFAgC9BagVFX4UWr64RAskcAKDHzCC/1vMxr//T5q0MKKlhBWqifQCgpKpn
03AJNWkIrGO/UTJd2WanG98=
=W80J
-----END PGP SIGNATURE-----
- Next message: Fletcher Glenn: "Re: Problems with shared libraries on Solaris"
- Previous message: joe_at_invalid.address: "Re: Problems with shared libraries on Solaris"
- In reply to: Francesco Moi: "C Sockets Newbie: Easy question"
- Next in thread: Francesco Moi: "Re: C Sockets Newbie: Easy question"
- Reply: Francesco Moi: "Re: C Sockets Newbie: Easy question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|