Proprietary TCP Connection Over HTTP(S)

Using the Web-server for initiating a proprietary (secure) TCP server connection.

The following example shows how to use the web-server for initiating a TCP connection. Services are normally differentiated by port numbers. When using the web-server to initiate the connection, port 80 is used for all services. Port 443 is used for all services if a secure connection is needed. Thus another mechanism is needed for differentiating the various services installed. When using a web-server for initiating a TCP connection, the services are differentiated by the URL to where the service is installed. The initial HTTP connection is only used for locating the service. As soon as the service starts, a pure TCP connection is established.

Client Code

For a client to initiate a connection, the client starts by sending out HTTP data.

For example, assuming a service is at URL: http://ipaddress/myservice.html, the client sets up a connection as follows:

static const char http[] = {
"GET /myservice.html HTTP/1.0\r\n"

s = socket(AF_INET, SOCK_STREAM, 0);
//Init addr
connect(s,(struct sockaddr *) &addr, sizeof(addr));
//Connect to service
send(s, http, sizeof(http))
//We can now use the socket as a regular TCP stream.

The above code is using a non secure connection. To use a secure connection, a SSL client library must be used on top of the socket.

Server Code

The server code typically comprises two files: a CSP file and a C file for the actual socket implementation.

For the above example, a CSP page named myservices.html must be put in the root directory. The CSP code calls a function in the C file for initiating the connection. The code in the C file moves the active socket connection out of the web-server's request object and uses the active socket connection for communicating with the peer.

Secure connection:
A secure connection is transparent to the server implementation. The underlying web-server socket code is transparently managing the secure connection. The client can decide to use a secure connection or not. Typically, web-servers listen for HTTP on port 80 and HTTPS on port 443. The client can connect to port 80 for non secure connections and port 443 for secure connections.

Example Code

The example server code implements an echo client. Whatever is sent to the server is echoed back to the client.

The example code in this release includes only the server side. To test the code: copy and paste the following into a terminal window:

telnet localhost 9357
GET / HTTP/1.0

Replace "localhost" with the IP address of the server and 9357 with the port number that the server is listening on.

As soon as the above is pasted into the terminal, any characters typed in the terminal are echoed back to the client.

csp/index.html: The bridge between the web-server and the TCP server code.
src/MyService.c: The implementation for the echo server.