Re: C Sockets Newbie: Easy question

From: Lew Pitcher (Lew.Pitcher_at_td.com)
Date: 04/16/04


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-----



Relevant Pages

  • Re: C Sockets Newbie: Easy question
    ... send(fd, outb, strlen(outb), 0) ... and I thought that they ware related with Sockets. ... > either the file descriptor of the open file (which is a small number ... "Unix Network Programming" by Stevens. ...
    (comp.unix.programmer)
  • Re: RMI vs. Sockets vs. ?
    ... RMI is easier than socket programming, ... But sockets can help to connect with application written ... Neglecting for a microsecond the labor cost of creating the apps, by the time you compare the parsing, error-handling and related tasks for a custom message infrastructure against a SOAP-based one with, say Apache and Sun tools, you may find that message sizes are not quite so different, and processing costs closer still. ... Undoubtedly SOAP messages would require more bytes than "equivalent" custom-format compact messages; I'd be dubious of claims that there are significant differences in "speed" once a message reaches its destination. ...
    (comp.lang.java.programmer)
  • Re: Implementations for parallel programming?
    ... construct a means for separate instances of Wraith Scheme, ... that communicate using Unix Sockets. ... I have often requested people on the Bigloo ... use Bigloo for parallel programming task. ...
    (comp.lang.scheme)
  • Re: Question about CSocket::Receive()
    ... you should not be using synchronous sockets for any purpose. ... It is the nature of TCP/IP that later bytes will not be lost; ... and return the segment up to the terminator; if no terminator was found, ... Receive), etc., but this is all just straightforward programming. ...
    (microsoft.public.vc.mfc)
  • Re: Batch connection to CSKL (was: Trigger CICS transaction from Batch Job)
    ... You can find the fine manual that describes the CICS side of such at z/OS ... in z/OS Communications Server IP Sockets Application Programming Interface ... transaction) that makes managing a listener in CICS quite simple and effective. ... All you need to do is to code the child server transaction in your programming ...
    (bit.listserv.ibm-main)