Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unix Networking

Posted on 1999-01-05
14
Medium Priority
?
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This program is used to assist in finding and resolving common problems with wireless connections.
Tech spooks aren't just for those who are tech savvy, it also happens to those of us running a business. Check out the top tech spooks for business owners.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

618 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