Solved

Unix Networking

Posted on 1999-01-05
14
251 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
Comment Utility
What do you mean by located?
How about the ping command?
0
 

Author Comment

by:galeboe
Comment Utility
Edited text of question
0
 
LVL 22

Expert Comment

by:blu
Comment Utility
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
Comment Utility
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
Comment Utility
Edited text of question
0
 

Author Comment

by:galeboe
Comment Utility
Adjusted points to 70
0
 
LVL 1

Accepted Solution

by:
cox earned 70 total points
Comment Utility
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
New My Cloud Pro Series - organize everything!

With space to keep virtually everything, the My Cloud Pro Series offers your team the network storage to edit, save and share production files from anywhere with an internet connection. Compatible with both Mac and PC, you're able to protect your content regardless of OS.

 

Author Comment

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

Author Comment

by:galeboe
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: IanTh
Hi Guys After a whole weekend getting wake on lan over the internet working, I thought I would share the experience. Your firewall has to have a port forward for port 9 udp to your local broadcast x.x.x.255 but if that doesnt work, do it to a …
What’s a web proxy server? A proxy server is a server that goes between clients and web servers, used in corporate to enforce corporate browsing policy and ensure security. Proxy servers are commonly used in three modes. A)    Forward proxy …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now