From 14cf6a9f8be455035baa81d4552ff927350cc5d0 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sat, 2 Jul 2011 12:38:01 +0800 Subject: [PATCH] Usermode/testclient - Fixes for debugging --- Usermode/Applications/testclient_src/Makefile | 1 + Usermode/Applications/testclient_src/main.c | 68 ++++++++++++++----- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/Usermode/Applications/testclient_src/Makefile b/Usermode/Applications/testclient_src/Makefile index ae6b8235..de475a11 100644 --- a/Usermode/Applications/testclient_src/Makefile +++ b/Usermode/Applications/testclient_src/Makefile @@ -4,6 +4,7 @@ OBJ = main.o BIN = testclient +LDFLAGS += -lnet -include ../Makefile.tpl 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; } + -- 2.20.1