Difference between revisions of "Simple UDP Example"
Jump to navigation
Jump to search
(Created page with '=== About === This is a dead simple example of how to use the UDP networking features in XINU 2.0. === Usage === * Add the both of the files to the shell directory as xsh_echoc...') |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
=== Usage === | === Usage === | ||
− | * Add the both of the files to the shell directory as | + | * Add the both of the files to the shell directory as xsh_udpclient.c and xsh_udpserver.c |
− | * Modify shell.c and shell.h to include the | + | * Modify shell.c and shell.h to include the udpclient and udpserver commands. |
+ | * Modify the Makefile in the shell directory to include xsh_udpclient.c and xsh_udpserver.c and then make XINU from the compile directory | ||
* Boot two separate XINU consoles | * Boot two separate XINU consoles | ||
* Run netup command from the shell on both consoles | * Run netup command from the shell on both consoles | ||
− | * Run | + | * Run udpserver from the shell on the first console |
− | * Run | + | * Run udpclient from the shell on the second console with the arguments of the udpserver's ip and the message to be sent in quotes |
− | ** eg: | + | ** eg: udpclient 192.168.6.102 "Hello XINU World!" |
− | |||
=== Source === | === Source === | ||
− | ''' | + | '''UDP Client''' |
<nowiki> | <nowiki> | ||
#include <stddef.h> | #include <stddef.h> | ||
Line 28: | Line 28: | ||
/** | /** | ||
− | * Shell command ( | + | * Shell command (udpclient) runs a client that sends an ASCII message over the network to a server using UDP. |
* Expects arg0 to be echoclient, args1 to be the destination IP address, args2 to be the message in quotes | * Expects arg0 to be echoclient, args1 to be the destination IP address, args2 to be the message in quotes | ||
* @param nargs number of arguments in args array | * @param nargs number of arguments in args array | ||
Line 34: | Line 34: | ||
* @return non-zero value on error | * @return non-zero value on error | ||
*/ | */ | ||
− | shellcmd | + | shellcmd xsh_udpclient(int nargs, char *args[]) |
{ | { | ||
ushort dev = 0; | ushort dev = 0; | ||
Line 92: | Line 92: | ||
</nowiki> | </nowiki> | ||
− | ''' | + | '''UDP Server''' |
<nowiki> | <nowiki> | ||
#include <stddef.h> | #include <stddef.h> | ||
Line 105: | Line 105: | ||
/** | /** | ||
− | * Shell command ( | + | * Shell command (udpserver) runs a UDP server that waits for an incoming message, and then prints it out. Does not expect any arguments. |
* @param nargs number of arguments in args array | * @param nargs number of arguments in args array | ||
* @param args array of arguments | * @param args array of arguments |
Latest revision as of 21:46, 13 June 2011
About
This is a dead simple example of how to use the UDP networking features in XINU 2.0.
Usage
- Add the both of the files to the shell directory as xsh_udpclient.c and xsh_udpserver.c
- Modify shell.c and shell.h to include the udpclient and udpserver commands.
- Modify the Makefile in the shell directory to include xsh_udpclient.c and xsh_udpserver.c and then make XINU from the compile directory
- Boot two separate XINU consoles
- Run netup command from the shell on both consoles
- Run udpserver from the shell on the first console
- Run udpclient from the shell on the second console with the arguments of the udpserver's ip and the message to be sent in quotes
- eg: udpclient 192.168.6.102 "Hello XINU World!"
Source
UDP Client
#include <stddef.h> #include <stdio.h> #include <device.h> #include <ether.h> #include <udp.h> #include <string.h> #define MSG_MAX_LEN 64 #define ECHO_PORT 9989 /** * Shell command (udpclient) runs a client that sends an ASCII message over the network to a server using UDP. * Expects arg0 to be echoclient, args1 to be the destination IP address, args2 to be the message in quotes * @param nargs number of arguments in args array * @param args array of arguments * @return non-zero value on error */ shellcmd xsh_udpclient(int nargs, char *args[]) { ushort dev = 0; char buf[MSG_MAX_LEN]; char *dest = args[1]; struct netaddr dst; struct netaddr *localhost; struct netif *interface; /* Allocate a new UDP device */ if ((ushort)SYSERR == (dev = udpAlloc())) { fprintf(stderr, "Client: Failed to allocate a UDP device."); return SYSERR; } /* Look up local ip info */ interface = netLookup((ethertab[0].dev)->num); if (NULL == interface) { fprintf(stderr, "Client: No network interface found\r\n"); return SYSERR; } localhost = &(interface->ip); /* Change the destination to ipv4 */ if (SYSERR == dot2ipv4(dest, &dst)) { fprintf(stderr, "Client: Failed to convert ip address."); return SYSERR; } /* Open the UDP device with the destination and echo port*/ if (SYSERR == open(dev, localhost, &dst, NULL, ECHO_PORT)) { fprintf(stderr, "Client: Could not open the UDP device\r\n"); return SYSERR; } /* Send the message to the destination*/ memcpy(buf, args[2], MSG_MAX_LEN); if(SYSERR == write(dev, buf, MSG_MAX_LEN)) { close(dev); return SYSERR; } /* Closee the device when done */ close(dev); return 0; }
UDP Server
#include <stddef.h> #include <stdio.h> #include <device.h> #include <udp.h> #include <stdlib.h> #include <ether.h> #include <string.h> #define ECHO_PORT 9989 /** * Shell command (udpserver) runs a UDP server that waits for an incoming message, and then prints it out. Does not expect any arguments. * @param nargs number of arguments in args array * @param args array of arguments * @return non-zero value on error */ shellcmd xsh_echoserver(int nargs, char *args[]) { ushort dev = 0; int len = 0; char buffer[UDP_MAX_DATALEN]; struct netaddr *localhost; struct netif *interface; struct udpPseudoHdr *pseudo; struct udpPkt *udp; /* Allocate a new UDP device */ if ((ushort)SYSERR == (dev = udpAlloc())) { fprintf(stderr, "Server: Failed to allocate a UDP device.\r\n"); return SYSERR; } /* Look up local ip info */ interface = netLookup((ethertab[0].dev)->num); if (NULL == interface) { fprintf(stderr, "Server: No network interface found\r\n"); return SYSERR; } /* Open the UDP device using localhost and the echo port to listen to*/ localhost = &(interface->ip); if (SYSERR == open(dev, localhost, NULL, ECHO_PORT, NULL)) { fprintf(stderr, "Server: Could not open the UDP device\r\n"); return SYSERR; } /* Set the UDP device to passive mode */ if (SYSERR == control(dev, UDP_CTRL_SETFLAG, UDP_FLAG_PASSIVE, NULL)) { kprintf("Server: Could not set UDP device to passive mode\r\n"); close(dev); return SYSERR; } /* Read lop, wait for a new request */ printf("Server: Waiting for message\r\n"); while (SYSERR != (len = read(dev, buffer, UDP_MAX_DATALEN))) { pseudo = (struct udpPseudoHdr *)buffer; udp = (struct udpPkt *)(pseudo + 1); printf("Server: Received Message - %s\r\n", udp->data); } close(dev); return 0; }