Skip to content

On Being a Web Client

August 25, 2015

It’s a bit counter-intuitive but being a web server is quite a bit easier than being a client.  The client is usually a pretty sophisticated browser running on a capable machine so a lot is expected of it.  The server can get away with spewing out some fixed html but the client has to parse and deal with it.

My servers usually open a socket and set it to listen on port 80, when a session is established they slurp in the request, pay not too much attention to it, and spit out a response.

To be a client, you first have to find the ip of your target with dns, then you have to issue a connect and check that it works, then send your request, wait for a response, and deal with that.  You can also get redirection and any number of other complexities added in.

To simplify things, I’m trying to work with an ip address that i’ve looked up in advance by pinging it. pings to so i’m bolting that into a variant of my server program and trying to connect rather than listen.  After a bunch of false starts I got google to respond with 2048 bytes of gobbledygook so that’s a start.

The status code 15 means it’s in connection state equivalent to 14 for listen I guess. Code 17 is session established as usual. Once I got code 17 i just sent my request and started polling for available data. Quite quickly I got 2048 bytes back which was all i was set up to handle. I’m sure there’s some organized way of getting the next window of data. My goal though is to hit a twitter server so i’m hoping i don’t get that much back.

//  File Name    : w5500mule.c
//  Version      : 1
//  Description  : wiznet w5500 test harness
//  Author       : WJR with thanks Karl Lunt & Wiznet
//  Target       : Olduino
//  Compiler     : LCC1802
//  IDE          : TextPad
//  Programmer   : Olduino bootloader via avrdude
//  Created		 : Nov 26, 2014
#define	nofloats			//not using floating point
#include <nstdlib.h> //for printf etc.
#include <cpu1802spd4port7.h>
#include <olduino.h> //for digitalRead, digitalWrite, delay
#include <hspi2.h> //hardware spi header
#include "w5500data.h"  //wiznet definitions
#include "w5500code.h"  //wiznet code definitions
union IPaddr thisip={182}; //the ip that a form/request came from
#define MAX_BUF 1024
unsigned char buf[MAX_BUF];			//memory buffer for incoming & outgoing data

void sendreq(){//send a request
	printf("getting response\n");
    sendlit("GET /search?q=olduino HTTP/1.1\n");
    sendlit("Connection: close\n");
void getresp(){	//handle a session once it's established
	unsigned int rsize,strncmpval;
	unsigned int tries=100;
	printf("getting response\n");
	rsize=wizGetCtl16(SnRX_RSR); //get the size of the received data
	while(rsize==0 && tries-->0){
		printf("re-size ");
		rsize=wizGetCtl16(SnRX_RSR); //retry size of the received data
	if (rsize>0){
		if (recv0(buf,min(1023,rsize))>0){ //get some characters
	printf("flushing %d\n",rsize);
  	if (rsize>0) flush(rsize);	//get rid of the received data
	wizCmd(CR_DISCON);// Disconnect the connection- we're done here

void wizWrite(unsigned int addr, unsigned char opcode, void * data, unsigned int len);
void socket0_client_init(){ //initialize socket 0 for http client
    unsigned char ggl_addr[]={24,156,153,25};//address for google destination
	wizCmd(CR_CLOSE); //make sure port is closed
	wizSetCtl8(SnIR,0xFF); //reset interrupt reg
	wizSetCtl8(SnMR,MR_TCP); //set mode register to tcp
	wizSetCtl16(SnPORT,1024); //set tcp port to 80
   	wizWrite(SnDIPR,WIZNET_WRITE_S0R,ggl_addr, 4); //write the outgoing dest ip address
   	wizSetCtl16(SnDPORT,80);//destination port
	wizCmd(CR_OPEN); //open the port
	wizCmd(CR_CONNECT); //try to make a conection

void main(void){
	unsigned char socket0status;
    unsigned char ip_addr[] = {192,168,1,182}; //{169,254,180,2}; //
	printf("\nW5500 Test Mule Client\n");
    wiz_Init(ip_addr); //initialize the wiznet chip

	while(1){  // Loop forever
		printf("s0s=%cx ",socket0status);
		if (socket0status==SOCK_ESTABLISHED){ //we're connected
			while(1);//loop here
#include <olduino.c>
#include <nstdlib.c>
#include <hspi2.c>
#include "w5500code.c"


13:14:51.777> W5500 Test Mule Client
13:14:52.399> Done Wiznet W5500 Initialization on IP address
13:14:52.399> Gateway Address
13:14:52.711> s0s=15 s0s=15 s0s=17 getting response
13:14:52.711> getting response
13:14:52.836> re-size re-size re-size re-size **rsz=2048
13:14:52.898> HTTP/1.1 200 OK
13:14:52.898> Date: Tue, 25 Aug 2015 17:14:52 GMT
13:14:52.898> Expires: -1
13:14:52.898> Cache-Control: private, max-age=0
13:14:52.898> Content-Type: text/html; charset=ISO-8859-1
13:14:52.962> P3P: CP="This is not a P3P policy! See for more info."
13:14:52.962> Server: gws
13:14:52.962> X-XSS-Protection: 1; mode=block
13:14:52.962> X-Frame-Options: SAMEORIGIN
13:14:53.024> Set-Cookie: PREF=ID=1111111111111111:FF=0:TM=1440522892:LM=1440522892:V=1:S=bPgUObdOy_1YCzrj; expires=Thu, 31-Dec-2015 16:02:17 GMT; path=/;
Set-Cookie: NID=70=JE6B7I2GKHSsE6xgpnZXSWt0-IS-Ww2BOKj8BNsJysk7nm2MzFYMW4Csmp_q70-oChJBLK3_v-13:14:53.086> gEbWbfQF4velwKkswhCYo2gWR7zezRcZ9q0BldXAQGzdSh2vN_0KPP; expires=Wed, 24-Feb-2016 17:14:52 GMT; path=/;; HttpOnly
13:14:53.086> Accept-Ranges: none
13:14:53.086> Vary: Accept-Encoding
13:14:53.086> Connection: close
<!doctype html><html itemscope="" 13:14:53.148> itemtype="" lang="en-CA"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/google_favicon_128.png" itemprop
13:14:53.148> flushing 2048
13:14:53.148> done
13:14:53.148> >
13:14:53.148> stalling

From → web server

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: