From: hadaq Date: Wed, 18 Jul 2012 22:13:51 +0000 (+0000) Subject: TRB3 simulator added X-Git-Tag: v6.0~25 X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=4b985cfafbfc51d95c6ef02d9d436d1637ee0fa4;p=trbnettools.git TRB3 simulator added --- diff --git a/libtrbnet/trb3sim.c b/libtrbnet/trb3sim.c new file mode 100644 index 0000000..371d110 --- /dev/null +++ b/libtrbnet/trb3sim.c @@ -0,0 +1,76 @@ +/* upd-server.c + * + * Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in + * whole or in part in accordance to the General Public License (GPL). + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/*****************************************************************************/ +/*** upd-server.c ***/ +/*** ***/ +/*** Create a datagram server that waits for client messages (which it ***/ +/*** echoes back). ***/ +/*****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEFAULT_PORT 25000 + +int main(int count, char *strings[]) +{ + int sd; + int port = DEFAULT_PORT; + struct sockaddr_in addr; + uint16_t buffer[32768]; + + if (count != 2) + printf("usage: %s \n...Using default port (%d).\n", strings[0], + port); + else + port = atoi(strings[1]); + + sd = socket(PF_INET, SOCK_DGRAM, 0); + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = INADDR_ANY; + if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0) + perror("bind"); + while (1) { + int bytes; + socklen_t addr_len = sizeof(addr); + int i; + + bytes = + recvfrom(sd, (void*)buffer, sizeof(buffer), 0, (struct sockaddr *)&addr, + &addr_len); + printf("msg from %s:%d (%d bytes)\n", inet_ntoa(addr.sin_addr), + ntohs(addr.sin_port), bytes); + for (i = 0; i < bytes / 2; i++) { + fprintf(stderr, "%d 0x%04x\n", i, buffer[i]); + } + sleep(6); + sendto(sd, buffer, bytes, 0, (struct sockaddr *)&addr, sizeof(addr)); + fprintf(stderr, "sendto done\n"); + } + close(sd); +}