• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

Unix Networking

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
galeboe
Asked:
galeboe
  • 8
  • 3
  • 2
  • +1
1 Solution
 
Brian UtterbackPrinciple Software EngineerCommented:
What do you mean by located?
How about the ping command?
0
 
galeboeAuthor Commented:
Edited text of question
0
 
Brian UtterbackPrinciple Software EngineerCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
harrysCommented:
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
 
galeboeAuthor Commented:
Edited text of question
0
 
galeboeAuthor Commented:
Adjusted points to 70
0
 
coxCommented:
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
 
galeboeAuthor Commented:
How do I compile and execute this program! I am using Soalris 2.5. Thanx for helping me cox
0
 
galeboeAuthor Commented:
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
 
coxCommented:
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
 
galeboeAuthor Commented:
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
 
galeboeAuthor Commented:
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
 
coxCommented:
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
 
galeboeAuthor Commented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now