Commit ce5a85ee authored by Oleg Dzhimiev's avatar Oleg Dzhimiev

initial

parent 61878bd7
Pipeline #1058 canceled with stages
/*
* Based on:
* https://www.geeksforgeeks.org/udp-server-client-implementation-c/
*/
// Server side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 44460
#define MAXLINE 1420
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_RST "\x1b[0m"
int parse_packet(char *buf){
char *token;
int cnt = 0;
int res = -1;
while ((token = strsep(&buf, "_")) != NULL){
if (cnt==2){
//printf("%s\n", token);
res = atoi(token);
break;
}
cnt++;
}
return res;
}
// server
int main(int argc, char **argv) {
int fd;
char buffer[MAXLINE];
struct sockaddr_in servaddr, cliaddr;
in_addr_t s_addr = INADDR_ANY;
if (argc>1){
if (!inet_aton(argv[1],&s_addr)){
perror("Invalid inet addr in argv[1]");
return -1;
}
}
if ( (fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Server info
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = s_addr;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if ( bind(fd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
int cliaddr_len, n;
bool first = true;
/*
if(first){
printf("Boolean type works\n");
}
*/
int pnum;
int pnum_old=-1;
int cnt = 0;
printf("Listening at %s:%d\n",inet_ntoa(servaddr.sin_addr),PORT);
// Receive
while(true){
n = recvfrom(fd, (char *)buffer, MAXLINE, 0, (struct sockaddr *) &cliaddr, (socklen_t *) &cliaddr_len);
buffer[n] = '\0';
//if (first && (cliaddr.sin_port!=0)){
if (first){
printf("Receiving from %s:%d (if 0s - cliaddr does not always get filled out)\n", inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
first = false;
}
pnum = parse_packet(buffer);
//printf("packet %d\n",pnum);
if (pnum_old!=-1){
if ((pnum-pnum_old)!=1){
printf(ANSI_COLOR_RED "PACKET LOSS, received: %d, expected: %d" ANSI_COLOR_RST "\n", pnum, pnum_old+1);
}
}
pnum_old = pnum;
cnt++;
}
return 0;
}
/*
* Based on:
* https://www.geeksforgeeks.org/udp-server-client-implementation-c/
*/
// Server side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/uio.h>
#include <sys/time.h>
#define PORT 44460
#define MAXLINE 1420-67
#define NUMBER_OF_PACKETS_IN_FRAME 10000000
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_RST "\x1b[0m"
int random_string(char *dest, size_t length) {
char charset[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
while (length-- > 0) {
size_t index = (double) rand() / RAND_MAX * (sizeof charset - 1);
*dest++ = charset[index];
}
*dest = '\0';
return 0;
}
int generate_packet(char * buf, int pnum){
char pnum_str[15];
sprintf(pnum_str,"__%d__",pnum);
//random_string(buf,MAXLINE);
strncpy(buf,pnum_str,strlen(pnum_str));
return 0;
}
//client
int main(int argc, char **argv) {
struct timeval t0;
int fd;
char buffer[MAXLINE];
int res = 0;
struct sockaddr_in servaddr;
in_addr_t s_addr = INADDR_ANY;
if (argc>1){
if (!inet_aton(argv[1],&s_addr)){
perror("Invalid inet addr in argv[1]");
return -1;
}
}
if ( (fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
// Server info
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = s_addr;
servaddr.sin_port = htons(PORT);
struct sockaddr_in saddr;
memset(&saddr, 0, sizeof(struct sockaddr_in));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(0);
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(fd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in))<0){
printf("BIND ERROR!");
}
printf("Sending to %s:%d\n",inet_ntoa(servaddr.sin_addr),PORT);
int frame_cnt = 0;
int pnum = 0;
if (connect(fd,(const struct sockaddr *) &servaddr, sizeof(servaddr))<0){
printf("connect() error: %d",errno);
return -2;
}
// Send
while(true){
random_string(buffer,MAXLINE);
for(int i=0;i<NUMBER_OF_PACKETS_IN_FRAME;i++){
gettimeofday(&t0, NULL);
printf("packet: %06d, time: %lu.%06d\n", pnum, t0.tv_sec, t0.tv_usec);
generate_packet(buffer,pnum);
// tested
//res = sendto(fd, (const char *)buffer, strlen(buffer), 0, (const struct sockaddr *) &servaddr, sizeof(servaddr));
// tested
//res = write(fd,(const char *)buffer,strlen(buffer));
//gettimeofday(&t0, NULL);
//printf("time before2 : %lu.%06d\n", t0.tv_sec,t0.tv_usec);
int iovcnt;
struct iovec iov[3];
char *buf1 = "This is a longer string\n";
char *buf2 = "This is the longest string in this example\n";
iov[0].iov_base = buffer;
iov[0].iov_len = strlen(buffer);
iov[1].iov_base = buf1;
iov[1].iov_len = strlen(buf1);
iov[2].iov_base = buf2;
iov[2].iov_len = strlen(buf2);
iovcnt = 3;
res = writev(fd,iov,iovcnt);
//gettimeofday(&t0, NULL);
//printf("time after : %lu.%06d\n", t0.tv_sec,t0.tv_usec);
if (res<0){
printf(" send result= %d, errno= %d\n",res,errno);
}else{
//printf(" sent %d bytes\n",res);
}
pnum++;
}
//printf("%s\n",buffer);
gettimeofday(&t0, NULL);
printf("Sent frame %d, total packets %d @ %lu.%06d\n",frame_cnt, pnum,t0.tv_sec,t0.tv_usec);
usleep(1000000);
frame_cnt++;
}
return 0;
}
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