Help with http port cheker which uses sockets

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2007
Posts: 23
Reputation: kohuke is an unknown quantity at this point 
Solved Threads: 0
kohuke kohuke is offline Offline
Newbie Poster

Help with http port cheker which uses sockets

 
0
  #1
Oct 30th, 2008
Hi.
The program works fine if i just compile and run.
But for the program to get graded it has to pass a test written in python which it does not.
Since i'm complete dummy at c and programming if you find to explain, dumb it down please...

so here is my code
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9.  
  10. /* http://docs.sun.com/app/docs/doc/806-1017/6jab5di3g?a=view */
  11. /* http://www.aquaphoenix.com/ref/gnu_c_library */
  12. int main(int argc, char ** argv) {
  13. char *hostname;
  14. int sock, port=80;
  15. struct hostent *hp;
  16. struct sockaddr_in sin;
  17.  
  18. /* kontrollime kahe argumendi olemasolu */
  19. if ( argc == 2 )hostname = argv[1];
  20. else
  21. {
  22. fprintf(stdout, "Error.\n");
  23. exit(EXIT_FAILURE);
  24. }
  25.  
  26. /* avame socketi */
  27. sock = socket(AF_INET, SOCK_STREAM, 0);
  28. if (sock == -1)
  29. {
  30. fprintf(stdout, "Error.\n");
  31. exit(EXIT_FAILURE);
  32. }
  33.  
  34. /* võtame hosti aadress */
  35.  
  36. //hp = gethostbyname(hostname);
  37. //hp = gethostbyaddr(hostname,sizeof(sin), AF_INET);
  38. if (hp == NULL || (hp ==NULL)
  39. {
  40. fprintf(stdout, "Server IP not found.\n");
  41. close(sock);
  42. exit(EXIT_FAILURE);
  43. }
  44. /* anna serverile aadressi ja pordi kohu minna */
  45. sin.sin_family = hp->h_addrtype;
  46. sin.sin_port = htons(port);
  47. /*kopeerin serverisse, mille külge ühendume */
  48. memcpy((char *)&sin.sin_addr, (char *)hp->h_addr,hp->h_length);
  49. if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
  50. {
  51. fprintf(stdout, "Does not support HTTP.\n");
  52. close(sock);
  53. exit(EXIT_FAILURE);
  54. }
  55.  
  56. else{
  57. fprintf(stdout,"Supports HTTP.\n");
  58. exit(EXIT_SUCCESS);
  59. }
  60.  
  61.  
  62. }

Here is the tests.py file that checks it...
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. import sys, os
  4.  
  5. verbose = 0
  6.  
  7. def run_test(f, site, expected, returnval, msg):
  8. """Fun function for provided tests"""
  9. if verbose:
  10. print
  11. print msg
  12. cmd = "%s %s" % (f, site)
  13. if verbose:
  14. print cmd
  15. print
  16.  
  17. if bool(os.system(cmd)) != bool(returnval):
  18. if verbose:
  19. sys.stderr.write("Error, wrong return value\n")
  20. else:
  21. print "ERR (Return)"
  22.  
  23. # compare files
  24. if map(lambda x: x[0] == x[1], zip([expected],os.popen(cmd).readlines()))[0]:
  25. print "OK"
  26. else:
  27. if verbose:
  28. sys.stderr.write("Output is not: %s" % (expected))
  29. else:
  30. print "ERR"
  31.  
  32.  
  33.  
  34. def test_working(f):
  35. run_test(f, "cs.ttu.ee", "Supports HTTTP.\n", 0, "Testing working HTTP.")
  36.  
  37. def test_nonexistent(f):
  38. run_test(f, "cs.ttu.ee", "Server IP not found.\n", 1, "Testing non-existent server")
  39.  
  40. def test_nonworking(f):
  41. run_test(f, "cs6.cs.ttu.ee", "Does not support HTTP.\n", 1, "Testing non-working HTTP (may take a while)")
  42.  
  43. def test_args(f):
  44. run_test(f, "", "Error.\n", 1, "Testing without any arguments.")
  45.  
  46.  
  47.  
  48. if __name__ == "__main__":
  49. progname = "./chkhttp"
  50. if len(sys.argv) > 1:
  51. progname = "/home/%s/CLABOR/alt7/chkhttp" % sys.argv[1]
  52. verbose = 0
  53. if not os.access(progname, os.X_OK):
  54. sys.stderr.write("Error: Cannot run file: %s\n" % (progname))
  55. exit(1)
  56. try:
  57. os.system("less /home/%s/CLABOR/alt7/chkhttp.c" % (sys.argv[1]))
  58. except:
  59. sys.stderr.write("Error: Canot display c-file\n")
  60.  
  61.  
  62. test_working(progname)
  63. test_nonworking(progname)
  64. test_args(progname)
  65. test_nonexistent(progname)
  66.  
  67. exit(0)

And if i use hp = gethostbyaddr(hostname,sizeof(sin), AF_INET);

i get this back:
Testing working HTTP.
Server IP not found.
ERR (Return)
ERR
Testing non-working HTTP (may take a while)
Server IP not found.
ERR
Testing without any arguments.
Error.
OK
Testing non-existent server
Server IP not found.
OK
But if i use hp = gethostbyname(hostname);
i get this back:
Testing working HTTP.
Supports HTTP.
ERR
Testing non-working HTTP (may take a while)
Does not support HTTP.
OK
Testing without any arguments.
Error.
OK
Testing non-existent server
Supports HTTP.
ERR (Return)
ERR
Can anyone tell me how to fix my code so that atleast the non-working HTTP and non-existent servers tests pass together. I have tried experiment, but that only lead to more errors.

Server itself is some sort of linux....
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 23
Reputation: kohuke is an unknown quantity at this point 
Solved Threads: 0
kohuke kohuke is offline Offline
Newbie Poster

Re: Help with http port cheker which uses sockets

 
0
  #2
Nov 3rd, 2008
Cannot really no one help me? I'm stuck on this problem?

It just needs to check weather a server uses http port 80 or not....

Please ^^ help me.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 23
Reputation: kohuke is an unknown quantity at this point 
Solved Threads: 0
kohuke kohuke is offline Offline
Newbie Poster

Re: Help with http port cheker which uses sockets

 
0
  #3
Nov 10th, 2008
The test itself was faulty
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC