renamed some files

This commit is contained in:
Steffen Vogel 2015-05-31 13:09:45 +02:00
parent d3220aeccc
commit 1130ecd086
8 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,11 @@
TARGETS = server client
CC = gcc
CFLAGS = -g
all: $(TARGETS) Makefile
clean:
rm -f $(TARGETS)
rm -f *.o *~
.PHONY: all clean

View File

@ -0,0 +1,65 @@
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void handle_error(char *msg)
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
int fd;
char *lineptr = NULL;
size_t linelen = 0;
struct sockaddr_in sa = { .sin_family = AF_INET };
if (argc != 3) {
printf("usage: %s DEST-IP PORT\n", argv[0]);
exit(-1);
}
if (!inet_aton(argv[1], &sa.sin_addr))
fprintf(stderr, "Failed to parse destination IP address\n");
sa.sin_port = atoi(argv[2]);
if (!sa.sin_port)
fprintf(stderr, "Failed to parse port number");
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
handle_error("Failed to create socket");
if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)))
handle_error("Failed to connect socket");
while (!feof(stdin)) {
size_t len = getline(&lineptr, &linelen, stdin);
if (len > 0) {
if (send(fd, lineptr, len+1, 0) == -1)
handle_error("Failed to send");
if (recv(fd, lineptr, linelen, 0) == -1)
handle_error("Failed to recv");
fprintf(stdout, "%s", lineptr);
}
}
if (close(fd))
handle_error("Failed to close socket");
free(lineptr);
return 0;
}

View File

@ -0,0 +1,74 @@
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
static int fd;
void echo(int sig)
{
char line[1024];
struct sockaddr_in sa = { .sin_family = AF_INET };
socklen_t salen = sizeof(sa);
ssize_t msglen = recvfrom(fd, &line, sizeof(line), 0, (struct sockaddr *) &sa, &salen);
if (msglen == -1)
error(-1, errno, "Failed to recv");
if (sendto(fd, &line, msglen, 0,
(struct sockaddr *) &sa, salen) == -1)
error(-1, errno, "Failed to send");
}
void quit()
{
if (close(fd))
error(-1, errno, "Failed to close socket");
}
int main(int argc, char *argv[])
{
if (argc != 2)
error(-1, 0, "Usage: %s PORT\n", argv[0]);
struct sockaddr_in sa = { .sin_family = AF_INET };
sa.sin_port = atoi(argv[1]);
if (!sa.sin_port)
fprintf(stderr, "Failed to parse port number");
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
error(-1, errno, "Failed to create socket");
if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)))
error(-1, errno, "Failed to bind socket");
if (fcntl(fd, F_SETOWN, getpid()))
error(-1, errno, "Failed fcntl");
if (fcntl(fd, F_SETFL, O_ASYNC, 1))
error(-1, errno, "Failed fcntl");
atexit(quit);
struct sigaction sig = {
.sa_handler = echo,
.sa_flags = 0
};
sigemptyset(&sig.sa_mask);
if (sigaction(SIGIO, &sig, NULL))
error(-1, errno, "sigaction");
while(1);
return 0;
}