From: hackbard Date: Thu, 17 Oct 2002 14:31:40 +0000 (+0000) Subject: added datagram.c udp streaming server X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=3fc47cc4c46c600105395487d51a9dccaf53a4f9;p=my-code%2Fivac.git added datagram.c udp streaming server --- diff --git a/datagram.c b/datagram.c new file mode 100644 index 0000000..a140c32 --- /dev/null +++ b/datagram.c @@ -0,0 +1,75 @@ +/* datagram.c - datagram sockets server + * + * author: hackbard + * + */ + +#include +#include + +/* memset */ +#include + +/* socket and bind stuff */ +#include +#include + +/* sockkaddr_in */ +#include + +/* inet_ntoa ... */ +#include + +/* errno stuff ... */ +#include + +/* read, close */ +#include + +int main(int argc, char *argv[]) { + int send_fd; + struct sockaddr_in local_addr, remote_addr; + socklen_t remote_addr_len; + int send_bytes; + + if(argc!=3) { + printf("usage: %s \n",argv[0]); + exit(1); + } + + if((send_fd=socket(AF_INET,SOCK_DGRAM,0)) == -1) { + printf("can't open socket.\n"); + exit(1); + } + + memset(&local_addr,0,sizeof(local_addr)); + local_addr.sin_family=AF_INET; + local_addr.sin_port=htons(atoi(argv[2])); + local_addr.sin_addr.s_addr=htonl(INADDR_ANY); + + if(bind(send_fd,(struct sockaddr *)&local_addr,sizeof(local_addr))==-1) { + printf("unable to bind on port %d.\n",atoi(argv[1])); + perror("bind"); + exit(1); + } + + // remote_addr_len=sizeof(remote_addr); + memset(&remote_addr,0,sizeof(remote_addr)); + remote_addr.sin_family=AF_INET; + remote_addr.sin_port=htons(atoi(argv[2])); + remote_addr.sin_addr=inet_addr(argv[1]); + + /* send stuff .... */ + read_bytes=1; + while(read_bytes>0) { + unsigned char buf[1000]; + read_bytes=read(0,buf,sizeof(buf)); + send_bytes=sendto(send_fd,buf,sizeof(buf),&remote_addr,sizeof(remote_addr); + } + + close(send_fd); + printf("connection closed ...\n"); + printf("%d from %d total bytes sent.\n",send_bytes,read_bytes); + + return 0; +}