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

#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...

#!/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
    print msg
    cmd = "%s %s" % (f, site)
    if verbose:
        print cmd
        print
    
    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

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

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.

The test itself was faulty

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.