Skip to content
Permalink
Browse files

added examples "ex_tcp_client2.ml" and "ex_tcp_server2.ml"

  • Loading branch information...
fccm committed Aug 29, 2019
1 parent 645a3fa commit 32945b77f90fea52d53a631f8ee9ba39d1ff4584
Showing with 49 additions and 0 deletions.
  1. +21 −0 examples/ex_tcp_client2.ml
  2. +28 −0 examples/ex_tcp_server2.ml
@@ -0,0 +1,21 @@

let () =
let port = 2000 in
let server = SFIpAddress.localHost () in

(* Create a socket for communicating with the server *)
let socket = SFTcpSocket.create () in

(* Connect to the server *)
SFTcpSocket.connect socket port server ();
print_endline ("Connected to server " ^ (SFIpAddress.toString server));

(* Receive a message from the server *)
let received = SFTcpSocket.receive_str socket in
print_endline ("Message received from the server: '" ^ received ^ "'");

(* Send an answer to the server *)
let out = "Hi, I'm a client" in
SFTcpSocket.send socket out;
print_endline ("Message sent to the server: '" ^ out ^ "'");
;;
@@ -0,0 +1,28 @@

let () =
let port = 2000 in

(* Create a server socket to accept new connections *)
let listener = SFTcpListener.create () in

(* Listen to the given port for incoming connections *)
SFTcpListener.listen listener port;

print_endline ("Server is listening to port " ^
(string_of_int port) ^ ", waiting for connections... ");

(* Wait for a connection *)
let socket = SFTcpListener.accept listener in

print_endline ("Client connected: " ^ (SFIpAddress.toString (
SFTcpSocket.getRemoteAddress socket)));

(* Send a message to the connected client *)
let out = "Hi, I'm the server" in
SFTcpSocket.send socket out;
print_endline ("Message sent to the client: '" ^ out ^ "'");

(* Receive a message back from the client *)
let received = SFTcpSocket.receive_str socket in
print_endline ("Answer received from the client: '" ^ received ^ "'");
;;

0 comments on commit 32945b7

Please sign in to comment.
You can’t perform that action at this time.