Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Sockets in CL

Name: Anonymous 2011-04-29 14:54

Thoughts? What's your favorite implementation?

Name: Anonymous 2011-04-29 15:05

Quit your attempt to create a bot in your inferior language you LISP weenie.

Name: Anonymous 2011-04-29 16:17

usocket is okay.

Name: BLACK HITLER 2011-04-30 0:44

glory BLACK AFRIKA HEIL NIGGERS. HEIL BLACK AFRIKA. NIG HEIL BLACK HITLER!

Name: BLACK HITLER 2011-04-30 0:44

glory BLACK AFRIKA HEIL NIGGERS. HEIL BLACK AFRIKA. NIG HEIL BLACK HITLER!

Name: WHITE HITLER 2011-04-30 0:48

glory WHITE EUROPE HEIL HONKEYS. HEIL WHITE EUROPE. SIG HEIL WHITE HITLER!

Name: WHITE HITLER 2011-04-30 0:48

glory WHITE EUROPE HEIL HONKEYS. HEIL WHITE EUROPE. SIG HEIL WHITE HITLER!

Name: WHITE HITLER 2011-04-30 0:51

glory WHITE EUROPE HEIL HONKEYS. HEIL WHITE EUROPE. SIG HEIL WHITE HITLER!

Name: WHITE HITLER 2011-04-30 0:51

glory WHITE EUROPE HEIL HONKEYS. HEIL WHITE EUROPE. SIG HEIL WHITE HITLER!

Name: Anonymous 2011-04-30 6:23

Name: Anonymous 2011-05-01 7:59

Thoughts? What's your favorite implementation?

I tried `iolib`, but it's relatively verbose. Still much shorter than C/C++.


Here is an example for downloading en.wikipedia.org/wiki/Main_Page

(require :iolib)
(defpackage :net-test (:use :cl :iolib))
(in-package :net-test)

(defun ip (x)
  (let ((bs (make-array (length x)
                        :element-type '(unsigned-byte 8)
                        :initial-contents  x)))
    (make-instance 'IPV4-ADDRESS :name bs)))

;; Usage: (http-get "en.wikipedia.org" 80 "/wiki/Main_Page")
(defun http-get (site port page)
  (let ((ip (lookup-hostname site)))
    (with-open-socket
        (socket
         :connect :active
         :address-family :internet
         :type :stream
         :external-format '(:utf-8 :eol-style :crlf)
         :ipv6 nil)
      (connect socket ip :port port :wait t)
      (format t "Connected to ~A:~A via ~A:~A~%"
              (remote-host socket) (remote-port socket)
              (local-host socket) (local-port socket))
      (progn
        ;;"GET /wiki/Main_Page http/1.1~%Host: en.wikipedia.org~%"
        (format socket "GET ~A HTTP/1.1~%" page)
        (format socket "Host: ~A~%" site)
        (format socket "~%")
        (finish-output socket) ;; switch socket direction to input
        (let ((line (read-line socket)))
          (format t "~A~%" line)) ;; print status code (should be "200 OK")
        t))))



And C/C++

#include <stdio.h>
#include <stdlib.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>

#ifndef   NI_MAXHOST
#define   NI_MAXHOST 1025
#endif

#define CRLF "\x0d\x0a"

#define REQSZ 4096
#define ANSSZ 4096

void httpGet(char *site, char *port, char *page) {
    char request[REQSZ];
    char answer[ANSSZ];
    int sock;
    struct addrinfo *pai;
    int error;

    printf("Resolving %s... ", site);
    error = getaddrinfo(site, port, 0, &pai);
    if(error) {
        printf("%s\n", gai_strerror(error));
        abort();
    }
    printf("done\n");

    printf("Creating socket... ");
    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(!sock) {
        printf("error\n");
        abort();
    }
    printf("done\n");

    printf("Connecting... ");
    error = connect(sock, pai->ai_addr, pai->ai_addrlen);
    if(error) {
        printf("%s\n", gai_strerror(error));
        abort();
    }
    printf("done\n");

    sprintf(request,
            "GET %s HTTP/1.1" CRLF
            "Host: %s" CRLF
            "" CRLF,
        page, site);
    printf("Sending:\n%s\n", request);

    printf("Writing... ");
    error = write(sock, request, strlen(request));
    if (error < 0) {
        printf("error\n");
        abort();
    }
    printf("done\n");

    printf("Reading... \n");
    while ((error = read(sock, answer, ANSSZ)) > 0) {
        answer[error]=0;
        printf("%s", answer);
    }
    printf("\ndone reading\n");

    printf("Closing socket... ");
    close(sock);
    printf("done\n");

    printf("Freeing addrinfo... ");
    freeaddrinfo(pai);
    printf("done\n");
}

Don't change these.
Name: Email:
Entire Thread Thread List