[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7.5.1 EN Socket

The EN Network Driver consists of two components.

Network Driver
System independent network driver. This driver implements a TCP/IP networking interface. The network driver and socket driver currently support BSD compliant calls.

Note: Some operating systems may not support all the functions of this plugin.

Network Driver
Network Driver. This driver is used to create an implementation of the Socket Driver via the CreateSocket() call. The Network Driver calls appropriate socket startup/shutdown calls for the operating system.

Socket Driver
This driver handles all the connection functions. This driver is implemented by calling the CreateSocket() call located in the Network Driver. The Socket Driver supports the following features (some might not work on all operating systems):

Network Driver

The Network Driver is used to setup the socket sub-system. The driver's purpose is to provide a method for creating a BSD sockets compliant socket of either UDP or TCP type. Upon construction all appropriate socket start-up calls are made, like wise with the destructor.

CreateSocket()
Returns a socket that has been created with the type of TCP or UDP.
LastError()
Returns the last error that was encountered.

Socket Driver

The Socket Driver is used to supply a common interface to a low-level TCP/IP network connection. It allows connecting/accepting TCP and UDP based socket connections. It provides very basic BSD compliant functionality.

LastError()
Returns the last error that was encountered.
LastOSError()
Returns the last error that is returned by the operating system.
IsConnected()
Returns true or false depending upon the status of the connection.
SetSocketBlock()
This function sets the blocking/non-blocking attribute of the socket.
SetSocketReuse()
This function sets the "reuse" attribute of a listening socket.
Connect()
This function connects to a remote host and port.
Send()
This function sends a number of bytes to the remote connection.
Recv()
This function reads a number of bytes from the remote connection.
Close()
This function closes the socket and it is no longer valid after this point.
Disconnect()
This function closes the connection then closes the socket.
WaitForConnection()
This function sets the socket up for incoming connections on a port.
Accept()
This function returns a socket that is connected to a incoming connection from a remote system.
set()
This function is for internal use and should not be called.
ReadLine()
This function performs a buffered read in both tcp blocking/non-blocking modes.
RemoteName()
This function returns the remote name/IP of the remote connection.

Example

Here is a simple example showing the module in use.

 
#include "inetwork/driver2.h"
#include "inetwork/socket2.h"
#include "inetwork/sockerr.h"

csRef<iNetworkDriver2> netdriver;
csRef<iSocketDriver2> client;
csRef<iSocketDriver2> server;

...

netdriver = CS_QUERY_REGISTRY(object_reg, iNetworkDriver2);
if (!netdriver)
{
  ...
}

// Create TCP socket.
server = netdriver->CreateSocket(CS_NET_SOCKET_TYPE_TCP);

// Last error is netdriver->LastError().
if (server)
{
  server->SetSocketReuse(true);
  if (server->LastError() != CS_NET_SOCKET_NOERROR)
    printf("Unable to set reuse option.\n");
  server->SetSocketBlock(true);
  if (server->LastError() != CS_NET_SOCKET_NOERROR)
    printf("Unable to set block option.\n");
  // Wait on port 9999 queue up to 5 connections.
  server->WaitForConnection(0, 9999, 5);
  if (server->LastError() != CS_NET_SOCKET_NOERROR)
    printf("Unable to bind to port.\n");
}

...

if (server)
{
  client = server->Accept();
  if (client != 0)
  {
    // We have a connection.
    client->Send("hello\r\n",7);
    client->Disconnect();
  }
  server->Close();
}


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated using texi2html