Created
August 3, 2016 04:47
-
-
Save justinc1/e3ba7596c02f4af4917ade130530c5c6 to your computer and use it in GitHub Desktop.
udp app to demo OSv issue https://github.com/cloudius-systems/osv/issues/775
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OSV_SRC=../.. | |
CPPLAGS += -std=c++11 | |
## CPPLAGS += -I$(OSV_SRC) -I$(OSV_SRC)/include/ -I$(OSV_SRC)/arch/x64 | |
CPP=c++ $(CPPLAGS) -ggdb | |
CPPSO=$(CPP) -fPIC -shared | |
GCC=gcc | |
GCCSO=$(GCC) -fPIC -shared | |
all: udp-rec udp-rec.so | |
clean: | |
rm -f udp-rec udp-rec.so | |
udp-rec: udp-rec.c | |
$(GCC) -o $@ $< | |
udp-rec.so: udp-rec.c | |
$(GCCSO) -o $@ $< | |
module: all | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <netdb.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#define BUFSIZE 1024 | |
/* | |
* error - wrapper for perror | |
*/ | |
void error(char *msg) { | |
perror(msg); | |
exit(1); | |
} | |
int main(int argc, char **argv) { | |
int sockfd; /* socket */ | |
int portno; /* port to listen on */ | |
int clientlen; /* byte size of client's address */ | |
struct sockaddr_in serveraddr; /* server's addr */ | |
struct sockaddr_in clientaddr; /* client addr */ | |
struct hostent *hostp; /* client host info */ | |
char buf[BUFSIZE]; /* message buf */ | |
char *hostaddrp; /* dotted decimal host addr string */ | |
int optval; /* flag value for setsockopt */ | |
int n; /* message byte size */ | |
/* | |
* check command line arguments | |
*/ | |
if (argc != 2) { | |
fprintf(stderr, "usage: %s <port>\n", argv[0]); | |
exit(1); | |
} | |
portno = atoi(argv[1]); | |
/* | |
* socket: create the parent socket | |
*/ | |
sockfd = socket(AF_INET, SOCK_DGRAM, 0); | |
if (sockfd < 0) | |
error("ERROR opening socket"); | |
/* setsockopt: Handy debugging trick that lets | |
* us rerun the server immediately after we kill it; | |
* otherwise we have to wait about 20 secs. | |
* Eliminates "ERROR on binding: Address already in use" error. | |
*/ | |
optval = 1; | |
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, | |
(const void *)&optval , sizeof(int)); | |
/* | |
* build the server's Internet address | |
*/ | |
bzero((char *) &serveraddr, sizeof(serveraddr)); | |
serveraddr.sin_family = AF_INET; | |
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); | |
serveraddr.sin_port = htons((unsigned short)portno); | |
/* | |
* bind: associate the parent socket with a port | |
*/ | |
if (bind(sockfd, (struct sockaddr *) &serveraddr, | |
sizeof(serveraddr)) < 0) | |
error("ERROR on binding"); | |
/* | |
* main loop: wait for a datagram, then echo it | |
*/ | |
clientlen = sizeof(clientaddr); | |
while (1) { | |
/* | |
* recvfrom: receive a UDP datagram from a client | |
*/ | |
bzero(buf, BUFSIZE); | |
#if 0 | |
n = recvfrom(sockfd, buf, BUFSIZE, 0, | |
(struct sockaddr *) &clientaddr, &clientlen); | |
if (n < 0) | |
error("ERROR in recvfrom"); | |
printf("server recvfrom received %d/%d bytes: %s\n", strlen(buf), n, buf); | |
#else | |
struct msghdr msg; | |
struct iovec iov; | |
iov.iov_base = buf; | |
iov.iov_len = BUFSIZE; | |
bzero(&msg, sizeof(msg)); | |
msg.msg_iov = &iov; | |
msg.msg_iovlen = 1; | |
printf(" AA iov.iov_base = %p\n", iov.iov_base); | |
n = recvmsg(sockfd, &msg, 0); | |
printf(" BB iov.iov_base = %p\n", iov.iov_base); | |
if (n < 0) | |
error("ERROR in recvmsg"); | |
printf("server recvmsg received %d/%d bytes: %s\n", strlen(buf), n, (char*)iov.iov_base); | |
#endif | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/udp-app/**: ${MODULE_DIR}/** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment