service


Most hosts on the Internet provide a set of standard services via well-known, numbered ports. Each service has a description, a port number, and a protocol description (either TCP or UDP).

You can construct an os_service from either a description/protocol pair or a port/protocol pair. Most services can be accessed via either TCP or UDP protocols. The following example illustrates both modes of construction.

Example <ospace/network/examples/service1.cpp>
#include <iostream>
#include <ospace/network.h>

void
main()
  {
  os_network_toolkit initialize;

  os_service service1( "echo", "tcp" );
  cout << endl << "service1 = " << service1 << endl;

  os_service service2( 37, "tcp" );
  cout << "service2 = ";
  }

service1 = os_service( echo, 7/tcp )
service2 = os_service( time, 37/tcp )

The next example shows how to use os_service , os_tcp_socket , and os_tstream to get the current date and time from the daytime service.

Example <ospace/network/examples/service2.cpp>
#include <iostream>
#include <ospace/network.h>
#include <ospace/stream.h>

void
main()
  {
  os_network_toolkit init_network;
  os_streaming_toolkit init_streaming;

  // Attach to the DAYTIME tcp port on a UNIX host.
  os_tcp_socket socket;
  os_service daytime( "daytime", "tcp" );
  socket.connect_to( os_socket_address( "solaris", daytime.port() ) );

  // Read time and date.
  char day[ 10 ];
  char month[ 10 ];
  int date;
  char time[ 10 ];
  int year;

  os_tstream stream( socket ); // Create a text stream on the socket.
  stream >> day >> month >> date >> time >> year; // Read time info.

  cout << "The day is " << day << "." << endl;
  cout << "The date is " << month << " " << date << ", " << year;
  cout << "." << endl;
  cout << "The time is " << time << "." << endl;
  }

The day is Tue.
The date is Oct 24, 1995.
The time is 00:13:06.

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.