From: Alan Cox Date: Thu, 25 Feb 2016 21:50:35 +0000 (+0000) Subject: socktest: this won't live here forever X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=fb60bfc976874996f52f6cc111c766d761b7c84b;p=FUZIX.git socktest: this won't live here forever Some initial socket testing code --- diff --git a/Applications/util/socktest.c b/Applications/util/socktest.c new file mode 100644 index 00000000..d2ad1a40 --- /dev/null +++ b/Applications/util/socktest.c @@ -0,0 +1,69 @@ +#include +#include +#include + +#define AF_INET 1 +#define SOCK_STREAM 3 +#define IPPROTO_TCP 6 + +struct in_addr { + uint32_t s_addr; +}; + +struct sockaddr_in { + uint16_t sin_family; + uint16_t sin_port; + struct in_addr sin_addr; + uint8_t sin_zero[8]; +}; + +struct sockaddr_in addr = { + AF_INET, + 0xEA1D, + { 0xF400A8C0 }, +}; + +struct sockaddr_in laddr = { + AF_INET, + 0x0A0A, + { 0x010000C0 }, +}; + +int main(int argc, char *argv[]) +{ + int fd; + int r; + char buf[5]; + + fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (fd < 0) + perror("af_inet sock_stream pf_tcp"); + close(fd); + + fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) { + perror("af_inet sock_stream 0"); + exit(1); + } + if (bind(fd, (struct sockaddr *)&laddr, sizeof(addr)) < 0) { + perror("bind"); + exit(1); + } + + if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + perror("connect"); + exit(1); + } + if (write(fd, "Hello", 5) == -1) { + perror("write"); + exit(1); + } + if ((r = read(fd, buf, 5)) < 0) { + perror("read"); + exit(1); + } + printf("Read %d bytes\n", r); + write(1, buf, r); + close(fd); + return 0; +}