| | |
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:
Solved Threads: 0
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
Here is the tests.py file that checks it...
And if i use hp = gethostbyaddr(hostname,sizeof(sin), AF_INET);
i get this back:
But if i use hp = gethostbyname(hostname);
i get this back:
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....
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
C Syntax (Toggle Plain Text)
#include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <netinet/in.h> #include <netdb.h> #include <unistd.h> #include <string.h> /* http://docs.sun.com/app/docs/doc/806-1017/6jab5di3g?a=view */ /* http://www.aquaphoenix.com/ref/gnu_c_library */ int main(int argc, char ** argv) { char *hostname; int sock, port=80; struct hostent *hp; struct sockaddr_in sin; /* kontrollime kahe argumendi olemasolu */ if ( argc == 2 )hostname = argv[1]; else { fprintf(stdout, "Error.\n"); exit(EXIT_FAILURE); } /* avame socketi */ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) { fprintf(stdout, "Error.\n"); exit(EXIT_FAILURE); } /* võtame hosti aadress */ //hp = gethostbyname(hostname); //hp = gethostbyaddr(hostname,sizeof(sin), AF_INET); if (hp == NULL || (hp ==NULL) { fprintf(stdout, "Server IP not found.\n"); close(sock); exit(EXIT_FAILURE); } /* anna serverile aadressi ja pordi kohu minna */ sin.sin_family = hp->h_addrtype; sin.sin_port = htons(port); /*kopeerin serverisse, mille külge ühendume */ memcpy((char *)&sin.sin_addr, (char *)hp->h_addr,hp->h_length); if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) { fprintf(stdout, "Does not support HTTP.\n"); close(sock); exit(EXIT_FAILURE); } else{ fprintf(stdout,"Supports HTTP.\n"); exit(EXIT_SUCCESS); } }
Here is the tests.py file that checks it...
C Syntax (Toggle Plain Text)
#!/usr/bin/env python # coding: utf-8 import sys, os verbose = 0 def run_test(f, site, expected, returnval, msg): """Fun function for provided tests""" if verbose: print msg cmd = "%s %s" % (f, site) if verbose: print cmd if bool(os.system(cmd)) != bool(returnval): if verbose: sys.stderr.write("Error, wrong return value\n") else: print "ERR (Return)" # compare files if map(lambda x: x[0] == x[1], zip([expected],os.popen(cmd).readlines()))[0]: print "OK" else: if verbose: sys.stderr.write("Output is not: %s" % (expected)) else: print "ERR" def test_working(f): run_test(f, "cs.ttu.ee", "Supports HTTTP.\n", 0, "Testing working HTTP.") def test_nonexistent(f): run_test(f, "cs.ttu.ee", "Server IP not found.\n", 1, "Testing non-existent server") def test_nonworking(f): run_test(f, "cs6.cs.ttu.ee", "Does not support HTTP.\n", 1, "Testing non-working HTTP (may take a while)") def test_args(f): run_test(f, "", "Error.\n", 1, "Testing without any arguments.") if __name__ == "__main__": progname = "./chkhttp" if len(sys.argv) > 1: progname = "/home/%s/CLABOR/alt7/chkhttp" % sys.argv[1] verbose = 0 if not os.access(progname, os.X_OK): sys.stderr.write("Error: Cannot run file: %s\n" % (progname)) exit(1) try: os.system("less /home/%s/CLABOR/alt7/chkhttp.c" % (sys.argv[1])) except: sys.stderr.write("Error: Canot display c-file\n") test_working(progname) test_nonworking(progname) test_args(progname) test_nonexistent(progname) 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
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
Server itself is some sort of linux....
![]() |
Other Threads in the C Forum
- Previous Thread: output in c programming
- Next Thread: help
Views: 578 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm command copyanyfile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework ide include initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf openwebfoundation overwrite pause pdf pointer posix power process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads turboc unix urboc user whythiscodecausesegmentationfault win32api windowsapi





