slang-users mailing list

[2010 Date Index] [2010 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] socket example


Hi Jean-Michel,
> hello i have done a small TCP client in C 
> http://jeanmichel.gens.free.fr/src/say.c
> i wonder how to translate in slang language
> i have found some functions in documentation
> socket , connect
> but not read and write

The `read', and `write' functions are documented at
<http://www.jedsoft.org/slang/doc/html/slangfun-15.html>.  You can
also use `fdopen' to get a stdio file descriptor and use the stdio
functions.  The stdio functions hide much of the detail that you would
have to worry about with the read/write functions.  Here is an example
of the latter approach based upon your c code.  Note that I changed
the port to 25 (smtp) for testing purposes.

I hope that this helps.  Thanks, --John

require ("socket");
private variable PORT=25;
define slsh_main ()
{
   variable
     msg = __argv[1] + "\n",
     msglen = strbytelen (msg);

   variable s = socket (AF_INET, SOCK_STREAM, 0);
   connect (s, "127.0.0.1", PORT);

   variable fp = fdopen (s, "r+");

   if ((msglen != fwrite (msg, fp))
       || (-1 == fflush (fp)))
     throw WriteError, "write failed: " + errno_string();

   % Read 56 bytes
   if (56 != fread_bytes (&msg, 56, fp))
     throw ReadError, "read failed: " + errno_string ();

   vmessage ("Read: %s", msg);

   () = close (s);
}



[2010 date index] [2010 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]