X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Ftestclient_src%2Fmain.c;h=1aaa9bddc721364460c62e5086ceb19067d320de;hb=5b487e31cf5145372e9777e9f82a8cd661d4f1b4;hp=3e049b09a7c21a4088df2b1463812fbd4203503f;hpb=402e274237161129c6e24e561cd3c11971a703be;p=tpg%2Facess2.git diff --git a/Usermode/Applications/testclient_src/main.c b/Usermode/Applications/testclient_src/main.c index 3e049b09..1aaa9bdd 100644 --- a/Usermode/Applications/testclient_src/main.c +++ b/Usermode/Applications/testclient_src/main.c @@ -4,6 +4,9 @@ #include #include #include +#include + + int OpenTCP(const char *AddressString, short PortNumber); /** * \fn int main(int argc, char *argv[]) @@ -13,20 +16,14 @@ int main(int argc, char *argv[]) { int con = -1; int len; - uint16_t port; - uint8_t buf[4] = {10,2,0,2}; uint8_t data[4096]; // Packet Data - - con = open("/Devices/ip/1/tcpc", OPENFLAG_READ|OPENFLAG_WRITE); + + con = OpenTCP("10.0.2.2", 80); if(con == -1) { - fprintf(stderr, "Unable top open TCP client '/Devices/ip/1/tcpc'\n"); + fprintf(stderr, "Unable to open TCP client\n"); return -1; } - port = 80; ioctl(con, 5, &port); // Set Remote Port - ioctl(con, 6, buf); // Set remote IP - ioctl(con, 7, NULL); // Connect - #define REQ_STR "GET / HTTP/1.1\r\n"\ "Host: sonata\r\n"\ "User-Agent: Acess2 TCP Test Client\r\n"\ @@ -34,17 +31,52 @@ int main(int argc, char *argv[]) write(con, sizeof(REQ_STR)-1, REQ_STR); - len = read(con, 4096, data); + while( (len = read(con, 4095, data)) > 0 ) + { + data[len] = 0; + _SysDebug("%i bytes - %s", len, data); + printf("%s", data); + } close(con); - if( len == -1 ) { - printf("Connection closed on us\n"); - return 0; + return 0; +} + +/** + * \brief Initialise a TCP connection to \a AddressString on port \a PortNumber + */ +int OpenTCP(const char *AddressString, short PortNumber) +{ + int fd, addrType; + uint8_t addrBuffer[16]; + + // Parse IP Address + addrType = Net_ParseAddress(AddressString, addrBuffer); + if( addrType == 0 ) { + fprintf(stderr, "Unable to parse '%s' as an IP address\n", AddressString); + return -1; } - if( len != 0 ) + + // Opens a R/W handle + fd = Net_OpenSocket(addrType, addrBuffer, "tcpc"); + if( fd == -1 ) { - printf("Packet Data: (%i bytes)\n", len); - printf("%s\n", data); - printf("--- EOP ---\n"); + fprintf(stderr, "Unable to open TCP Client\n"); + return -1; } - return 0; + + // Set remote port and address + ioctl(fd, 5, &PortNumber); + ioctl(fd, 6, addrBuffer); + + // Connect + if( ioctl(fd, 7, NULL) == 0 ) { + fprintf(stderr, "Unable to start connection\n"); + return -1; + } + + printf("Connection opened\n"); + + // Return descriptor + return fd; } +