| | |
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
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windowsapi





