1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

added new tools to send/receive data to/from stdin/out

git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@33 8ec27952-4edc-4aab-86aa-e87bb2611832
This commit is contained in:
Steffen Vogel 2014-06-05 09:35:02 +00:00
parent 68776b5980
commit 49efbf2e64
3 changed files with 154 additions and 0 deletions

2
.gitignore vendored
View file

@ -9,3 +9,5 @@ doc/latex/
server
test
random
send
receive

73
src/receive.c Normal file
View file

@ -0,0 +1,73 @@
/**
* Receive messages from server snd print them on stdout
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "config.h"
#include "utils.h"
#include "msg.h"
int sd;
void quit(int sig, siginfo_t *si, void *ptr)
{
close(sd);
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
struct sockaddr_in sa;
if (argc != 2) {
printf("Usage: %s IP:PORT\n", argv[0]);
printf(" IP is the destination ip of our packets\n");
printf(" PORT is the port to receive from\n\n");
printf("s2ss Simulator2Simulator Server v%s\n", VERSION);
printf("Copyright 2014, Institute for Automation of Complex Power Systems, EONERC\n");
exit(EXIT_FAILURE);
}
/* Setup signals */
struct sigaction sa_quit = {
.sa_flags = SA_SIGINFO,
.sa_sigaction = quit
};
sigemptyset(&sa_quit.sa_mask);
sigaction(SIGINT, &sa_quit, NULL);
/* Resolve address */
if (resolve(argv[1], &sa, 0))
error("Failed to resolve: %s", argv[1]);
/* Create socket */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd < 0)
error("Failed to create socket: %s", strerror(errno));
/* Bind socket */
if (bind(sd, (struct sockaddr *) &sa, sizeof(struct sockaddr_in)))
error("Failed to bind socket: %s", strerror(errno));
struct msg m;
while (1) {
recv(sd, &m, sizeof(struct msg), 0);
msg_fprint(stdout, &m);
}
return 0;
}

79
src/send.c Normal file
View file

@ -0,0 +1,79 @@
/**
* Send messages from stdin to server
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "config.h"
#include "utils.h"
#include "msg.h"
int sd;
void quit(int sig, siginfo_t *si, void *ptr)
{
close(sd);
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
struct sockaddr_in sa;
if (argc != 2) {
printf("Usage: %s IP:PORT\n", argv[0]);
printf(" IP is the destination ip of our packets\n");
printf(" PORT is the port to send to\n\n");
printf("s2ss Simulator2Simulator Server v%s\n", VERSION);
printf("Copyright 2014, Institute for Automation of Complex Power Systems, EONERC\n");
exit(EXIT_FAILURE);
}
/* Setup signals */
struct sigaction sa_quit = {
.sa_flags = SA_SIGINFO,
.sa_sigaction = quit
};
sigemptyset(&sa_quit.sa_mask);
sigaction(SIGINT, &sa_quit, NULL);
/* Resolve address */
if (resolve(argv[1], &sa, 0))
error("Failed to resolve: %s", argv[1]);
/* Create socket */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd < 0)
error("Failed to create socket: %s", strerror(errno));
// TODO: remove workaround
struct msg msg = {
.length = 5 * sizeof(double)
};
/* Connect socket */
if (connect(sd, (struct sockaddr *) &sa, sizeof(struct sockaddr_in))) {
error("Failed to connect socket: %s", strerror(errno));
}
while (!feof(stdin)) {
msg_fscan(stdin, &msg);
msg_fprint(stdout, &msg);
send(sd, &msg, 8 + msg.length, 0);
}
return 0;
}