Commit f4118e0c authored by Joanne Hugé's avatar Joanne Hugé

Add simple client application

parent 4a16b459
*.o
*.d
simple-client/build/main
PROG = main
SRCDIR = ../src
SRCS = main.c
SRCS += getip.c
SRCS += send_packet.c
OBJS = $(SRCS:%.c=%.o)
CFLAGS = -O1 -g -Wall -Werror -Wextra
CFLAGS += -MD -MP
CFLAGS += -I $(SRCDIR)
CFLAGS += -std=gnu99
vpath %.c $(SRCDIR)
$(PROG): $(OBJS)
$(CC) $(LDFLAGS) $^ -o $@
-include $(subst .c,.d,$(SRCS))
run: $(PROG)
./$^
clean:
$(RM) $(OBJS) $(PROG) $(subst .c,.d,$(SRCS))
.PHONY: clean
#include "getip.h"
#include <ifaddrs.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void print_ifs(void) {
struct ifaddrs* ifap;
getifaddrs(&ifap);
printf("Interfaces:\n\n");
for (struct ifaddrs* cur_ifap = ifap; cur_ifap;
cur_ifap = cur_ifap->ifa_next) {
printf("%s, ", cur_ifap->ifa_name);
void * addr;
char * ipver;
char ipstr[INET6_ADDRSTRLEN];
struct sockaddr * sa = cur_ifap->ifa_addr;
if( sa->sa_family == AF_INET ) { // IPV4
struct sockaddr_in * ipv4 = (struct sockaddr_in *) sa;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else {
struct sockaddr_in6 * ipv6 = (struct sockaddr_in6 *) sa;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
inet_ntop(sa->sa_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
}
freeifaddrs(ifap);
}
#ifndef GETIP_H
#define GETIP_H
void print_ifs(void);
#endif
#include <stdio.h>
#include "getip.h"
#include "send_packet.h"
int main() {
for(int i = 0; i < 100; i++)
send_udp_packet();
return 0;
}
#include "send_packet.h"
/*
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SERVER_IP "192.168.0.100"
#define SERVER_PORT "50000"
int send_udp_packet(void) {
int status;
int sockfd;
struct addrinfo hints, *servinfo, *servinfo_it;
char msg[1024] = "hello";
int bytes_sent = 0;
int msg_len = strlen(msg);
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
status = getaddrinfo(SERVER_IP, SERVER_PORT, &hints, &servinfo);
if( status != 0 ) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
printf("getaddrinfo error, exiting...\n");
return 1;
}
for(servinfo_it = servinfo; servinfo_it != NULL; servinfo_it = servinfo_it->ai_next) {
sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
if(sockfd == -1) {
printf("Socket error, continuing...\n");
continue;
}
break;
}
if(servinfo_it == NULL) {
fprintf(stderr, "Failed to create socket\n");
printf("Failed to create socket\n");
return 2;
}
while (bytes_sent < msg_len) {
bytes_sent += sendto(sockfd, msg + bytes_sent, strlen(msg), 0, servinfo->ai_addr,
servinfo->ai_addrlen);
}
freeaddrinfo(servinfo);
printf("Sent %d bytes to %s\n", bytes_sent, SERVER_IP);
close(sockfd);
return 0;
}
#ifndef SEND_PACKET_H
#define SEND_PACKET_H
int send_udp_packet(void);
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment