tcp_socket, tcp_connection_server |
To establish TCP socket
communication, use the os_tcp_socket and os_tcp_connection_server
classes. The following sequence describes how communication is established
between a client and a server.
os_tcp_connection_server
and binds it to an address that is known to the server process' clients.os_tcp_socket
on an arbitrary port. This is the server endpoint for the final connection.os_tcp_connection_server
in the server process waits for an incoming connection request using accept()
and passes accept() a reference to the os_tcp_socket
created in step 2.os_tcp_socket
on an arbitrary port.connect_to()
to connect the os_tcp_socket to the os_tcp_connection_server.os_tcp_socket
objects, created in step 2 and step 4, are connected. This connection
returns accept() in the server process and
returns connect_to() in the client process. Any
data written to the server socket appears as input to the client socket; any
data written to the client socket appears as input to the server socket.
Because stream communication is reliable, the sender is notified if any
bytes sent are not received. After communication is
established, the server can issue another accept()
command to the connection server and then wait for additional clients. A typical
server often spawns a thread to service a client connection; then the server
quickly returns to accept another connection.
The following example illustrates simple TCP communications between a client and a server. The first program, socket1s.cpp, is the server. The server process creates a connection server and waits for the client to request a connection. The second program, socket1c.cpp, is the client. The client process connects to the server process and sends it a simple text string.
// Example socket1 server.
#include <iostream>
#include <ospace/network.h>
void
main()
{
os_network_toolkit initialize;
// Create connection server with well-known port on local host.
os_tcp_connection_server server( os_socket_address( 3000 ) );
os_tcp_socket socket;
server.accept( socket ); // Accept incoming connection.
cout << "Server accepts connection from: " << endl;
cout << " " << socket.peer_address() << endl;
// Echo text till end of input.
char buffer[ 100 ];
int result = socket.read( buffer, sizeof( buffer ) );
buffer[ result ] = 0; // Null terminate result.
cout << "Server read: " << buffer << endl;
}
Server accepts connection from:
os_socket_address( os_ip_address( 128.200.51.1 ), port 46283 )
Server read: hello there
// Example socket1 client.
#include <iostream>
#include <ospace/network.h>
void
main()
{
os_network_toolkit initialize;
// Create with unique system assigned port number.
os_tcp_socket socket;
// Connect to server on my host.
socket.connect_to( os_socket_address( 3000 ) );
cout << "Client connected to: " << endl;
cout << " " << socket.peer_address() << endl;
cout << "Client sends: hello there" << endl;
socket.write( "hello there", 11 );
}
Client connected to:
os_socket_address( os_ip_address( 128.200.51.1 ), port 3000 )
Client sends: hello there
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.