Socket linked library
The socket library provides functions for IPv4 and IPv6 TCP/IP connection protocol,
which may be called from Cind programs.
To connect socket library use syntax of getting linked libraries,
with "socket" word as the second argument:
var lsocket = @("lib","socket");
For more about connecting libraries see Using linked libraries.
Functions
List of functions available from the socket library:
function |
args |
type of arguments |
type of result |
description |
connect |
2 |
(string name,int port) |
int |
creates the socket and connects to given server on given port number,
returns the socket as number. |
send |
4 |
(int socket,sheet data,int off,int count) |
int |
sends a data on a socket, returns number of bytes sent. |
recv |
3 |
(int socket,sheet data,int count) |
int |
receives a data from a socket, returns number of bytes received. |
shutdown |
2 |
(int socket,int how) |
- |
shut down part of the connection. 'how' parameter values:
1 - close receiving, 2 - close sending, 3 - close both. |
close |
1 |
(int socket) |
- |
closes a socket. |
bind |
1 |
(int port) |
int |
creates the socket and bind to given port number,
returns the socket as number. |
listen |
2 |
(int socket,int qsize) |
- |
listen for connection on a given socket, setting also the length of waiters queue. |
accept |
1 |
(int socket) |
int |
accepts another incomming connection, returns the socket of the new connection. |
getPeerHost |
3 |
(int socket,int ipv,sheet name) |
int |
gets the IP number of the connection, the 'ipv' parameter should be 4 or 6, saying,
that resuling string must be in IPv4 or IPv6 format, returns number of written bytes. |
getPeerPort |
3 |
(int socket) |
int |
returns the port number of the connection of given socket. |
Remarks:
- Those functions allows to implement client and server TCP/IP applications.
- The server may accpet both IPv4 and IPv6 connections.
- The IP number of the calling peer may be readed in visual form
in both IPv4 and IPv6 formats.
- The data parameter of
send and recv functions
is a part of memory, in which data will be written or readed from
(see Sheet object).
Example of usage
// get socket library
var libsocket = @("lib","socket");
// connect to some point in the internet
int socket = libsocket.connect("www.cindlang.com",80);
// send data
{
sheet data = (sheet)"GET / HTTP/1.0\r\n\r\n";
int datalen = data.size();
int was = libsocket.send(socket,data,0,datalen);
// assuming here that all data has been sent (if not, send them in some loop)
}
// receive data
{
sheet recBuffer = ((sheet)"").setSize(512);
int was;
while((was = libsocket.recv(socket,recBuffer,512)) > 0) {
sheet receivedPart = recBuffer.subSheet(0,was);
host.print(receivedPart);
}
}
// close socket
libsocket.close(socket);
|
|