Solved

Unix Networking

Posted on 1999-01-05
14
252 Views
Last Modified: 2013-12-23
I want to write a program in C under Unix such that when executed it prompts the user for the user to enter a UNIX servername or IP address, then the program "looks" for the server in  question and notifies the user on the display if the user can communicate withit (more or less like ping works). The program must run using Sun Operating System 5.5!!

Blu - the answer I am expecting is more or less like what Harry has sent!

Harry - Your answer is more like what I am looking for! Test the program and send it over if it works! Please comment more on the source code. I am still learning C! I will try testing what you have sent and will notify you of my results!
0
Comment
Question by:galeboe
  • 8
  • 3
  • 2
  • +1
14 Comments
 
LVL 22

Expert Comment

by:blu
ID: 1582784
What do you mean by located?
How about the ping command?
0
 

Author Comment

by:galeboe
ID: 1582785
Edited text of question
0
 
LVL 22

Expert Comment

by:blu
ID: 1582786
I still do not know what you want to do.  Can you explain why
ping is not an answer for you?  Ping tells you if you can
communicate with a server and it runs under Solaris 2.5 (5.5).
I understand that you want something more than ping, I just can't
figure out what functions are missing.
0
 
LVL 2

Expert Comment

by:harrys
ID: 1582787
You mean somthing like...


int main(int argc, char **argv)
{
  struct hostent *host;
  int err, i, net;
  struct sockaddr_in sa;

    host = gethostbyname("SERVER.FOO.BAR");
    sa.sin_family = AF_INET;
    sa.sin_port = htons(PORTNUMBER);
    net = socket(AF_INET, SOCK_STREAM, 0);
     }
    err = connect(net, (struct sockaddr *) &sa, sizeof sa);
    if (err < 0) {
     printf("NO\n");
    } else {
      printf("YES\n");
      if (shutdown(net, 2) < 0) {
        perror("\nshutdown");
        exit(1);
      }
    }
    close(net);
  return (0);
}


this is untestet, server.foo.bar is the server to connect to, portnumber  is the TCP port to connect...
0
 

Author Comment

by:galeboe
ID: 1582788
Edited text of question
0
 

Author Comment

by:galeboe
ID: 1582789
Adjusted points to 70
0
 
LVL 1

Accepted Solution

by:
cox earned 70 total points
ID: 1582790
commenting the code

/* main function */
int main(int argc, char **argv)
{
   /* struct hostent  - use this to returnm of the function gethostbyname()*/
   struct hostent *host;
   int err, i, net;
   /* Internet Socket Address Struct */
   struct sockaddr_in sa;

  /* search for "SERVER.FOO.BAR" and return hostent struct for address or error */
   if (host = gethostbyname("SERVER.FOO.BAR"))
      /* copy the address to Socket Address Struct */
      bcopy(host->h_addr, (char *)&sa.sin_addr, host->h_length);
   /* AF_INET == ARPA Internet protocol (use it to intenret conections)
   sa.sin_family = AF_INET;
   /* htons convert the PORTNUMBER to network byte order -  Most Significant Byte first*/
   sa.sin_port = htons(PORTNUMBER);
   /* Create a stream for the conection */
   net = socket(AF_INET, SOCK_STREAM, 0);
 
  /* try to conect */
  err = connect(net, (struct sockaddr *) &sa, sizeof sa);
   if (err < 0) {
      printf("NO\n");
   } else {
      printf("YES\n");
      /* Close the conection */
      if (shutdown(net, 2) < 0) {
         perror("\nshutdown");
         exit(1);
      }
   }
    /* Close the conection */
   close(net);
   return (0);
}
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:galeboe
ID: 1582791
How do I compile and execute this program! I am using Soalris 2.5. Thanx for helping me cox
0
 

Author Comment

by:galeboe
ID: 1582792
Thanx for the answer cox, but have you tested it and does it run correctly? If yes, how do I compile and execute this program! I am using Soalris 2.5. Thanx for helping me cox!
0
 
LVL 1

Expert Comment

by:cox
ID: 1582793
i compiled this program, and its works.. please look at your *.h  

#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

/* main function */
int main(int argc, char **argv)
{
   struct hostent *host;
   int err, i, net;
   struct sockaddr_in sa;

   if (host = gethostbyname("localhost"))
      bcopy(host->h_addr, (char *)&sa.sin_addr, host->h_length);

    sa.sin_family = AF_INET;
    sa.sin_port = htons(25);
   /* Create a stream for the conection */
   net = socket(AF_INET, SOCK_STREAM, 0);

   /* try to conect */
   err = connect(net, (struct sockaddr *) &sa, sizeof sa);
   if (err < 0)
   {
      printf("NO\n");
   }
   else
   {
      printf("YES\n");
      if (shutdown(net, 2) < 0)
      {
         perror("\nshutdown");
         exit(1);
      }
   }
   /* Close the conection */
   close(net);
   return (0);
}

0
 

Author Comment

by:galeboe
ID: 1582794
How do I make this program an executable and what are its typical arguments! I compiled it and it is error-free but how do I test it actually works correctly? Thnax for the comments you have added! They are quit helpful since I know little C programming!
0
 

Author Comment

by:galeboe
ID: 1582795
How do I make this program an executable and what are its typical arguments! I compiled it and it is error-free but how do I test it actually works correctly? Thnax for the comments you have added! They are quit helpful since I know little C programming!
0
 
LVL 1

Expert Comment

by:cox
ID: 1582796
what compiler are you using??

normally, i use:

gcc -c file.c
gcc -o file file.o

It will make a executable file, and the name of this file is 'file'
This program dont use arguments, because server and port are into the program, but you can pass like arguments...
0
 

Author Comment

by:galeboe
ID: 1582797
How do I make this program an executable and what are its typical arguments! I compiled it and it is error-free but how do I test it actually works correctly? Thanx for the comments you have added! They are quite helpful since I know little C programming!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
This is an article about my experiences with remote access to my clients (so that I may serve them) and eventually to my home office system via Radmin Remote Control. I have been using remote access for over 10 years and have been improving my metho…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now