Skip to content

Instantly share code, notes, and snippets.

View kYroL01's full-sized avatar
🥊

Michele Campus kYroL01

🥊
View GitHub Profile
@kYroL01
kYroL01 / server.c
Last active August 31, 2021 14:27
TCP server
/* -*- compile-command: "gcc -Wall -pedantic -g3 server.c -o server" -*- */
/**
TCP server implementation
1. Create a TCP socket.
2. bind(), Bind the socket to server address.
3. listen(), Put the server socket in a passive mode, it waits for the client to make a connection
4. accept(), Connection is established between client and server, ready to transfer data.
5. go back to Step 3.
@kYroL01
kYroL01 / client.c
Created August 30, 2021 22:31
TCP client
/* -*- compile-command: "gcc -Wall -pedantic -g3 client.c -o client" -*- */
/**
TCP client implementation
1. Create TCP socket.
2. connect() Connect newly created client socket to server.
**/
#include <stdio.h>
@kYroL01
kYroL01 / two_sum_fast.go
Created August 24, 2021 21:01
Fast two sum - alternative way instead of two for loops
func twoSum(nums []int, target int) []int {
var tempDict = make(map[int]int)
for key, value := range nums {
if _, ok := tempDict[value]; ok{
return []int{tempDict[value], key}
}
tempDict[target-value] = key
}
return []int{}
}
@kYroL01
kYroL01 / lowestCommAnc.go
Created July 25, 2021 12:08
Lowest Common Ancestor for a Binary Tree
/*
Algorithm
- if a node is NULL or equal p or q, we return it
- if not, we traverse the left and right subtree until we return a node, assigning to bool value RetLeft or RetRight
- Then we check if we have to return the node (RetLeft and RetRight are both != NULL), or only left or right
- The LCA is of course the node with RetLeft AND RetRight != NULL
*?
/**
* Definition for a binary tree node.
@kYroL01
kYroL01 / lowestCommAncBST.go
Last active July 25, 2021 11:59
Lowest Common Ancestor for a BST
/**
Algorithm
- Start traversing the tree from the root node.
- If both p and q are < root, then continue to left subtree and starting step 1.
- If both p and q are > root, then continue to right subtree and starting step 1.
- If both step 2 and step 3 are not true, we have found the LCA for p and q, and we return it
*/
/**
@kYroL01
kYroL01 / slice_passing.go
Last active July 24, 2021 21:25
Time calculation of passing slice as a pointer vs as a value
package main
import (
"fmt"
"time"
"strconv"
"strings"
)
func track(msg string) (string, time.Time) {
@kYroL01
kYroL01 / dir_cleaner.c
Last active July 16, 2021 10:11
Directory Cleaner with date format name
/* -*- compile-command: "gcc -Wall -pedantic -g3 dir_cleaner.c -o dir_cleaner" -*- */
#define _XOPEN_SOURCE 500
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ftw.h>
#include <dirent.h>
#include <unistd.h>
@kYroL01
kYroL01 / tcp_client.c
Created February 17, 2021 11:08 — forked from teraPacket/tcp_client.c
example TCP client using libuv (version 1.0)
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
//based on https://gist.githubusercontent.com/snatchev/5255976/
//raw/8392c42d719bb775053036e32b21affdf932c1b7/libuv-tcp-client.c
//which was based on libuv 0.1, there is considerable difference there.
static void on_close(uv_handle_t* handle);
static void on_connect(uv_connect_t* req, int status);
static void on_write(uv_write_t* req, int status);
@kYroL01
kYroL01 / curl_pcap.c
Last active July 12, 2021 16:38
curl_pcap
/**
* Take the URL of remote pcap from command line and download the file to disk
* It check if the extension is allowed (pcap-cap-pcapng)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#define FILENAME_LEN 1000
@kYroL01
kYroL01 / server.php
Created July 25, 2020 15:26 — forked from BenMorel/server.php
(Medium) Sample PHP server
<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($sock, '0.0.0.0', 10000);
for (;;) {
socket_recvfrom($sock, $message, 1024, 0, $ip, $port);
$reply = str_rot13($message);
socket_sendto($sock, $reply, strlen($reply), 0, $ip, $port);
}