sockets

Using TCP/IP sockets to read and write packets.

Download jsocket.zip

Synopsis:

jsocketmain.cpp
jsocket.cpp
jsocket.h


jsocketmain.cpp

Synopsis
//WARNING! NO UNITTESTS
#include <stdio.h>
#include "jsocket.h"

int main()
  {
  char buf[1024];

  jsocket* s = new jsocket("127.0.0.1", 80);
  
  s->connect();
  
  sprintf(buf, "GET /cgi-bin/test.cgi HTTP/1.0\r\n\n\n");
  s->send(buf, (int) strlen(buf));
  
  for(;;)
    {
    unsigned long avail = s->avail();
    if (avail == 0)
      break;

    memset(buf, 0, sizeof(buf));
    s->recv(buf, sizeof(buf));
    }

  delete s;
  
  return 0;
  }

jsocket.cpp

Synopsis
#include "jsocket.h"

jsocket::jsocket(char* ipaddr, int port)
  {
  this->s = 0;
  this->isOpen = true;
  this->port = port;
  fillSocketAddress(ipaddr);
  
  startup();
  }

jsocket::~jsocket()
  {
  cleanup();
  }

void jsocket::connect()
  {
  int rc;
  int gle;

  open();
  if (!this->isOpen) return;

  rc = ::connect(this->s, (SOCKADDR *)&this->addr, sizeof(SOCKADDR_IN));
  if (rc)
    gle = WSAGetLastError();
  }

void jsocket::send(char* buf, int len)
  {
  int rc;
  int gle;
  if (!this->isOpen) return;

  rc = ::send(this->s, buf, len, 0);    
  if (rc)
    gle = WSAGetLastError();
  }

void jsocket::recv(char* buf, int maxlen)
  {
  int rc;
  int gle;
  if (!this->isOpen) return;
    
  rc = ::recv(this->s, buf, maxlen, 0);    
  if (rc)
    gle = WSAGetLastError();
  }

unsigned long jsocket::avail()
  {
  int rc;
  int gle;
  u_long arg;

  if (!this->isOpen) return 0;
  arg = 0;
  rc = ioctlsocket (this->s, FIONREAD, &arg);
  if (rc)
    {
    gle = WSAGetLastError();
    return 0;
    }

  return arg;
  }

//private from here on

void jsocket::startup()
  {
  WORD wVersionRequested;
  WSADATA wsaData;
  wVersionRequested = MAKEWORD( 2, 2 );
  WSAStartup(wVersionRequested,  &wsaData );
  }

void jsocket::open()
  {
  int gle;

  this->s = socket(AF_INET, SOCK_STREAM, 0);
  if (this->s == INVALID_SOCKET)
    gle = WSAGetLastError();
  else
    this->isOpen = true;
  }

void jsocket::close()
  {
  if (!this->isOpen) return;
  closesocket(this->s);
  this->isOpen = false;
  }

void jsocket::cleanup()
  {
  close();
  WSACleanup( );
  }

void jsocket::fillSocketAddress(char* ipaddr)
  {
  memset(&this->addr, 0, sizeof(SOCKADDR_IN));
  this->addr.sin_family = AF_INET;
  this->addr.sin_port = htons(this->port);

  //todo: convert string to an actual addr
  this->addr.sin_addr.S_un.S_un_b.s_b1 = 127;
  this->addr.sin_addr.S_un.S_un_b.s_b2 = 0;
  this->addr.sin_addr.S_un.S_un_b.s_b3 = 0;
  this->addr.sin_addr.S_un.S_un_b.s_b4 = 1;
  }


jsocket.h

Synopsis
#pragma once

#include <windows.h>
class jsocket
  {
  public:
    jsocket( char* ipaddr, int port);
    ~jsocket();

    void connect();
    void send(char* buf, int len);
    void recv(char* buf, int maxlen);
    unsigned long avail();

  private:
    void startup();
    void cleanup();
    void close();
    void open();
    void fillSocketAddress(char* ipaddr);

    SOCKET s;
    bool isOpen;
    SOCKADDR_IN addr;
    u_short port;
  } ;






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001-2010