added first session UDP echo server and client
This commit is contained in:
commit
e272d5f279
4 changed files with 147 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*.o
|
14
session1/udp_echo/Makefile
Normal file
14
session1/udp_echo/Makefile
Normal file
|
@ -0,0 +1,14 @@
|
|||
TARGETS = echo_server echo_client
|
||||
|
||||
CC = gcc
|
||||
CFLAGS = -g -D$(DEFINES)
|
||||
|
||||
all: $(TARGETS) Makefile
|
||||
clean:
|
||||
rm -f $(TARGETS)
|
||||
rm -f *.o *.~
|
||||
|
||||
echo_server: echo_server.o
|
||||
echo_client: echo_client.o
|
||||
|
||||
.PHONY: all clean
|
67
session1/udp_echo/echo_client.c
Normal file
67
session1/udp_echo/echo_client.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
#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;
|
||||
ssize_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");
|
||||
|
||||
#ifdef ECHO
|
||||
if (recv(fd, lineptr, linelen, 0) == -1)
|
||||
handle_error("Failed to recv");
|
||||
|
||||
fprintf(stdout, "%s", lineptr);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (close(fd))
|
||||
handle_error("Failed to close socket");
|
||||
|
||||
free(lineptr);
|
||||
|
||||
return 0;
|
||||
}
|
65
session1/udp_echo/echo_server.c
Normal file
65
session1/udp_echo/echo_server.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 line[1024];
|
||||
ssize_t msglen;
|
||||
socklen_t salen;
|
||||
|
||||
struct sockaddr_in sa = { .sin_family = AF_INET };
|
||||
|
||||
if (argc != 2) {
|
||||
printf("usage: %s PORT\n", argv[0]);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
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)
|
||||
handle_error("Failed to create socket");
|
||||
|
||||
if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)))
|
||||
handle_error("Failed to bind socket");
|
||||
|
||||
while (1) {
|
||||
salen = sizeof(sa);
|
||||
|
||||
msglen = recvfrom(fd, &line, sizeof(line), 0, (struct sockaddr *) &sa, &salen)
|
||||
if (msglen == -1)
|
||||
handle_error("Failed to recv");
|
||||
|
||||
#ifdef ECHO
|
||||
if (sendto(fd, &line, msglen, 0,
|
||||
(struct sockaddr *) &sa, salen) == -1)
|
||||
handle_error("Failed to send");
|
||||
#else
|
||||
fprintf(stdout, "%s", line);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (close(fd))
|
||||
handle_error("Failed to close socket");
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue