Class OcsfmlNetwork.tcp_socket

class tcp_socket : object .. end
Specialized socket using the TCP protocol.

TCP is a connected protocol, which means that a TCP socket can only communicate with the host it is connected to.

It can't send or receive anything if it is not connected.

The TCP protocol is reliable but adds a slight overhead. It ensures that your data will always be received in order and without errors (no data corrupted, lost or duplicated).

When a socket is connected to a remote host, you can retrieve informations about this host with the getRemoteAddress and GetRemotePort functions. You can also get the local port to which the socket is bound (which is automatically chosen when the socket is connected), with the getLocalPort function.

Sending and receiving data can use either the low-level or the high-level functions. The low-level functions process a raw sequence of bytes, and cannot ensure that one call to Send will exactly match one call to Receive at the other end of the socket.

The high-level interface uses packets (see sf::Packet), which are easier to use and provide more safety regarding the data that is exchanged. You can look at the sf::Packet class to get more details about how they work.

The socket is automatically disconnected when it is destroyed, but if you want to explicitely close the connection while the socket instance is still alive, you can call disconnect.

Usage example:

    (* ----- The client ----- *)
    
    (* Create a socket and connect it to 192.168.1.50 on port 55001 *)
    let socket = new tcp_socket in
    socket#connect (new ip_address (`String "192.168.1.50")) 55001 ;
    
    (* Send a message to the connected host *)
    let message = "Hi, I am a client" in
    
    socket#send_string message;
    
    (* Receive an answer from the server *)
    let buffer = String.create 1024 in
    let (status, received) = socket#receive_string buffer in
    print_string "The server said: " ; 
    output stdout buffer 0 received ;
    print_newline () ;
    
    (* ----- The server ----- *)

    (* Create a listener to wait for incoming connections on port 55001 *)
    let listener = new listener in
    listener#listen 55001 ;
    
    (* Wait for a connection *)
    let socket = new tcp_socket in
    listener#accept socket;
    print_endline ("New client connected: " + socket#get_remote_address#to_string) ;
    
    (* Receive a message from the client *)
    let buffer = String.create 1024 in
    let (status, received) = socket#receive_string buffer in
    print_string "The client said: " ; 
    output stdout buffer 0 received ; 
    print_newline () ;
    
    (* Send an answer *)
    let message = "Welcome, client";
    socket#send_string message;
    

Inherits
method connect : ?timeout:OcsfmlSystem.Time.t ->
ip_address ->
Port.t -> socket_status
Connect the socket to a remote peer.

In blocking mode, this function may take a while, especially if the remote peer is not reachable. The timeout parameter allows you to stop trying to connect after a given timeout. If the socket was previously connected, it is first disconnected.
Returns Status code

timeout : Optional maximum time to wait
method destroy : unit
method disconnect : unit
Disconnect the socket from its remote peer.

This function gracefully closes the connection. If the socket is not connected, this function has no effect.

method get_local_port : Port.t
Get the port to which the socket is bound locally.

If the socket is not connected, this function returns 0.
Returns Port to which the socket is bound.

method get_remote_address : ip_address
Get the address of the connected peer.

It the socket is not connected, this function returns IPAddress.none.
Returns Address to the remote peer

method get_remote_port : Port.t
Get the port of the connected peer to which the socket is connected.

If the socket is not connected, this function returns 0.
Returns Remote port to which the socket is connected

method receive_data : OcsfmlSystem.raw_data_type -> socket_status * int
Receive raw data from the remote peer.

In blocking mode, this function will wait until some bytes are actually received. This function will fail if the socket is not connected.
Returns Status code and the actual number of byte received.

method receive_packet : 'a. (#packet as 'a) -> socket_status
Receive a formatted packet of data from the remote peer.

In blocking mode, this function will wait until the whole packet has been received. This function will fail if the socket is not connected.
Returns Status code

method receive_string : string -> socket_status * int
Receive raw data from the remote peer.

In blocking mode, this function will wait until some bytes are actually received. This function will fail if the socket is not connected.
Returns Status code and the actual number of byte received

method send_data : OcsfmlSystem.raw_data_type -> socket_status
Send raw data to the remote peer.

This function will fail if the socket is not connected.
Returns Status code

method send_packet : 'b. (#packet as 'b) -> socket_status
Send a formatted packet of data to the remote peer.

This function will fail if the socket is not connected.
Returns Status code

method send_string : string -> socket_status
Send raw data to the remote peer.

This function will fail if the socket is not connected.
Returns Status code