Skip to content

Instantly share code, notes, and snippets.

@elazarl
elazarl / poll_dev.c
Created June 18, 2013 14:33
A simple test to determine if your OS allows polling devices, e.g. /dev/null
#include <stdio.h>
#include <poll.h>
#include <fcntl.h>
int main() {
int fd = open("/dev/null", O_WRONLY);
struct pollfd pollfds = { fd, POLLOUT, 0 };
if (fd < 0) {
perror("open");
@elazarl
elazarl / NoLoopBackSocket.java
Created June 11, 2013 06:48
This test shows that Java will not allow a socket to connect to itself via TCP's simultaneous connection feature, by setting the source port identical to the destination port.
import org.junit.Test;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
/**
* This tests make sure Java prevents a socket from connecting to itself
* via binding to the same port it connects to
@elazarl
elazarl / tcpsymcon.go
Created June 11, 2013 06:36
This code snippet demonstrate the tcp simultaneous connection feature. No one of the goroutines has a listening socket, yet, when both try to connect to each other simultaneously the connection succeeds. I've yet to understand what is this feature good for, I definitely know what is it not good for: http://golang.org/src/pkg/net/tcpsock_posix.go…
package main
import "fmt"
import "net"
var localhost = net.ParseIP("127.0.0.1")
var port1, port2 = 1220, 1229
func conwrite() {
c1, err := net.DialTCP("tcp", &net.TCPAddr{localhost, port2}, &net.TCPAddr{localhost, port1})
@elazarl
elazarl / cp.go
Last active June 27, 2020 14:42
Unfortunately, searching for "golang copy file" gives subtly wrong code snippets (e.g. https://groups.google.com/d/msg/golang-nuts/JNyQxQLyf5o/kbGnTUK32TkJ that don't check close error code). This is an attempt to copy file content from `src` to `dst`
package cp
import (
"io"
"os"
)
func cp(dst, src string) error {
s, err := os.Open(src)
if err != nil {
package main
import (
"io"
"os"
"time"
)
func singLine(line string,w io.Writer) {
time.Sleep(time.Second)
@elazarl
elazarl / partialResp.go
Created March 25, 2012 18:11
A draft code to read a partial response from http server
package main
import ("bufio"
"fmt"
"io"
"net/http"
"net"
"strings")
type readFirstCloseSecond struct {
@elazarl
elazarl / clientDoGzip.go
Created March 16, 2012 15:01
Client.Do will not uncompress response body
package main
// run this file with `go run clientDoGzip.go, and then file tmp.out to verify body is gzipped
import "net/http"
import "io/ioutil"
import "bytes"
import "fmt"
import "log"
import "compress/gzip"
@elazarl
elazarl / BencodeWriter.java
Created February 1, 2012 17:00
A simple bencode encoder that can handle streaming data
class BencodeWriter {
static class SizedInputStream {
public InputStream stream;
public int size;
public SizedInputStream(InputStream _stream,int _size) {stream = _stream;size = _size;}
}
public static class BencodeProducer {
private OutputStream out;
public BencodeProducer(OutputStream o) {out = o;}
@elazarl
elazarl / trie.c
Created January 21, 2012 23:21
A simple and minimal C implementation of trie
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// weird exercise from a friend
// need to Implement this struct
typedef struct attrib_t {
struct attrib_t* bytes[256];
const char *data;
@elazarl
elazarl / Publishing.java
Created December 20, 2011 18:02
Trying to recreate the "Holder" class publishing error from JCIP.
/**
* Trying to recreate the "Holder" class publishing error from JCIP.
* See http://stackoverflow.com/a/6812936/55094 for the description of the problem.
*/
public class Publishing {
static class Holder {
int h = 0;
int dummy = 0;
public Holder(int h) {
for (long i=0;i<1000*1000;i++) {