Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages | Examples

socketClientExample.cpp

ArSocket example client program

This is an example to show how to use ArSocket to create a client and a server. This is the client program. The server is socketServerExample.cpp.

This connects to a server on the host 'localhost' on port 7777. It waits for the server to say hello, then it says hello in reponse. It then closes the socket and exits.

First run the server program socketServerExample. Then run this client program on the same computer, perhaps in a different terminal window. You should see the server accept the connection, send its greeting to this client program, and see this client respond with its greeting text to the server.

See also:
ArSocket

socketServerExample.cpp

Also see the ArNetworking library for a more complete networking framework.

#include "Aria.h"


int main()
{
  // The string to send to the server. 
  char *strToSend="Hello Server";
  // The buffer in which to recieve the hello from the server
  char buff[100];
  // The size of the string the server sent
  size_t strSize;

  // The socket object
  ArSocket sock;

  // Initialize Aria.  It is especially important to do
  // this on Windows, because it will initialize Window's
  // sockets system. 
  Aria::init();

  // Connect to the server
  ArLog::log(ArLog::Normal, "socketClientExample: Connecting to localhost TCP port 7777...");
  if (sock.connect("localhost", 7777, ArSocket::TCP))
    ArLog::log(ArLog::Normal, "socketClientExample: Connected to server at localhost TCP port 7777.");
  else
  {
    ArLog::log(ArLog::Terse, "socketClientExample: Error connecting to server at localhost TCP port 7777: %s", sock.getErrorStr().c_str());
    return(-1);
  }

  // Read data from the socket. read() will block until
  // data is received. 
  strSize=sock.read(buff, sizeof(buff));

  // If the amount read is 0 or less, its an error condition.
  if (strSize > 0)
  {
    buff[strSize]='\0'; // Terminate the string with a NULL character:
    ArLog::log(ArLog::Normal, "socketClientExample: Server said: \"%s\"", buff);
  }
  else
  {
      ArLog::log(ArLog::Terse, "socketClientExample: Error in waiting/reading from the server.");
    return(-1);
  }

  // Send the string 'Hello Server' to the server. write() should
  // return the same number of bytes that we told it to write. Otherwise,
  // its an error condition.
  if (sock.write(strToSend, strlen(strToSend)) == strlen(strToSend))
    ArLog::log(ArLog::Normal, "socketClientExample: Said hello to the server.");
  else
  {
      ArLog::log(ArLog::Terse, "socketClientExample: Error sending hello string to the server.");
    return(-1);
  }

  // Now close the connection to the server
  sock.close();
  ArLog::log(ArLog::Normal, "socketClientExample: Socket to server closed.");

  // Uninitialize Aria and exit
  Aria::uninit();
  return(0);
}

Generated on Thu Jan 7 10:34:38 2010 for Aria by  doxygen 1.4.2