[92e717c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup nettest
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
| 34 | * Networking test 3.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <async.h>
|
---|
| 38 | #include <stdio.h>
|
---|
| 39 | #include <str.h>
|
---|
| 40 |
|
---|
| 41 | #include <net/in.h>
|
---|
| 42 | #include <net/in6.h>
|
---|
| 43 | #include <net/inet.h>
|
---|
| 44 | #include <net/socket.h>
|
---|
| 45 |
|
---|
| 46 | #define BUF_SIZE 32
|
---|
| 47 |
|
---|
| 48 | static char *data;
|
---|
| 49 | static size_t size;
|
---|
| 50 |
|
---|
| 51 | static char buf[BUF_SIZE];
|
---|
| 52 |
|
---|
| 53 | static struct sockaddr_in addr;
|
---|
| 54 |
|
---|
| 55 | static uint16_t port;
|
---|
| 56 |
|
---|
| 57 | int main(int argc, char *argv[])
|
---|
| 58 | {
|
---|
| 59 | int rc;
|
---|
| 60 | int fd;
|
---|
[453f9e33] | 61 | char *endptr;
|
---|
[92e717c] | 62 |
|
---|
[453f9e33] | 63 | port = 7;
|
---|
[92e717c] | 64 |
|
---|
| 65 | data = (char *)"Hello World!";
|
---|
| 66 | size = str_size(data);
|
---|
| 67 |
|
---|
[453f9e33] | 68 | /* Connect to local IP address by default */
|
---|
[92e717c] | 69 | addr.sin_family = AF_INET;
|
---|
| 70 | addr.sin_port = htons(port);
|
---|
| 71 | addr.sin_addr.s_addr = htonl(0x7f000001);
|
---|
| 72 |
|
---|
[453f9e33] | 73 | if (argc >= 2) {
|
---|
| 74 | printf("parsing address '%s'\n", argv[1]);
|
---|
| 75 | rc = inet_pton(AF_INET, argv[1], (uint8_t *)&addr.sin_addr.s_addr);
|
---|
| 76 | if (rc != EOK) {
|
---|
| 77 | fprintf(stderr, "Error parsing address\n");
|
---|
| 78 | return 1;
|
---|
| 79 | }
|
---|
| 80 | printf("result: rc=%d, family=%d, addr=%x\n", rc,
|
---|
| 81 | addr.sin_family, addr.sin_addr.s_addr);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if (argc >= 3) {
|
---|
| 85 | printf("parsing port '%s'\n", argv[2]);
|
---|
| 86 | addr.sin_port = htons(strtoul(argv[2], &endptr, 10));
|
---|
| 87 | if (*endptr != '\0') {
|
---|
| 88 | fprintf(stderr, "Error parsing port\n");
|
---|
| 89 | return 1;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[92e717c] | 93 | printf("socket()\n");
|
---|
| 94 | fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
---|
| 95 | printf(" -> %d\n", fd);
|
---|
| 96 | if (fd < 0)
|
---|
| 97 | return 1;
|
---|
| 98 |
|
---|
| 99 | printf("connect()\n");
|
---|
| 100 | rc = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
|
---|
| 101 | printf(" -> %d\n", rc);
|
---|
| 102 | if (rc != 0)
|
---|
| 103 | return 1;
|
---|
| 104 |
|
---|
| 105 | printf("send()\n");
|
---|
| 106 | rc = send(fd, data, size, 0);
|
---|
| 107 | printf(" -> %d\n", rc);
|
---|
| 108 | if (rc < 0)
|
---|
| 109 | return 1;
|
---|
| 110 |
|
---|
| 111 | do {
|
---|
| 112 | printf("recv()\n");
|
---|
| 113 | rc = recv(fd, buf, BUF_SIZE, 0);
|
---|
| 114 | printf(" -> %d\n", rc);
|
---|
| 115 | } while (rc > 0);
|
---|
| 116 |
|
---|
[b0d82d1] | 117 | async_usleep(1000*1000);
|
---|
[92e717c] | 118 |
|
---|
| 119 | printf("closesocket()\n");
|
---|
| 120 | rc = closesocket(fd);
|
---|
| 121 | printf(" -> %d\n", rc);
|
---|
| 122 |
|
---|
| 123 | return 0;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | /** @}
|
---|
| 128 | */
|
---|