Skip to content

Instantly share code, notes, and snippets.

@limingzju
limingzju / EchoServer.py
Created September 8, 2013 10:19
A simple Python echo server, show how to write socket program use python.
# Echo server program
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
@limingzju
limingzju / rwlock.cc
Last active August 29, 2015 14:02
read write lock
#include <pthread.h>
struct rwlock {
pthread_mutex_t lock;
pthread_cond_t read, write;
unsigned readers, writers, read_waiters, write_waiters;
};
void reader_lock(struct rwlock *self) {
pthread_mutex_lock(&self->lock);
@limingzju
limingzju / rwmutex.go
Created June 5, 2014 06:57
go read write lock
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sync
import (
"sync/atomic"
"unsafe"
)
@limingzju
limingzju / SSLTest.java
Created September 21, 2014 05:29
https java test
package hello;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import javax.net.ssl.SSLContext;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestNet {
public static void main(String[] args) throws UnknownHostException {
InetAddress[] machines = InetAddress.getAllByName("lbs.yxgslb.netease.com");
for (InetAddress address : machines) {
System.out.println(address.getHostAddress());
}
@limingzju
limingzju / post.go
Created October 20, 2014 02:12
http post
package main
import (
"net"
"fmt"
"io"
"bytes"
"os"
)
import webbrowser
import os
def openWeb(url):
webbrowser.open_new_tab(url)
def main():
urls = ['http://www.baidu.com', 'http://www.bing.com', 'http://www.zhihu.com']
for url in urls:
option = raw_input('open %s Y/N: ' %url)
type CopyCb func(written int64)
func CopyWithCb(dst io.Writer, src io.Reader, cb CopyCb) (written int64, err error) {
for {
n, err := io.CopyN(dst, src, KB)
written += n
cb(written)
if err != nil {
if err == io.EOF {
return written, nil
}
@limingzju
limingzju / get.java
Created May 9, 2015 03:49
spring return binary file
@RequestMapping(value="/pdfmethod", produces="application/pdf")
public void pdfMethod(HttpServletRequest request, HttpServletResponse response){
response.setContentType("application/pdf");
InputStream inputStream = null;
OutputStream outputStream = null;
try{
inputStream = getInputStreamFromYourPdfFile();
outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
}catch(IOException ioException){
@limingzju
limingzju / path.java
Created May 9, 2015 04:05
spring path variable
@RequestMapping(
value = "/{id}.json",
method = RequestMethod.GET,
produces = "application/json")
@ResponseBody
public Person getDetailsAsJson(@PathVariable Long id) {
return personRepo.findOne(id);
}
@RequestMapping(