Skip to content

Instantly share code, notes, and snippets.

@IGBC
Created May 27, 2017 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IGBC/c1ee6e53eaf6b8b2ca0b3c321eee295a to your computer and use it in GitHub Desktop.
Save IGBC/c1ee6e53eaf6b8b2ca0b3c321eee295a to your computer and use it in GitHub Desktop.
basic flood program
/*
Send a message up to a newline to the server.
Read a reply up to a newline and print it to stdout.
Loop.
Usage:
./client [<server-address> [<port>]]
Default ip: localhost.
Default port: 12345
*/
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h> /* getprotobyname */
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
int main(int argc, char **argv) {
char buffer[BUFSIZ];
char protoname[] = "tcp";
struct protoent *protoent;
char *server_hostname = "127.0.0.1";
char *user_input = NULL;
in_addr_t in_addr;
in_addr_t server_addr;
int sockfd;
size_t getline_buffer = 0;
ssize_t nbytes_read, i, user_input_len;
struct hostent *hostent;
/* This is the struct used by INet addresses. */
struct sockaddr_in sockaddr_in;
unsigned short server_port = 12345;
if (argc > 1) {
server_hostname = argv[1];
if (argc > 2) {
server_port = strtol(argv[2], NULL, 10);
}
}
/* Get socket. */
protoent = getprotobyname(protoname);
if (protoent == NULL) {
perror("getprotobyname");
exit(EXIT_FAILURE);
}
sockfd = socket(AF_INET, SOCK_STREAM, protoent->p_proto);
if (sockfd == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
/* Prepare sockaddr_in. */
hostent = gethostbyname(server_hostname);
if (hostent == NULL) {
fprintf(stderr, "error: gethostbyname(\"%s\")\n", server_hostname);
exit(EXIT_FAILURE);
}
in_addr = inet_addr(inet_ntoa(*(struct in_addr*)*(hostent->h_addr_list)));
if (in_addr == (in_addr_t)-1) {
fprintf(stderr, "error: inet_addr(\"%s\")\n", *(hostent->h_addr_list));
exit(EXIT_FAILURE);
}
sockaddr_in.sin_addr.s_addr = in_addr;
sockaddr_in.sin_family = AF_INET;
sockaddr_in.sin_port = htons(server_port);
/* Do the actual connection. */
if (connect(sockfd, (struct sockaddr*)&sockaddr_in, sizeof(sockaddr_in)) == -1) {
perror("connect");
return EXIT_FAILURE;
}
fprintf(stderr, "connected\n");
while (1) {
#define xoff 0
#define yoff 0
#define w 1920
#define h 1200
#define BARS 6
char colours[BARS][7] = {"f40000\0", "ff8601\0", "faf601\0", "018821\0", "4600ff\0", "830189\0"}; //LGBT
//char colours[BARS][7] = {"fff434\0", "ffffff\0", "9c59cf\0", "2d2d2d\0"}; //NONBINARY
//char colours[BARS][7] = {"5bcefa\0", "f5a9b8\0", "ffffff\0", "f5a9b8\0", "5bcefa\0"}; //TRANS
#define framesize 8500
char line[framesize] = "\0";
char buf[21];
int bh = h / BARS;
for (int y = yoff; y < h + yoff; y++) {
int col = (y - yoff) / bh;
for (int x = xoff; x < w + xoff; x++) {
sprintf(buf, "PX %i %i %s\n", x, y, colours[col]);
//fprintf(stderr, "%s, %i", buf, col);
if (strlen(line) + strlen(buf) > framesize) {
if (write(sockfd, line, strlen(line)) == -1) {
fprintf(stderr, "write error");
exit(EXIT_FAILURE);
}
strcpy(line, buf);
} else {
strcat(line, buf);
}
}
}
}
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment