apps/examples/udgram: Add a simple test of Unix domain datagram sockets
This commit is contained in:
parent
1c2cb493e6
commit
f429f54aa2
@ -75,6 +75,7 @@ source "$APPSDIR/examples/discover/Kconfig"
|
||||
source "$APPSDIR/examples/webserver/Kconfig"
|
||||
source "$APPSDIR/examples/usbserial/Kconfig"
|
||||
source "$APPSDIR/examples/usbterm/Kconfig"
|
||||
source "$APPSDIR/examples/udgram/Kconfig"
|
||||
source "$APPSDIR/examples/ustream/Kconfig"
|
||||
source "$APPSDIR/examples/watchdog/Kconfig"
|
||||
source "$APPSDIR/examples/wget/Kconfig"
|
||||
|
@ -318,6 +318,10 @@ ifeq ($(CONFIG_EXAMPLES_USBTERM),y)
|
||||
CONFIGURED_APPS += examples/usbterm
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_EXAMPLES_UDGRAM),y)
|
||||
CONFIGURED_APPS += examples/udgram
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_EXAMPLES_USTREAM),y)
|
||||
CONFIGURED_APPS += examples/ustream
|
||||
endif
|
||||
|
@ -45,7 +45,7 @@ SUBDIRS += nxffs nxflat nxhello nximage nxlines nxtext ostest pashello pipe
|
||||
SUBDIRS += poll posix_spawn pwm qencoder random relays rgmp romfs sendmail
|
||||
SUBDIRS += serialblaster serloop serialrx slcd smart smart_test tcpecho
|
||||
SUBDIRS += telnetd thttpd tiff timer touchscreen udp usbserial usbterm
|
||||
SUBDIRS += ustream watchdog webserver wget wgetjson xmlrpc
|
||||
SUBDIRS += ustream udgram watchdog webserver wget wgetjson xmlrpc
|
||||
|
||||
# Sub-directories that might need context setup. Directories may need
|
||||
# context setup for a variety of reasons, but the most common is because
|
||||
@ -59,7 +59,7 @@ CNTXTDIRS += djoystick flash_test ftpd hello helloxx i2schar json keypadtest
|
||||
CNTXTDIRS += ltdc modbus lcdrw mtdpart mtdrwb netpkt nettest nx nxhello
|
||||
CNTXTDIRS += nximage nxlines nxtext nrf24l01_term ostest random relays
|
||||
CNTXTDIRS += qencoder serialblasters lcd serialrx smart_test tcpecho telnetd
|
||||
CNTXTDIRS += tiff timer touchscreen usbterm ustream watchdog wgetjson
|
||||
CNTXTDIRS += tiff timer touchscreen usbterm udgram ustream watchdog wgetjson
|
||||
endif
|
||||
|
||||
all: nothing
|
||||
|
@ -1979,8 +1979,22 @@ examples/usbterm
|
||||
examples/ustream
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
||||
This is the same test as examples/nettest, but using Unix domain
|
||||
sockets.
|
||||
This is the same test as examples/udp and similar to examples/ustream,
|
||||
but using Unix domain datagram sockets.
|
||||
|
||||
Dependencies:
|
||||
CONFIG_NET_LOCAL - Depends on support for Unix domain sockets
|
||||
|
||||
Configuration:
|
||||
CONFIG_EXAMPLES_UDGRAM - Enables the Unix domain socket example.
|
||||
CONFIG_EXAMPLES_UDGRAM_ADDR - Specifics the Unix domain address.
|
||||
Default "/var/fifo/fifo".
|
||||
|
||||
examples/ustream
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
||||
This is the same test as examples/udp and similar to examples/udgram,
|
||||
but using Unix domain stream sockets.
|
||||
|
||||
Dependencies:
|
||||
CONFIG_NET_LOCAL - Depends on support for Unix domain sockets
|
||||
|
13
examples/udgram/.gitignore
vendored
Normal file
13
examples/udgram/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
/Make.dep
|
||||
/.depend
|
||||
/.built
|
||||
/*.asm
|
||||
/*.obj
|
||||
/*.rel
|
||||
/*.lst
|
||||
/*.sym
|
||||
/*.adb
|
||||
/*.lib
|
||||
/*.src
|
||||
/*.exe
|
||||
/*.dSYM
|
19
examples/udgram/Kconfig
Normal file
19
examples/udgram/Kconfig
Normal file
@ -0,0 +1,19 @@
|
||||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see misc/tools/kconfig-language.txt.
|
||||
#
|
||||
|
||||
config EXAMPLES_UDGRAM
|
||||
bool "Unix domain datagram example"
|
||||
default n
|
||||
depends on NET_LOCAL
|
||||
---help---
|
||||
Enable the Unix domain SOCK_DGRAM test example
|
||||
|
||||
if EXAMPLES_UDGRAM
|
||||
|
||||
config EXAMPLES_UDGRAM_ADDR
|
||||
string "Unix domain address"
|
||||
default "/var/fifo/fifo"
|
||||
|
||||
endif # EXAMPLES_UDGRAM
|
173
examples/udgram/Makefile
Normal file
173
examples/udgram/Makefile
Normal file
@ -0,0 +1,173 @@
|
||||
############################################################################
|
||||
# examples/udgram/Makefile
|
||||
#
|
||||
# Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions
|
||||
# are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in
|
||||
# the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# 3. Neither the name NuttX nor the names of its contributors may be
|
||||
# used to endorse or promote products derived from this software
|
||||
# without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
# COPYRIGHT OWNER 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
-include $(TOPDIR)/.config
|
||||
-include $(TOPDIR)/Make.defs
|
||||
include $(APPDIR)/Make.defs
|
||||
|
||||
# Basic Unix domain networking test
|
||||
|
||||
SERVER_ASRCS =
|
||||
SERVER_AOBJS = $(SERVER_ASRCS:.S=$(OBJEXT))
|
||||
|
||||
SERVER_CSRCS =
|
||||
SERVER_COBJS = $(SERVER_CSRCS:.c=$(OBJEXT))
|
||||
|
||||
SERVER_MAINSRC = udgram_server.c
|
||||
SERVER_MAINOBJ = $(SERVER_MAINSRC:.c=$(OBJEXT))
|
||||
|
||||
SERVER_SRCS = $(SERVER_ASRCS) $(SERVER_CSRCS) $(SERVER_MAINSRC)
|
||||
SERVER_OBJS = $(SERVER_AOBJS) $(SERVER_COBJS)
|
||||
|
||||
SERVER_PROGNAME = server$(EXEEXT)
|
||||
|
||||
SERVER_APPNAME = server
|
||||
SERVER_PRIORITY = SCHED_PRIORITY_DEFAULT
|
||||
SERVER_STACKSIZE = 2048
|
||||
|
||||
CLIENT_ASRCS =
|
||||
CLIENT_AOBJS = $(CLIENT_ASRCS:.S=$(OBJEXT))
|
||||
|
||||
CLIENT_CSRCS =
|
||||
CLIENT_COBJS = $(CLIENT_CSRCS:.c=$(OBJEXT))
|
||||
|
||||
CLIENT_MAINSRC = udgram_client.c
|
||||
CLIENT_MAINOBJ = $(CLIENT_MAINSRC:.c=$(OBJEXT))
|
||||
|
||||
CLIENT_SRCS = $(CLIENT_ASRCS) $(CLIENT_CSRCS) $(CLIENT_MAINSRC)
|
||||
CLIENT_OBJS = $(CLIENT_AOBJS) $(CLIENT_COBJS)
|
||||
|
||||
CLIENT_PROGNAME = server$(EXEEXT)
|
||||
|
||||
CLIENT_APPNAME = client
|
||||
CLIENT_PRIORITY = SCHED_PRIORITY_DEFAULT
|
||||
CLIENT_STACKSIZE = 2048
|
||||
|
||||
AOBJS = $(CLIENT_AOBJS) $(SERVER_AOBJS)
|
||||
COBJS = $(CLIENT_COBJS) $(CLIENT_MAINOBJ) $(SERVER_COBJS) $(SERVER_MAINOBJ)
|
||||
|
||||
SRCS = $(CLIENT_SRCS) $(SERVER_SRCS)
|
||||
OBJS = $(AOBJS) $(COBJS)
|
||||
|
||||
ifneq ($(CONFIG_BUILD_KERNEL),y)
|
||||
SERVER_OBJS += $(SERVER_MAINOBJ)
|
||||
CLIENT_OBJS += $(CLIENT_MAINOBJ)
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
|
||||
UDGRAM_BIN = ..\..\libapps$(LIBEXT)
|
||||
else
|
||||
ifeq ($(WINTOOL),y)
|
||||
UDGRAM_BIN = ..\\..\\libapps$(LIBEXT)
|
||||
else
|
||||
UDGRAM_BIN = ../../libapps$(LIBEXT)
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(WINTOOL),y)
|
||||
INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}"
|
||||
else
|
||||
INSTALL_DIR = $(BIN_DIR)
|
||||
endif
|
||||
|
||||
# Common build
|
||||
|
||||
VPATH =
|
||||
|
||||
all: .built
|
||||
.PHONY: clean depend distclean
|
||||
|
||||
$(AOBJS): %$(OBJEXT): %.S
|
||||
$(call ASSEMBLE, $<, $@)
|
||||
|
||||
$(COBJS): %$(OBJEXT): %.c
|
||||
$(call COMPILE, $<, $@)
|
||||
|
||||
.built: $(OBJS)
|
||||
$(call ARCHIVE, $(UDGRAM_BIN), $(OBJS))
|
||||
$(Q) touch .built
|
||||
|
||||
ifeq ($(CONFIG_BUILD_KERNEL),y)
|
||||
|
||||
$(BIN_DIR)$(DELIM)$(CLIENT_PROGNAME): $(CLIENT_OBJS)
|
||||
@echo "LD: $(CLIENT_PROGNAME)"
|
||||
$(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(CLIENT_PROGNAME) $(ARCHCRT0OBJ) $(CLIENT_MAINOBJ) $(LDLIBS)
|
||||
$(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(CLIENT_PROGNAME)
|
||||
|
||||
$(BIN_DIR)$(DELIM)$(SERVER_PROGNAME): $(OBJS) $(SERVER_MAINOBJ)
|
||||
@echo "LD: $(SERVER_PROGNAME)"
|
||||
$(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(SERVER_PROGNAME) $(ARCHCRT0OBJ) $(SERVER_MAINOBJ) $(LDLIBS)
|
||||
$(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(SERVER_PROGNAME)
|
||||
|
||||
install: $(BIN_DIR)$(DELIM)$(CLIENT_PROGNAME) $(BIN_DIR)$(DELIM)$(SERVER_PROGNAME)
|
||||
|
||||
else
|
||||
install:
|
||||
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
|
||||
|
||||
$(BUILTIN_REGISTRY)$(DELIM)$(CLIENT_APPNAME)_main.bdat: $(DEPCONFIG) Makefile
|
||||
$(call REGISTER,$(CLIENT_APPNAME),$(CLIENT_PRIORITY),$(CLIENT_STACKSIZE),$(CLIENT_APPNAME)_main)
|
||||
|
||||
$(BUILTIN_REGISTRY)$(DELIM)$(SERVER_APPNAME)_main.bdat: $(DEPCONFIG) Makefile
|
||||
$(call REGISTER,$(SERVER_APPNAME),$(SERVER_PRIORITY),$(SERVER_STACKSIZE),$(SERVER_APPNAME)_main)
|
||||
|
||||
context: $(BUILTIN_REGISTRY)$(DELIM)$(CLIENT_APPNAME)_main.bdat $(BUILTIN_REGISTRY)$(DELIM)$(SERVER_APPNAME)_main.bdat
|
||||
|
||||
else
|
||||
|
||||
context:
|
||||
|
||||
endif
|
||||
|
||||
.depend: Makefile $(SERVER_SRCS)
|
||||
@$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SERVER_SRCS) >Make.dep
|
||||
@touch $@
|
||||
|
||||
depend: .depend
|
||||
|
||||
clean:
|
||||
$(call DELFILE, .built)
|
||||
$(call DELFILE, *.dSYM)
|
||||
$(call CLEAN)
|
||||
|
||||
distclean: clean
|
||||
$(call DELFILE, Make.dep)
|
||||
$(call DELFILE, .depend)
|
||||
|
||||
-include Make.dep
|
||||
|
54
examples/udgram/udgram.h
Normal file
54
examples/udgram/udgram.h
Normal file
@ -0,0 +1,54 @@
|
||||
/****************************************************************************
|
||||
* examples/udgram/udgram.h
|
||||
*
|
||||
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __EXAMPLES_UDGRAM_UDGRAM_H
|
||||
#define __EXAMPLES_UDGRAM_UDGRAM_H
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define ASCIISIZE (0x7f - 0x20)
|
||||
#define SENDSIZE (ASCIISIZE+1)
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
#endif /* __EXAMPLES_UDGRAM_UDGRAM_H */
|
152
examples/udgram/udgram_client.c
Normal file
152
examples/udgram/udgram_client.c
Normal file
@ -0,0 +1,152 @@
|
||||
/****************************************************************************
|
||||
* examples/udp/udp-client.c
|
||||
*
|
||||
* Copyright (C) 2007, 2015 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name Gregory Nutt nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "udgram.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static inline void fill_buffer(unsigned char *buf, int offset)
|
||||
{
|
||||
int ch;
|
||||
int j;
|
||||
|
||||
buf[0] = offset;
|
||||
for (ch = 0x20, j = offset + 1; ch < 0x7f; ch++, j++)
|
||||
{
|
||||
if (j >= SENDSIZE)
|
||||
{
|
||||
j = 1;
|
||||
}
|
||||
buf[j] = ch;
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_BUILD_KERNEL
|
||||
int main(int argc, FAR char *argv[])
|
||||
#else
|
||||
int client_main(int argc, char *argv[])
|
||||
#endif
|
||||
{
|
||||
struct sockaddr_un server;
|
||||
unsigned char outbuf[SENDSIZE];
|
||||
socklen_t addrlen;
|
||||
int sockfd;
|
||||
int nbytes;
|
||||
int offset;
|
||||
|
||||
/* Create a new UDP socket */
|
||||
|
||||
sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
printf("client socket failure %d\n", errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Then send and receive 256 messages */
|
||||
|
||||
for (offset = 0; offset < 256; offset++)
|
||||
{
|
||||
/* Set up the output buffer */
|
||||
|
||||
fill_buffer(outbuf, offset);
|
||||
|
||||
/* Set up the server address */
|
||||
|
||||
addrlen = strlen(CONFIG_EXAMPLES_UDGRAM_ADDR);
|
||||
if (addrlen > UNIX_PATH_MAX - 1)
|
||||
{
|
||||
addrlen = UNIX_PATH_MAX - 1;
|
||||
}
|
||||
|
||||
server.sun_family = AF_LOCAL;
|
||||
strncpy(server.sun_path, CONFIG_EXAMPLES_UDGRAM_ADDR, addrlen);
|
||||
server.sun_path[addrlen] = '\0';
|
||||
|
||||
addrlen += sizeof(sa_family_t) + 1;
|
||||
|
||||
/* Send the message */
|
||||
|
||||
printf("client: %d. Sending %d bytes\n", offset, SENDSIZE);
|
||||
nbytes = sendto(sockfd, outbuf, SENDSIZE, 0,
|
||||
(struct sockaddr *)&server, addrlen);
|
||||
printf("client: %d. Sent %d bytes\n", offset, nbytes);
|
||||
|
||||
if (nbytes < 0)
|
||||
{
|
||||
printf("client: %d. sendto failed: %d\n", offset, errno);
|
||||
close(sockfd);
|
||||
return 1;
|
||||
}
|
||||
else if (nbytes != SENDSIZE)
|
||||
{
|
||||
printf("client: %d. Bad send length: %d Expected: %d\n",
|
||||
offset, nbytes, SENDSIZE);
|
||||
close(sockfd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Now, sleep a bit. No packets should be dropped due to overrunning
|
||||
* the server.
|
||||
*/
|
||||
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
close(sockfd);
|
||||
return 0;
|
||||
}
|
208
examples/udgram/udgram_server.c
Normal file
208
examples/udgram/udgram_server.c
Normal file
@ -0,0 +1,208 @@
|
||||
/****************************************************************************
|
||||
* examples/udgram/udgram_server.c
|
||||
*
|
||||
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name Gregory Nutt nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT OWNER 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "udgram.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static inline int check_buffer(unsigned char *buf)
|
||||
{
|
||||
int ret = 1;
|
||||
int offset;
|
||||
int ch;
|
||||
int j;
|
||||
|
||||
offset = buf[0];
|
||||
for (ch = 0x20, j = offset + 1; ch < 0x7f; ch++, j++)
|
||||
{
|
||||
if (j >= SENDSIZE)
|
||||
{
|
||||
j = 1;
|
||||
}
|
||||
if (buf[j] != ch)
|
||||
{
|
||||
printf("server: Buffer content error for offset=%d, index=%d\n", offset, j);
|
||||
ret = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_BUILD_KERNEL
|
||||
int main(int argc, FAR char *argv[])
|
||||
#else
|
||||
int server_main(int argc, char *argv[])
|
||||
#endif
|
||||
{
|
||||
struct sockaddr_un server;
|
||||
struct sockaddr_un client;
|
||||
unsigned char inbuf[1024];
|
||||
socklen_t addrlen;
|
||||
socklen_t recvlen;
|
||||
int sockfd;
|
||||
int nbytes;
|
||||
int optval;
|
||||
int offset;
|
||||
|
||||
/* Create a new UDP socket */
|
||||
|
||||
sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
printf("server: socket failure: %d\n", errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Set socket to reuse address */
|
||||
|
||||
optval = 1;
|
||||
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
||||
{
|
||||
printf("server: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Bind the socket to a local address */
|
||||
|
||||
addrlen = strlen(CONFIG_EXAMPLES_UDGRAM_ADDR);
|
||||
if (addrlen > UNIX_PATH_MAX - 1)
|
||||
{
|
||||
addrlen = UNIX_PATH_MAX - 1;
|
||||
}
|
||||
|
||||
server.sun_family = AF_LOCAL;
|
||||
strncpy(server.sun_path, CONFIG_EXAMPLES_UDGRAM_ADDR, addrlen);
|
||||
server.sun_path[addrlen] = '\0';
|
||||
|
||||
addrlen += sizeof(sa_family_t) + 1;
|
||||
|
||||
if (bind(sockfd, (struct sockaddr*)&server, addrlen) < 0)
|
||||
{
|
||||
printf("server: bind failure: %d\n", errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Then receive up to 256 packets of data */
|
||||
|
||||
for (offset = 0; offset < 256; offset++)
|
||||
{
|
||||
printf("server: %d. Receiving up 1024 bytes\n", offset);
|
||||
recvlen = addrlen;
|
||||
nbytes = recvfrom(sockfd, inbuf, 1024, 0,
|
||||
(struct sockaddr*)&client, &recvlen);
|
||||
|
||||
if (recvlen < sizeof(sa_family_t))
|
||||
{
|
||||
printf("server: %d. ERROR Received %d bytes from client with invalid length %d\n",
|
||||
offset, nbytes, recvlen);
|
||||
}
|
||||
else if (recvlen == sizeof(sa_family_t))
|
||||
{
|
||||
printf("server: %d. Received %d bytes from a nameless client\n",
|
||||
offset, nbytes);
|
||||
}
|
||||
else if (recvlen == sizeof(sa_family_t) + 1)
|
||||
{
|
||||
printf("server: %d. Received %d bytes from an abstract client\n",
|
||||
offset, nbytes);
|
||||
}
|
||||
else /* if (recvlen > sizeof(sa_family_t) */
|
||||
{
|
||||
printf("server: %d. Received %d bytes from a client %s\n",
|
||||
offset, nbytes, client.sun_path);
|
||||
}
|
||||
|
||||
if (nbytes < 0)
|
||||
{
|
||||
printf("server: %d. recv failed: %d\n", offset, errno);
|
||||
close(sockfd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (nbytes != SENDSIZE)
|
||||
{
|
||||
printf("server: %d. recv size incorrect: %d vs %d\n",
|
||||
offset, nbytes, SENDSIZE);
|
||||
close(sockfd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (offset < inbuf[0])
|
||||
{
|
||||
printf("server: %d. %d packets lost, resetting offset\n",
|
||||
offset, inbuf[0] - offset);
|
||||
offset = inbuf[0];
|
||||
}
|
||||
else if (offset > inbuf[0])
|
||||
{
|
||||
printf("server: %d. Bad offset in buffer: %d\n",
|
||||
offset, inbuf[0]);
|
||||
close(sockfd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!check_buffer(inbuf))
|
||||
{
|
||||
printf("server: %d. Bad buffer contents\n", offset);
|
||||
close(sockfd);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
close(sockfd);
|
||||
return 0;
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
#
|
||||
|
||||
config EXAMPLES_USTREAM
|
||||
bool "Unix domain socket example"
|
||||
bool "Unix domain stream example"
|
||||
default n
|
||||
depends on NET_LOCAL
|
||||
---help---
|
||||
|
Loading…
x
Reference in New Issue
Block a user