1 /* 2 * Copyright 2006, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Oliver Tappe, zooey@hirschkaefer.de 7 */ 8 9 10 #include <arpa/inet.h> 11 #include <netinet/in.h> 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <string.h> 15 #include <sys/socket.h> 16 17 #define MAXLEN 32768 18 19 20 static void 21 udp_client(int sockFD, const struct sockaddr_in* serverAddr) 22 { 23 char sendbuf[MAXLEN]; 24 long status; 25 26 while (!feof(stdin)) { 27 status = fread(sendbuf, 1, MAXLEN, stdin); 28 if (status < 0) { 29 printf("fread(): %lx (%s)\n", status, strerror(status)); 30 exit(5); 31 } 32 printf("trying to send %ld bytes...\n", status); 33 status = sendto(sockFD, sendbuf, status, 0, 34 (struct sockaddr*)serverAddr, sizeof(struct sockaddr_in)); 35 if (status < 0) { 36 printf("sendto(): %lx (%s)\n", status, strerror(status)); 37 exit(5); 38 } 39 } 40 } 41 42 43 int 44 main(int argc, char** argv) 45 { 46 long status; 47 int sockFD; 48 struct sockaddr_in serverAddr, clientAddr; 49 50 if (argc < 3) { 51 printf("usage: %s <IP-address> <port> [local-port]\n", argv[0]); 52 exit(5); 53 } 54 55 memset(&serverAddr, 0, sizeof(struct sockaddr_in)); 56 serverAddr.sin_family = AF_INET; 57 serverAddr.sin_port = htons(atoi(argv[2])); 58 serverAddr.sin_addr.s_addr = inet_addr(argv[1]); 59 printf("addr=%lx port=%u\n", serverAddr.sin_addr.s_addr, ntohs(serverAddr.sin_port)); 60 61 sockFD = socket(AF_INET, SOCK_DGRAM, 0); 62 63 if (argc > 3) { 64 memset(&clientAddr, 0, sizeof(struct sockaddr_in)); 65 clientAddr.sin_family = AF_INET; 66 clientAddr.sin_port = htons(atoi(argv[3])); 67 printf("binding to port %u\n", ntohs(clientAddr.sin_port)); 68 status = bind(sockFD, (struct sockaddr *)&clientAddr, sizeof(struct sockaddr_in)); 69 if (status < 0) { 70 printf("bind(): %lx (%s)\n", status, strerror(status)); 71 exit(5); 72 } 73 } 74 75 udp_client(sockFD, &serverAddr); 76 return 0; 77 } 78