Re: C Sockets Newbie: Easy question

From: Francesco Moi (francescomoi_at_europe.com)
Date: 04/17/04


Date: 17 Apr 2004 08:13:39 -0700

Thank you very much both.

I was checking some codefiles and found lines like:

---
rv = read(infd, outb, sizeof(outb))
send(fd, outb, strlen(outb), 0)
sendfile(fd, infd, 0, curstat.st_size)
---
and I thought that they ware related with Sockets.
Regards.
Lew Pitcher <Lew.Pitcher@td.com> wrote in message news:<6bWfc.34317$vF3.1971403@news20.bellglobal.com>...
> -----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
    ... If this is an exam question for a sockets course, ... 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: open fd management
    ... >> number of sockets this process has open. ... >> have an file descriptor associated with it or does it only get the file ... > performance as the number of entries increases. ... Memory is more manageable - I can readily detect my app's own ...
    (comp.unix.programmer)
  • 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)