Willy Tarreau | 06d73f7 | 2010-03-24 18:43:56 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | #include <netinet/in.h> |
| 6 | #include <arpa/inet.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/socket.h> |
| 9 | #include <fcntl.h> |
| 10 | |
| 11 | int main(int argc, char **argv) { |
| 12 | char *addr; |
| 13 | int port; |
| 14 | int sock; |
| 15 | struct sockaddr_in saddr; |
| 16 | const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 }; |
| 17 | |
| 18 | if (argc < 4) { |
| 19 | fprintf(stderr, |
| 20 | "usage : %s <addr> <port> <string>\n" |
| 21 | " This will connect to TCP port <addr>:<port> and send string <string>\n" |
| 22 | " then immediately reset.\n", |
| 23 | argv[0]); |
| 24 | exit(1); |
| 25 | } |
| 26 | |
| 27 | addr = argv[1]; |
| 28 | port = atoi(argv[2]); |
| 29 | |
| 30 | sock = socket(AF_INET, SOCK_STREAM, 0); |
| 31 | bzero(&saddr, sizeof(saddr)); |
| 32 | saddr.sin_addr.s_addr = inet_addr(addr); |
| 33 | saddr.sin_port = htons(port); |
| 34 | saddr.sin_family = AF_INET; |
| 35 | |
| 36 | if (connect(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) { |
| 37 | perror("connect"); |
| 38 | exit(1); |
| 39 | } |
| 40 | |
| 41 | send(sock, argv[3], strlen(argv[3]), MSG_DONTWAIT | MSG_NOSIGNAL); |
| 42 | setsockopt(sock, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 43 | close(sock); |
| 44 | exit(0); |
| 45 | } |