how to check socket connection using C ?

how to check socket connection using C ?

Post by Eric La » Wed, 04 Sep 1996 04:00:00



I'd like to write a C program that can check socket connection status of
some particular socket on either local or remote workstation, just like to
one that netstat does.  What lib. or func. that I can make use of ?

Looking forward to any advice.

Rgds
Eric Lam

 
 
 

how to check socket connection using C ?

Post by Darrell Denlinge » Wed, 04 Sep 1996 04:00:00


I want to check if a specified file is a symbolic link.  
I looked at the stat call and can't see how to use it to answer
this question.

sh has the -h test flag, but I want to check in a C program.

If anyone could tell me, I would appreciate the info.
Thanks,

 -Darrell
________________________________________________________________________
        __            ______ Darrell W. Denlinger - Software Engineer
    0   | \          /_______ ESEA Inc.
   /\_^_|  |__      /_________ 5150 El Camino Real, Suite B-15
  `__  ----+  \____/___________ Los Altos, CA 94022

________________________________________________________________________

 
 
 

how to check socket connection using C ?

Post by Andrew Gier » Thu, 05 Sep 1996 04:00:00


 Darrell> I want to check if a specified file is a symbolic link.  I
 Darrell> looked at the stat call and can't see how to use it to
 Darrell> answer this question.

 Darrell> sh has the -h test flag, but I want to check in a C program.

The reason stat() doesn't tell you is that it *follows* the link and
returns info about the pointed-to object.

So, use lstat() which doesn't follow symlinks, and look at the st_mode
field (use the S_ISLNK() macro if available).

Hope this helps

--

Unsolicited email (technical questions or otherwise) is NOT welcome.

 
 
 

how to check socket connection using C ?

Post by Fletcher.Gl.. » Thu, 05 Sep 1996 04:00:00



>I want to check if a specified file is a symbolic link.  
>I looked at the stat call and can't see how to use it to answer
>this question.

>sh has the -h test flag, but I want to check in a C program.

>If anyone could tell me, I would appreciate the info.
>Thanks,

> -Darrell
>________________________________________________________________________
>        __            ______ Darrell W. Denlinger - Software Engineer
>    0   | \          /_______ ESEA Inc.
>   /\_^_|  |__      /_________ 5150 El Camino Real, Suite B-15
>  `__  ----+  \____/___________ Los Altos, CA 94022

>________________________________________________________________________

If you look into the file /usr/include/sys/stat.h, you will find definitions
for all of the mode bits in struct stat.st_mode.  Quoting directly from
the stat.h:

#define S_IFLNK         0xA000  /* symbolic link */

and

#define S_IFMT          0xF000  /* type of file */

This last can be used as a mask to isolate the file type bits.


 
 
 

how to check socket connection using C ?

Post by Michael J. Fromberg » Thu, 05 Sep 1996 04:00:00



>I want to check if a specified file is a symbolic link.  
>I looked at the stat call and can't see how to use it to answer
>this question.

>sh has the -h test flag, but I want to check in a C program.

>If anyone could tell me, I would appreciate the info.

Most Unices define constants in sys/stat.h that you can use to test the
value of the st_mode field in the structure returned by stat().  Under
certain OS's (notably IRIX, in which st_mode is not (necessarily) a
bit-field) there is a macro S_ISLNK() which gives you this functionality:

struct stat   sbuf;

int islink(char *fname)
{
   if(stat(fname, &sbuf) < 0) {
      /* error, stat failed   */
   } else {
      return S_ISLNK(sbuf.st_mode);
   }

Quote:}

If your Unix hasn't got this macro, you can just use the S_IFLNK constant
to mask the bit value out of the st_mode field directly:

...
return sbuf.st_mode & S_IFLNK;

Cheers,
-M

--
Michael J. Fromberger
Thayer School of Engineering Unix Group
Dartmouth College, Hanover, New Hampshire, USA

 
 
 

how to check socket connection using C ?

Post by David Madd » Thu, 05 Sep 1996 04:00:00




Quote:>I want to check if a specified file is a symbolic link.  
>I looked at the stat call and can't see how to use it to answer
>this question.

>sh has the -h test flag, but I want to check in a C program.

>If anyone could tell me, I would appreciate the info.
>Thanks,

> -Darrell

From stat's man page:

SYNOPSIS
     #include <sys/types.h>
     #include <sys/stat.h>

     int stat(const char *path, struct stat *buf);

     int lstat(const char *path, struct stat *buf);

     int fstat(int fildes, struct stat *buf);

        <snipola>

     st_mode   The mode of the file as described in mknod(2).  In
               addition  to  the modes described in mknod(2), the
               mode of a file may also be S_IFLNK if the file  is
               a  symbolic  link.  (Note that S_IFLNK may only be
               returned by lstat().)

--
I8R,
Dave "Wile E."
'76 FXE
'70 FLH
HSB #33 1/3

 
 
 

how to check socket connection using C ?

Post by Jonathan Ch » Fri, 06 Sep 1996 04:00:00



Quote:>I want to check if a specified file is a symbolic link.  
>I looked at the stat call and can't see how to use it to answer
>this question.

>sh has the -h test flag, but I want to check in a C program.

>If anyone could tell me, I would appreciate the info.
>Thanks,

man lstat(2).
--
Jonathan Chen           | One Net to rule them all, One Net to find them,

 
 
 

how to check socket connection using C ?

Post by Andrew Gier » Fri, 06 Sep 1996 04:00:00


 Michael> struct stat   sbuf;

 Michael> int islink(char *fname)
 Michael> {
 Michael>    if(stat(fname, &sbuf) < 0) {
 Michael>       /* error, stat failed   */
 Michael>    } else {
 Michael>       return S_ISLNK(sbuf.st_mode);
 Michael>    }
 Michael> }

The above always returns false, because stat() will follow the symbolic
link to return info about the object it points to. You need lstat()
instead.

 Michael> If your Unix hasn't got this macro, you can just use the
 Michael> S_IFLNK constant to mask the bit value out of the st_mode
 Michael> field directly:

 Michael> ...
 Michael> return sbuf.st_mode & S_IFLNK;

No... that breaks on many systems; needs to be more like

  return (sbuf.st_mode & S_IFMT) == S_IFLNK;

(but the point is moot, since S_ISLNK should be there anyway).

--

Unsolicited email (technical questions or otherwise) is NOT welcome.

 
 
 

how to check socket connection using C ?

Post by Michael J. Fromberg » Fri, 06 Sep 1996 04:00:00




> Michael> return sbuf.st_mode & S_IFLNK;

>No... that breaks on many systems; needs to be more like

>  return (sbuf.st_mode & S_IFMT) == S_IFLNK;

>(but the point is moot, since S_ISLNK should be there anyway).

Yes, you're absolutely right, that is a better way to do it.  And, as you
say, S_ISLNK() -should- be there on most systems.  I sit corrected.

Of course, we all know about "should" vs. "is" on a Unix system... :)

-M

--
Michael J. Fromberger
Thayer School of Engineering Unix Group
Dartmouth College, Hanover, New Hampshire, USA

 
 
 

1. Opening a socket connection while in a socket connection.

Hello,

I am new to using sockets and am working on a project that requires
me to audit services accessed under a client/server program (TCP/IP)
socket connection.

What I want to do is send a datapacket to a collector machine each
time a client accesses a service on the server.  It seems that
I am unable to do this from the server.  Basically, in short form
I need to be able to do the following:

1. Client connects (TCP/IP socket connection) to server
2. Client requests a service from the server
3. At that point I want to send a datapacket to a audit server.

Any Ideas?  I am familiar with most of the concepts involved in
using sockets.  Maybe I have been working on this to long and have
gotten tunnel vision.

Thanks

***********************************************************
Brent Curtiss


Academic Computing Services
The Ohio State University
***********************************************************

2. Soft Link vs Hard Link

3. Checking for a closed socket connection

4. How to read position of scrollwheel?

5. $$ Parsing internet Log Files

6. Help checking a TCP/IP socket connection

7. looking for participants for a usability study

8. check requested socket connection

9. checking socket connections

10. Socket connections using shared libraries

11. How to send EOF using socket connection

12. Weird: get a socket connection without a running socket server !