renamed some files
This commit is contained in:
parent
d3220aeccc
commit
1130ecd086
8 changed files with 150 additions and 0 deletions
11
session4/udp_signal/Makefile
Normal file
11
session4/udp_signal/Makefile
Normal 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
|
65
session4/udp_signal/client.c
Normal file
65
session4/udp_signal/client.c
Normal 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;
|
||||
}
|
74
session4/udp_signal/server.c
Normal file
74
session4/udp_signal/server.c
Normal 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;
|
||||
}
|
Loading…
Add table
Reference in a new issue